Editor - How to upload files in specific folder with custom validation action
Editor - How to upload files in specific folder with custom validation action
Hello,
I would like to use Editor and the upload functionnality in order to create a simple gallery.
So, for my example, I have 2 tables.
The first one named gallery to store galleries with 3 columns:
- ID (primary key)
- "Description"
- img_id (the id for the photo)
The second one named gallery_files with 5 columns (like in the documentation) to store uploaded photos:
- id
- fileName
- fileSize
- web_path
- system_path
I just want to upload photos in specific folder, not in $_SERVER['DOCUMENT_ROOT'] like it seems to be done by default.
Can you give me the way to do it please?
I have tried to use "Custom validation" but without success (I don't really understand how to use it in fact)
If my web site is stored on 'D:/WebSite', I simply want to store my images into the following folder : $_SERVER['CONTEXT_DOCUMENT_ROOT'] . /gallery/ (so into 'D:/WebSite/gallery')
Here is a piece of code I need to modify in order to make it works. It seems quite simple but I am not able to make it works.
Editor::inst( $db, 'gallery', 'id' )
->fields(
Field::inst( 'gallery.id' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'gallery.description' ),
Field::inst( 'gallery.img_id' )
->setFormatter( Format::ifEmpty( null ) )
->upload( Upload::inst( $_SERVER['CONTEXT_DOCUMENT_ROOT'].'gallery/__ID__.__EXTN__' )
/*
->upload( Upload::inst(
function ( $file, $id ) {
move_uploaded_file( $file['tmp_name'], $_SERVER['CONTEXT_DOCUMENT_ROOT'].'gallery/'.$id);
return $id;
} )
*/
->db( 'gallery_files, 'id', array(
'filename' => Upload::DB_FILE_NAME,
'filesize' => Upload::DB_FILE_SIZE,
//'web_path' => 'galeries/'.Upload::DB_WEB_PATH,
'web_path' => 'gallery/__ID__.__EXTN__',
//'system_path' => Upload::DB_SYSTEM_PATH
'system_path' => 'D:/WebSite/gallery/__ID__.__EXTN__'
) )
->dbClean( function ( $data ) {
// Remove the files from the file system
for ( $i=0, $ien=count($data) ; $i<$ien ; $i++ ) {
unlink( $data[$i]['systemPath'] );
}
// Have Editor remove the rows from the database
return true;
})
)
)
->process( $_POST )
->json();
Thanks in advance for your help and have a nice day.
Answers
After several hours to look for a solution, I finnaly found it on the forum.
You can have a look to these posts if you are interested with a really good anwser to my problem.
https://datatables.net/forums/discussion/34579/php-file-upload-with-custom-upload-actions
and
https://datatables.net/forums/discussion/comment/108080/#Comment_108080
I hope it will help someone like it helped me.