Home › Forums › Quform PHP › Validator to check if file already exists before uploading › Reply To: Validator to check if file already exists before uploading
April 21, 2019 at 12:07 am #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