我想使用以下代码将多个图像上传到我的服务器:
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input id="picture_01" name="userfile['01']" tabindex="auto" type="file">
<input id="picture_02" name="userfile['02']" tabindex="auto" type="file">
<input id="picture_03" name="userfile['03']" tabindex="auto" type="file">
<input id="picture_04" name="userfile['04']" tabindex="auto" type="file">
<input id="picture_05" name="userfile['05']" tabindex="auto" type="file">
<input id="picture_06" name="userfile['06']" tabindex="auto" type="file">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
和upload_file.php
<?php
$foldername = "anotherfolder";
$userfile = array(
'01' => 'hello',
'02' => 'bye',
'03' => 'likka',
'04' => 'pippa',
'05' => 'laptop',
'06' => 'cow06',
'07' => 'cow07',
'08' => 'cow08',
'09' => 'cow09',
'10' => 'cow10',
);
echo $userfile ['01'];
foreach ($userfile as $keys => $values);
//Upload Images
$success = 0;
$fail = 0;
$uploads_dir = "temp_images";
$count = 1;
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["userfile"]["tmp_name"][$key];
$name = $_FILES["userfile"]["name"][$key];
$uploadfile = "$uploads_dir/$name";
$ext = strtolower(substr($uploadfile,strlen($uploadfile)-3,3));
if (preg_match("/(jpg|gif|png|bmp|jpeg)/",$ext)){
$newfile = "$uploads_dir/"."$values".".".$ext;
if(move_uploaded_file($tmp_name, $newfile)){
}else{
echo "Couldn't move file: Error Uploading the file. Retry after sometime.\n";
}
}else{
echo "Invalid Extension.\n";
$fail++;
}
}
}
?>
我希望当有人在第一个输入中上传第一张图片时,它被重命名为“你好”,如果它在第二个输入中上传,它被重命名为“再见”。如果上传了两张图片,第一张图片命名为“hello”,第二张图片重命名为“bye”。
我试图寻找解决方案,但没有成功。我不明白出了什么问题。无论图片是从哪个输入上传的,这些文件都按数组中的最新值顺序命名为“cow10”。
有什么帮助或建议吗?