Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP上传并根据输入文件数组重命名上传的文件名_Php_Arrays_File Upload - Fatal编程技术网

PHP上传并根据输入文件数组重命名上传的文件名

PHP上传并根据输入文件数组重命名上传的文件名,php,arrays,file-upload,Php,Arrays,File Upload,我想使用以下代码将多个图像上载到我的服务器: <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="userfi

我想使用以下代码将多个图像上载到我的服务器:

<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++;
        }
    }
}

?>

有几件事对我来说很突出。1.$userfile有一个foreach循环,实际上它不是一个循环。然后,您应该查看错误并引用$value

下面是我在上传文件夹中的文件权限方面遇到的一些问题。问题是您没有正确引用$userfile数组,$\u 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++;
        }
    }
}
?>

我的代码正在运行,您必须根据需要进行更改。我上传了2.jpg文件进行测试,并将其保存为1.jpg和2 pictures.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>

有几件事对我来说很突出。1.你对$userfile有一个foreach循环,但实际上它不是一个循环。然后你应该检查错误并引用$valuesCan你可以用你的解决方案安排我的代码吗?嗨,谢谢你的帮助。当我尝试时,所有上传的文件名都被称为“hello.gif”或“hello.jpg”。Furt我希望如果用户选择从第四次输入上传它,它被称为-pippa,如果从第七次输入“cow”07。谢谢你的帮助,但它没有起作用。如果你从第二次输入上传图像,即picture_02,它被重命名为1.jpg而不是2.jpgNow,这是根据你的需要。希望它能帮助你。注意,它是
$array=数组(“'01'=>”1.jpg“,“'02'=>”2.jpg”);
ByeI收到以下错误HP警告:移动上传的文件():第8行中的文件名不能为空。您必须自己解决此类问题。我的数组仅包含2个值,我认为您正在上载2个以上的索引。请填充完整数组并重试。我的代码是帮助您的示例,而不是工作代码。我仅使用一个值进行了尝试…我认为问题可能出现在$tmp\u name=$\u文件中[“用户文件”][“tmp_名称”][$key];