In D7,I have new_form with a managed_file field to allow my user to upload documents to public://. I'm trying to prevent my user from uploading a document if the file name they are trying to upload already exists in the system. So far I've been successful using this:
function mymodule_file_validate($file) {
$filename = $file->filename;
$errors = array();
if (file_exists('public://' . $filename)) {
$errors[] = t("File exists already!.");
}
return $errors;
}
This displays an error if the user browses for the file and presses upload, even before the file submission, which is exactly what I need.
However, I also have another form modify_form in this module that allows users to modify the document and in that case it's okay to upload the document with the same name even though the file name exists already. BUT... the function above blocks my modify_form too.
Any suggestions how to make the function apply only to my new_form? Or another way to prevent upload if file exists already only on new_form? Thanks!