How to change filename in file upload using Web::instance()?

702 views
Skip to first unread message

Michael Miquel

unread,
Dec 13, 2013, 4:12:23 AM12/13/13
to f3-fra...@googlegroups.com
How to change filename in file upload using Web::instance()? $file['name'] = "newfilename"; doesn't work.


$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){ 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 );

Michael Miquel

unread,
Dec 13, 2013, 6:18:58 AM12/13/13
to f3-fra...@googlegroups.com
I got an error:

Parameter 1 to {closure}() expected to be a reference, value given

code:
$files = $web->receive(function(&$file){
[...]
$file['name'] = "newfilename";

(PHP 5.3)


On Friday, December 13, 2013 11:34:22 AM UTC+1, bcosca wrote:
function(&$file) {}

Message has been deleted

bcosca

unread,
Dec 13, 2013, 8:59:27 AM12/13/13
to f3-fra...@googlegroups.com
Since we can't work around PHP's call_user_func* limitation on passing arguments by reference, the dev branch code now allows you to specify a user-defined function in place of $slug, so you can name the file whatever you want.

J Francisco Herrera Roldán

unread,
May 27, 2014, 8:11:23 PM5/27/14
to f3-fra...@googlegroups.com
So, how I can retrieve a var in the slug scope, like the extension of the file or the type of the file.
$web->receive(function($file){
            var_dump($file); //I can see this
            $_SESSION['tmp_name']=$file['name'];
           
            if($file['size'] > (4 * 1024 * 1024)) // if bigger than 2 MB
                return false;
            },
                true, // overwrite
                function(){
                    list($txt, $ext) = explode(".", $file['name']);
                    return time().$ext;}
            );

ikkez

unread,
May 28, 2014, 2:10:37 AM5/28/14
to f3-fra...@googlegroups.com
did you try     function($file){ ??

SoftWord (Giancarlo Voci)

unread,
May 28, 2014, 3:41:36 AM5/28/14
to f3-fra...@googlegroups.com

bcosca

unread,
Dec 13, 2013, 5:34:22 AM12/13/13
to f3-fra...@googlegroups.com
function(&$file) {}

Reply all
Reply to author
Forward
0 new messages