Home › Forums › Quform PHP › Validator to check if file already exists before uploading
- This topic has 4 replies, 2 voices, and was last updated 5 years, 7 months ago by waubain.
- AuthorPosts
- April 19, 2019 at 1:43 pm #29109waubainParticipant
I looked through the documentation, but I could not see a Validator that checks if a file exists. I saw the GetFileUploadValidator, but that did not seem to contain a file check.
I could create an array of all the files in a directory/folder, and then using InArray Validator, but I would prefer to do is alert the user of the duplication and then give the option to Cancel or Overwrite the current file.
Here is my current Validator/Filter with the InArray validator added.
/** * Configure the upload element * Filters: (None) * Validators: FileUpload (Added automatically) */ $upload = new Quform_Element_File('upload'); $upload->addFilters(array('trim', 'filename')); $upload->addValidator('inArray', array('haystack' => $curFiles)); $upload->getFileUploadValidator() ->setRequired(true) ->setAllowedExtensions(array('pdf')) ->setMaximumFileSize(10485760); $form->addElement($upload);
April 19, 2019 at 8:27 pm #29111waubainParticipantI realized that ‘inArray’ will not work at this point since the ‘filename’ does not yet exist until farther in the code.
April 21, 2019 at 12:07 am #29116waubainParticipantThe below piece of code works in a test page, but I don’t know where I can place it to make it work properly within your code, if at all.
The form is being used to upload a file to a directory and not to send an email or to a database.
/** Create an array of file names in handout directory to check if file already exists **/ $dir = '../../handouts'; // get directory files $dirfilenames = scandir($dir); //get name of file to be uploaded $files = $form->getValue('upload'); $filename = $files[0]['filename']; //check if file exists foreach( $dirfilenames as $value ){ if( $value == $filename ){ //terminating script with a message using exit() exit('File already exists!'); } } /** End of file exists check **/ // Save to a MySQL database
April 22, 2019 at 2:00 pm #29128AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
April 22, 2019 at 7:18 pm #29135waubainParticipantWorks perfectly. Thank you for the code and a great product.
- AuthorPosts
- You must be logged in to reply to this topic.