Closed wzapk closed 7 years ago
Oh, I learned that this component awesome!
you can learn tests that have examples who to do that https://github.com/brandonsavage/Upload/blob/master/tests/FileTest.php#L36-L73
@wzapk did u managed to handle multiple file uploads?, im following the example on https://github.com/brandonsavage/Upload/blob/master/README.md, did look at what @williamespindola mention but didnt figure it out how to do it.. thanks in advance guys
I still can't get multiple files upload to work. Any simple example?
This section is an example of multiple files uploads.
// Prepare uploaded files
$_FILES['multiple'] = array(
'name' => array(
'foo.txt',
'bar.txt'
),
'tmp_name' => array(
$this->assetsDirectory . '/foo.txt',
$this->assetsDirectory . '/bar.txt'
),
'error' => array(
UPLOAD_ERR_OK,
UPLOAD_ERR_OK
)
);
$_FILES['single'] = array(
'name' => 'single.txt',
'tmp_name' => $this->assetsDirectory . '/single.txt',
'error' => UPLOAD_ERR_OK
);
$_FILES['bad'] = array(
'name' => 'single.txt',
'tmp_name' => $this->assetsDirectory . '/single.txt',
'error' => UPLOAD_ERR_INI_SIZE
);
...
public function testConstructionWithMultipleFiles()
{
$file = new \Upload\File('multiple', $this->storage);
$this->assertCount(2, $file);
$this->assertEquals('foo.txt', $file[0]->getNameWithExtension());
$this->assertEquals('bar.txt', $file[1]->getNameWithExtension());
}
Searching over the net, was able to figure it out, dont remember at the moment the references.. Hope it helps
$app->post('/upload', 'api_auth', 'user_auth', function () use ($app) {
// check for required params
$dbu = new Users();
$db = new BankQuestions();
$user_email = whoami();
$creator_id = $dbu->getUserId($user_email);
date_default_timezone_set('America/Hermosillo');
$today = date("YmdHis");
$response = array();
if (has(1) || has(8)) {
$storage = new \Upload\Storage\FileSystem('../temp/');
$file = new \Upload\File('upload', $storage);
if (count($file)>=1) {
//Multiple file upload
$data = array();
for ($i = 0; $i < count($file); $i++) {
// Access data about the file that has been uploaded
$tmp = array(
'name' => $file[$i]->getNameWithExtension(),
'extension' => $file[$i]->getExtension(),
'mime' => $file[$i]->getMimetype(),
'size' => $file[$i]->getSize(),
'md5' => $file[$i]->getMd5(),
'dimensions' => $file[$i]->getDimensions()
);
// Rename the file on upload
$new_filename = uniqid();
$file[$i]->setName($today."-".$new_filename."-".$creator_id);
array_push($data, $tmp);
}
// Single File upload, it doesn't support uploading each file reference
try {
// Validate file upload
// MimeType List => http://www.iana.org/assignments/media-types/media-types.xhtml
$file->addValidations(array(
// Ensure file is of type "image/png"
new \Upload\Validation\Mimetype('application/zip'),
//You can also add multi mimetype validation
//new \Upload\Validation\Mimetype(array('image/png', 'image/gif'))
// Ensure file is no larger than 5M (use "B", "K", M", or "G")
new \Upload\Validation\Size('1M')
));
//Trying to save the File
$file->upload();
$response["error"] = false;
$response["file"] = $data;
} catch (\Exception $e) {
//var_dump(__FILE__." ". __LINE__." ". print_r($e, 1));
$errors = $file->getErrors();
$response["error"] = true;
$response["message"] = $errors;
}
}
} else {
$response["error"] = true;
$response["message"] = "User with insufficient privileges to carry out this transaction";
}
// echo json response
echoRespnse(201, $response);
}); `
Thanks for the replies and examples. My problem was: I have a route to a page with a form. When form is submitted, I process the form (file inputs) with an ajax call to another route which in turn uploads the files. My ajax code wasn't processing all the file inputs, just one.
How to make multiple file uploads?