如何将单个PHP上载图像转换为多个上载图像
我有一个HTML表单+PHP脚本上传一个图像保持原始名称。我想将此脚本从单个图像转换为多个图像,并为每个图像保留原始名称 这是我的HTML代码:如何将单个PHP上载图像转换为多个上载图像,php,html,file-upload,Php,Html,File Upload,我有一个HTML表单+PHP脚本上传一个图像保持原始名称。我想将此脚本从单个图像转换为多个图像,并为每个图像保留原始名称 这是我的HTML代码: <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="userfile"/> <input type="text" name="imgdec"> &
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="userfile"/>
<input type="text" name="imgdec">
<button name="upload" type="submit" value="Submit">
</form>
这是我的PHP代码:
<?php
if(isset($_POST['upload'])) {
$allowed_filetypes = array('.jpg','.jpeg','.png','.gif');
$max_filesize = 10485760;
$upload_path = 'uploads/';
$description = $_POST['imgdesc'];
$filename = $_FILES['userfile']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
if(!in_array($ext,$allowed_filetypes))
die('The file you attempted to upload is not allowed.');
if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
die('The file you attempted to upload is too large.');
if(!is_writable($upload_path))
die('You cannot upload to the specified directory, please CHMOD it to 777.');
if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) {
$query = "INSERT INTO uploads (name, description) VALUES ($filename, $description)";
mysql_query($query);
echo 'Your file upload was successful!';
} else {
echo 'There was an error during the file upload. Please try again.';
}
}
?>
我知道这是一篇老文章,但对于试图上传多个文件的人来说,进一步的解释可能会有用。。。以下是您需要做的:
- 输入名称必须定义为数组,即name=“inputName[]”
- 输入元素必须有multiple=“multiple”或只有multiple
- 在PHP文件中使用以下语法 “$\u文件['inputName']['param'][索引]”
- 确保查找空文件名和路径,数组可能会
包含空字符串
PHP:
//数组中上载文件的计数
$total=计数($_文件['upload']['name']);
//循环浏览每个文件
对于($i=0;$i)
<input name="upload[]" type="file" multiple="multiple" />
// Count # of uploaded files in array
$total = count($_FILES['upload']['name']);
// Loop through each file
for($i=0; $i<$total; $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['upload']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != ""){
//Setup our new file path
$newFilePath = "./uploadFiles/" . $_FILES['upload']['name'][$i];
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $newFilePath)) {
//Handle other code here
}
}
}