Validator to check if file already exists before uploading

Home Forums Quform PHP Validator to check if file already exists before uploading

This topic is: resolved
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #29109
    waubain
    Participant

    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);
    
    #29111
    waubain
    Participant

    I realized that ‘inArray’ will not work at this point since the ‘filename’ does not yet exist until farther in the code.

    #29116
    waubain
    Participant

    The 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
    
    #29128
    Ally
    Support Staff

    You don't have permission to view this content. Please log in or register and then verify your purchases to gain access.

    #29135
    waubain
    Participant

    Works perfectly. Thank you for the code and a great product.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.
Be inspired. © 2024 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy