Trouble with Editor uploads and file validation
Trouble with Editor uploads and file validation
I have the following code.
The problem is that the portion of it that clears the DB and file system runs into problems when validation fails due to image size being too high. I think the code is then attempting to remove a non-existent entry from the DB and file system.
I've tried various different orders of placing the functions, to no avail. Is there a correct way to stop this happening?
$editor = Editor::inst( $db, 'Branding', 'id' )
->fields(
Field::inst( 'email_domain' ),
Field::inst( 'company_name' ),
Field::inst( 'override_group' ),
Field::inst( 'company_logo' )
->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploader/logos/__NAME__' )
->db( 'Logos', 'id', array(
'filename' => Upload::DB_FILE_NAME,
'filesize' => Upload::DB_FILE_SIZE,
'web_path' => Upload::DB_WEB_PATH,
'system_path' => Upload::DB_SYSTEM_PATH
) )
->validator( function ( $file ) {
if ( $file['size'] >= 100000 ) {
return "Max: 100K";
}
return null;
} )
->dbClean( function ( $data ) {
// Remove the files from the file system
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
unlink( $data[$i]['system_path'] );
}
// Remove the rows from the database
return true;
} )
->allowedExtensions( array( 'png', 'jpg', 'gif' ), "png / jpg / gif" )
)
->setFormatter( 'Format::nullEmpty' )
)
->process( $_POST )
->json();
From the server log:
PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpJKOKMM' to '/var/www/html.....
PHP Warning: unlink(/var/www/html/..../Screen Shot 2017-04-22 at 15.02.24.png): No such file or directory
Many thanks in advance for any tips.
This question has accepted answers - jump to:
Answers
Hi,
It would be worth adding an
if ( is_file( $data[$i]['system_path'] ) )
condition around theunlink
line (line 25 above) and see if that addresses it.Regards,
Allan
Thanks Allan, that works perfectly!
I was thinking along those lines, but I wasn't sure how to view the array contents of $file or $data. Inserting a print_r line into the function for test purposes didn't seem to output the array contents anywhere visible.
What I normally do is use
file_put_contents()
in PHP and have it log information to a file, so I can justtail -f
that in a console and see the debug trace information live.Allan
Good plan, thanks Allan!
I use the tail -f method for the web server error log already, so this makes total sense.
Hmm, if I now add additional validation (such as notEmpty), the problem reoccurs.
I'm only posting this for informational value; I've decided to remove the file system purging as the database still seems to update correctly without the dbclean function.