using web recieve for 2 different file upload input in one form.

73 views
Skip to first unread message

DevMan12

unread,
Nov 11, 2016, 8:17:34 AM11/11/16
to Fat-Free Framework
I have 2 upload file inputs in a form, how can I make web->receive function to work for 2 different upload paths depending on the file input name etc $_FILES['input_images'], $_FILES['input_documents'].


$f3->set('UPLOADS','uploads/'); // don't forget to set an Upload directory, and make it writable!

$overwrite = false; // set to true, to overwrite an existing file; Default: false
$slug = true; // rename file to filesystem-friendly version

$files = $web->receive(function($file,$formFieldName){
       
var_dump($file);
       
/* looks like:
          array(5) {
              ["name"] =>     string(19) "csshat_quittung.png"
              ["type"] =>     string(9) "image/png"
              ["tmp_name"] => string(14) "/tmp/php2YS85Q"
              ["error"] =>    int(0)
              ["size"] =>     int(172245)
            }
        */

       
// $file['name'] already contains the slugged name now

       
// maybe you want to check the file size
        if($file['size'] > (2 * 1024 * 1024)) // if bigger than 2 MB
            return false; // this file is not valid, return false will skip moving it

       
// everything went fine, hurray!
        return true; // allows the file to be moved from php tmp dir to your defined upload dir
    },
   
$overwrite,
   
$slug
);
Enter code here...



ikkez

unread,
Nov 11, 2016, 8:41:30 AM11/11/16
to Fat-Free Framework
I think this should already work for both. The callback function is executed for every file. $formFieldName will contain your key input_images on the first call and input_documents on the 2nd (or even multiple times, if these are multiple file uploads.
$files should contain an array of received files, keyed by their path.

DevMan12

unread,
Nov 11, 2016, 8:49:30 AM11/11/16
to f3-fra...@googlegroups.com

thanks ikkez for the fast reply. Yes, for some reason I did not see that formfieldname. Thanks for pointing that out.

DevMan12

unread,
Nov 11, 2016, 9:48:45 AM11/11/16
to Fat-Free Framework
btw, is it possible to change the uploads directory inside the web->receive function?

I tried to set this inside base on the formfieldname the but file is not being uploaded.
if($formfieldname == "key1"){
  $f3->set('UPLOADS',$folder_key1);
} else {
  $f3
->set('UPLOADS',$folder_key2);
}

is there any approach to change directories per formfieldname?




ikkez

unread,
Nov 11, 2016, 12:03:01 PM11/11/16
to Fat-Free Framework
well I've built something like that but I was using ajax uploads and used a different upload route for each type of file, then used the route token beforehand and updated UPLOADS before running web->receive

You could also move the files afterwards.
Reply all
Reply to author
Forward
0 new messages