File Upload

File Upload

aarontharkeraarontharker Posts: 41Questions: 11Answers: 0
edited March 2017 in Editor

Hi, I'm trying to use the file upload on a new table and having issues. When I try to upload I'm getting a server error. If I check the network response from the upload it is getting a PHP warning saying no such file or stream exists, I've basically just copied your examples changing the various fields to suit my tables.

My js file has the following

{
"label": "ID image:",
"name": "id_file",
"type": "upload",
display: function ( id ) {
return '<img src="'+editor.file( 'files', id ).systemPath+'"/>';
},
noImageText: 'No image'
},

My php file this

Field::inst( 'id_file' )
->upload(
Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/uploads/__ID__/__NAME__' )
->db( 'files', 'files_id', array(
'fileName' => Upload::DB_FILE_NAME,
'systemPath' => Upload::DB_SYSTEM_PATH
) )
->dbClean( function ( $data ) {
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
unlink( $data[$i]['systemPath'] );
}
return true;
} )
},

I have two different tables the applicants table has a field called id_file and the files table has files_id, fileName, systemPath. When I upload a file the files table gets filled in but nothing is added to the applicants table and the file is not moved into the correct folder on the server. All the fields in the files table get filled in correctly though?

This question has an accepted answers - jump to answer

Answers

  • aarontharkeraarontharker Posts: 41Questions: 11Answers: 0

    Additionally, the ID being used in the file path is the id number from the files table, is there any simple way of using the id from the applicants table instead as each applicant will have multiple different files attached and it would make recovery after a failure easier if they were grouped together that way.

  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    edited March 2017 Answer ✓

    The problem is that the upload class won't create directories for you - you are using:

    $_SERVER['DOCUMENT_ROOT'].'/uploads/__ID__/__NAME__'

    Whereas the demo uses:

    $_SERVER['DOCUMENT_ROOT'].'/upload/__ID__.__EXTN__'

    If you want to create a new directory for every file, you'd need to use a custom upload action along with PHP's mkdir function to create the directory and then move it into place.

    Allan

This discussion has been closed.