我想使用以下代码将多个图像上传到我的服务器:
<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”。如果上传了两张图片,则第一张图片名为“hello”,第二张图片重命名为“bye”。
我试图寻找解决方案,但我没有成功。我无法理解有什么不对。这些文件被顺序命名为“cow10”数组中的最新值,无论图片从哪个输入上传。
任何帮助或建议?
答案 0 :(得分:0)
有些事情让我很突出。 1.你有一个$ userfile的foreach循环,它实际上不是一个循环。然后,您应该查看错误,并引用$ values
以下是我在上传文件夹中没有文件权限的问题。问题是您没有正确引用$ userfile数组,$ _FILES对象索引实际上是:'01'与$ userfile数组上的01。
<?php
$foldername = "/var/www/vhosts/application/tmp"; // <-- Unused
$userfile = array(
'01' => 'hello',
'02' => 'bye',
'03' => 'likka',
'04' => 'pippa',
'05' => 'laptop',
'06' => 'cow06',
'07' => 'cow07',
'08' => 'cow08',
'09' => 'cow09',
'10' => 'cow10',
);
//Upload Images
$success = 0;
$fail = 0;
$uploads_dir = "temp_images";
$count = 1;
foreach ($_FILES["userfile"]["error"] as $key => $error) {
$file_key = str_replace("'",'',$key);
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."/".$userfile[$file_key].".".$ext;
echo "Copying ".$tmp_name." to ".$newfile;
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++;
}
}
}
?>
提交3张图片时,这是我的输出。
将/ tmp / phpBttoCX复制到temp_images / hello.jpg
将/ tmp / phpFCHlLK复制到temp_images / bye.jpg
将/ tmp / phpi5u51x复制到temp_images / likka.jpg
答案 1 :(得分:0)
我的代码正在运行,您必须根据您的要求进行更改。我上传了2个.jpg文件进行测试,它保存为1.jpg和2张图片.jpg。我没有完成我的代码,因为完成这项工作不是我的任务,而是为了帮助你。下面是文件代码 testing.php (它上传到同一个文件,这是这个表格。我更新了代码。它正在我的电脑上工作。你应该尝试自己解决这些问题。
<?php
$array = array("'01'"=>"1.jpg","'02'"=>"2.jpg");
$i=0;
if(!empty($_FILES["userfile"]["error"])){
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["userfile"]["tmp_name"][$key];
$name = $array[$key];echo $key;
move_uploaded_file($tmp_name, $name);
$i++;
}
}
}
?>
<html>
<body>
<form action="testing.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>