note 94597 added to function.move-uploaded-file

From: Date: Fri, 13 Nov 2009 12:50:45 +0000
Subject: note 94597 added to function.move-uploaded-file
Groups: php.notes 
Request: Send a blank email to php-notes+get-162864@lists.php.net to get a copy of this message
This function can upload many files whitout parameters in the declaration. If one of them can not be uploaded, the function returns false and deletes all files that have been sent <?php function uploadFiles() { $num_args = func_num_args(); $arg_list = func_get_args(); $valReturn = false; $i = 0; $unlinkElement = array(); foreach($arg_list as $key=>$value) { if(is_array($value) AND is_array($value[0])) { if($value[0]['error'] == 0 AND isset($value[1])) { if($value[0]['size'] > 0 AND $value[0]['size'] < 500000) { $typeAccepted = array("image/jpeg", "image/gif", "image/png"); if(in_array($value[0]['type'],$typeAccepted)) { $destination = $value[1]; if(isset($value[2])) { $extension = substr($value[0]['name'] , strrpos($value[0]['name'] , '.') +1); $destination .= (str_replace(" ","-",$value[2])).".".$extension; } else { $destination .= $value[0]['name']; } if(move_uploaded_file($value[0]['tmp_name'],$destination)) { $i++; $unlinkElement[] = $destination; } } } } } } if($i == $num_args) { $valReturn = true; } else { foreach($unlinkElement as $value) { unlink($value); } } return $valReturn; } ?> To use this function you must specify an array with min two parameters like that. NAME is an optional parameter ! uploadFiles(array("FILES","DESTINATION","NAME")); <?php $file_one = array($_FILES['file_one'],$destination,$name_one); $file_two = array($_FILES['file_two'],$destination); //The name will be the same that $_FILES[]['name'] if(uploadFiles($file_one,$file_two)) { echo "true"; } else { echo "false"; } ?> ---- Server IP: 89.31.146.176 Probable Submitter: 78.192.2.234 ---- Manual Page -- http://www.php.net/manual/en/function.move-uploaded-file.php Edit -- https://master.php.net/note/edit/94597 Del: integrated -- https://master.php.net/note/delete/94597/integrated Del: useless -- https://master.php.net/note/delete/94597/useless Del: bad code -- https://master.php.net/note/delete/94597/bad+code Del: spam -- https://master.php.net/note/delete/94597/spam Del: non-english -- https://master.php.net/note/delete/94597/non-english Del: in docs -- https://master.php.net/note/delete/94597/in+docs Del: other reasons-- https://master.php.net/note/delete/94597 Reject -- https://master.php.net/note/reject/94597 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#162864) next »