[英]Uploading Multiple Images With An Input File Field
I'm building a simple website where I want to upload images into a folder so I'm doing this by enabling multiple
in the input box. 我正在建立一个简单的网站,我想在其中将图像上传到一个文件夹中,因此我要通过在输入框中启用
multiple
来做到这一点。
Here is my HTML: 这是我的HTML:
<input type="file" name="gallery[]" multiple />
And here is the PHP: 这是PHP:
$id = time();
$year = date("Y");
$photo_path = "photos/$year/";
$zip_path = "sets/$year/";
$photo = $_FILES["photo"]["name"];
$gallery = $_FILES["gallery"]["name"];
move_uploaded_file($_FILES["photo"]["tmp_name"],"$photo_path" . $id . ".jpg"); // This Adds Photo To $photo_path
move_uploaded_file($_FILES["gallery"]["tmp_name"],"$zip_path"); // This Doesn't Add 5 Files To $zip_path
This adds the photo into the $photo_path
folder, but doesn't add the selected 5 images to the $zip_path
folder. 这会将照片添加到
$photo_path
文件夹中,但不会将所选的5张图像添加到$zip_path
文件夹中。
Any ideas where I'm going wrong? 有什么想法我要去哪里吗?
Update: 更新:
foreach ($_FILES["gallery"] as $file) {
move_uploaded_file($_FILES["gallery"]["tmp_name"],"$zip_path");
}
You need to handle multiple files in a loop. 您需要循环处理多个文件。 Here is an example directly from the manual .
这是直接来自手册的示例。
$uploads_dir = '/uploads';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
When you calling multiple file upload, it will return an array of files. 当您调用多个文件上传时,它将返回一个文件数组。 you can iterate it like:
您可以像这样迭代它:
if(count($_FILES['gallery'])) {
foreach ($_FILES['gallery'] as $key=>$file) {
//do your upload stuff here
move_uploaded_file($_FILES['gallery']["tmp_name"][$key], $zip_path . time() . ".jpg");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.