Reply To: Validator to check if file already exists before uploading

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

#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
Be inspired. © 2019 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy