Create folder for uploaded files
Create folder for uploaded files
lincolncbennett
Posts: 25Questions: 11Answers: 1
Hi.
I am trying to upload many files to dynamically created folders based on the primary id. Basically on selection of table row, if a file is uploaded, create a folder named after the id of the table and upload file to it. I have read https://datatables.net/forums/discussion/31884 but cannot get to work.
Any help would be great.
My PHP........ (upload many)
<?php
include( "lib/DataTables.php" );
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Mjoin,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
Editor::inst( $db, 'yamba' ,'id' )
->fields(
Field::inst( 'yamba.client_status_sort' ),
Field::inst( 'yamba.icon' ),
Field::inst( 'yamba.client_status_change_date' ),
Field::inst( 'yamba.id' ),
Field::inst( 'yamba.year' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'yamba.name' )
->validator( 'Validate::notEmpty' ),
Field::inst( 'yamba.client_status' ),
Field::inst( 'yamba.active_1' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
}),
Field::inst( 'yamba.active_2' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
}),
Field::inst( 'yamba.active_3' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
}),
Field::inst( 'yamba.active_4' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
}),
Field::inst( 'yamba.active_5' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
}),
Field::inst( 'yamba.active_6' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
}),
Field::inst( 'yamba.active_7' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
}),
Field::inst( 'yamba.active_8' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
}),
Field::inst( 'yamba.active_9' )
->setFormatter( function ( $val, $data, $opts ) {
return ! $val ? 0 : 1;
}),
Field::inst( 'yamba.contact_relationship_1' ),
Field::inst( 'yamba.contact_relationship_2' ),
Field::inst( 'yamba.add_contact_1' ),
Field::inst( 'yamba.file_note_1' ))
->join(
Mjoin::inst( 'files', 'id' )
->link( 'yamba.id', 'users_files.user_id' )
->link( 'files.id', 'users_files.file_id' )
->fields(
Field::inst( 'id' )
->upload( Upload::inst( $_SERVER['DOCUMENT_ROOT'].'/funeral/uploads/__NAME__' )
->db( 'files', 'id', array(
'fileName' => Upload::DB_FILE_NAME,
'fileSize' => Upload::DB_FILE_SIZE,
'webPath' => Upload::DB_WEB_PATH,
'systemPath' => Upload::DB_SYSTEM_PATH
) )
->validator( function ( $file ) {
return$file['size'] >= 500000000 ?
"Files must be smaller than 50000K" :
null;
} )
->allowedExtensions( array( 'png', 'jpg', 'pdf', 'jpeg', 'doc', 'docx', 'csv' ), "Please upload an image" )
)
)
)
->process( $_POST )
->json();
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Rather than using:
You would need to perform a custom upload action that will create the directory (
mkdir
in PHP) and move the file into the correct place.Allan
Hi Al, thanks for the reply. I have been looking at this all day and cannot figure out. I'm sorry. I have changed the PHP as shown below and although there is no error on upload, the folder is not being created or file being uploaded. thanks
I don't see a call to the PHP
mkdir()
function in your code above. How are you currently attempting to create the directory?Allan
I have been trying to implement this example as shown in the linked discussion ;
with the custom upload in my last post, I am not seeing any errors when I upload but the file does not upload to the folder specified.
With the mkdir() above and this code I can save files to the -1 folder which is being created.
I can't seem to figure out how to pass the id (primary) of the table to the php, so the _GET can pass it to the upload () when editor is opened?
Any help would be great.
Full code