Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.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_Html_Mysql_Forms_File Upload - Fatal编程技术网

Php 多个图像上载无法仅上载第一个图像

Php 多个图像上载无法仅上载第一个图像,php,html,mysql,forms,file-upload,Php,Html,Mysql,Forms,File Upload,我有一个表单,允许用户上传多个图像,每个图像上限为2MB。提交表单后,图像将存储在服务器上(例如,在本例中是我的计算机) 问题 上载多个文件时,上载的第一个图像始终无法上载到服务器,而其他图像(例如第二个、第三个等)能够成功保存到我的计算机。当仅使用一个图像测试表单时,图像也无法上载 我的结论是,通过表单提交的第一个图像总是无法上传,尽管我不明白为什么会这样,因为我能够成功地回显图像文件的tmp_名称 我的代码(摘录): if(isset($_FILES['upload']['tmp_nam

我有一个表单,允许用户上传多个图像,每个图像上限为2MB。提交表单后,图像将存储在服务器上(例如,在本例中是我的计算机)

问题 上载多个文件时,上载的第一个图像始终无法上载到服务器,而其他图像(例如第二个、第三个等)能够成功保存到我的计算机。当仅使用一个图像测试表单时,图像也无法上载

我的结论是,通过表单提交的第一个图像总是无法上传,尽管我不明白为什么会这样,因为我能够成功地回显图像文件的tmp_名称

我的代码(摘录):

  if(isset($_FILES['upload']['tmp_name']))
 {
$numfile=count($_FILES['upload']['tmp_name']);

    for($i=0;$i<$numfile;$i++)
    {
        if(!empty($_FILES['upload']['tmp_name'][$i]))
        {
            if(is_uploaded_file($_FILES['upload']['tmp_name'][$i]))
            {

                //Conditionals for uploaded file
                $foldername=$_SESSION['UserId'];
                $cat=$_POST['category'];
                $sub=$_POST['subcat'];
                $itemname=$_POST['itemname'];
                $allowed_filetypes=array('.jpg','.gif','.bmp','.png','.JPG');
                $max_filesize = 2097152; // Maximum filesize in BYTES (currently 2.0MB).
                $upload_path = 'C:\Users\Kence\Desktop\UniServer\www\images\\'.$foldername.'\\'.$cat.'\\'.$sub.'\\'.$itemname.'\\'; // The place the files will be uploaded to.

                //Checks if Folder for User exists
                //If not, A folder for the user is created to store the user's images
                if(!is_dir($upload_path))
                {
                    $upload_path=mkdir($upload_path,0644,true);
                }

                $filename = $_FILES['upload']['name'][$i]; // Get the name of the file (including file extension).
                $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename.

                // Check if the filetype is allowed, if not DIE and inform the user.
                if(in_array($ext,$allowed_filetypes))
                {
                    if(filesize($_FILES['upload']['tmp_name'][$i])<$max_filesize)
                    {
                        if(is_writable($upload_path))
                        {echo"$upload_path <br>";
                        print_r($_FILES);
                            if(!move_uploaded_file($_FILES['upload']['tmp_name'][$i],"$upload_path" . $filename))
                            {
                                $errormsg="Upload Failed.Error in moving file";
                            }
                        }
                        else
                        {
                        $errormsg="Image Upload Failed.";
                        }
                    }
                    else
                    {
                        $errormsg="Upload failed.ONly images below 2MB are allowed";
                    }
                }
                else
                {
                    $errormsg="Error. Only JPEG,PNG and BMP files are allowed";
                }

            }
            else
            {
                $errormsg="Error.File not uploaded";
            }
        }
    }

}
else
{
$errormsg="Upload failed";
}
<body onload="getcategory()">
  <form action="<?PHP echo $_SERVER['PHP_SELF'] ?>" name="additem" enctype="multipart/form-data" method="POST">
<table>
<tr>
  <td>Select Category: </td><td>
    <select name="category" id="category" onchange="getsubcategory(this)">
      <option disabled="disabled" value="">Choose a category</option>
    </select>
  </td>
</tr>
<tr>
  <td>Select SubCategory</td>
  <td>
    <select id="subcat" name="subcat">
      <option value=""></option>
    </select>
  </td>
</tr>
<tr>
  <td>Item Name</td>
  <td><input type="text" name="itemname" size="30" maxlength="50" required="required"></td>
</tr>
<tr>
  <td>Item Price</td>
  <td><input type="number" name="itemprice" size="30" min="1" required="required"></td>
</tr>
<tr>
  <td>Item Info</td>
  <td><textarea name="iteminfo" col="40" rows="10" maxlength="300" required="required"></textarea>
</tr>

<tr>
  <td>Filename:</td>
  <td><input type="file" name="upload[]" /></td>
</tr>

<tr>
  <td>Filename:</td>
  <td><input type="file" name="upload[]" /></td>
</tr>

<tr>
  <td>Filename:</td>
  <td><input type="file" name="upload[]" /></td>
</tr>

<tr>
  <td>Filename:</td>
  <td><input type="file" name="upload[]" /></td>
</tr>

<tr>
  <td>Filename:</td>
  <td><input type="file" name="upload[]" /></td>
</tr>
<tr>
 <td colspan="2"><input type="SUBMIT" name="Button" value="Submit"></td>
</tr>
<tr>
  <td colspan="2"><?PHP if(isset($errormsg)){echo"$errormsg";}?></td>
</tr>       
<tr>
  <td colspan="3"><font color="#FF0000"></font></td>            
</tr> 
然而,即使如此,提交到表单的第一个图像也不会被保存(即,如果我只上传一个图像,如果上传失败,没有任何错误,如果我上传多个图像,只有第一个图像不会被上传,而其余的图像会被成功保存。)但是,如果我要刷新页面(从而重新提交相同的表单)然后保存第一个图像

这里的问题是文件路径不可写,导致第一个图像无法保存。然而,让我困惑的是,如果第一个图像的文件路径不可写,为什么后续图像在同一代码上运行时会有有效的文件路径?还有,为什么要刷新页面(从而重新提交表单)神奇地让第一个图像成功保存(这意味着文件路径现在是可写的?)

我的打印结果(美元文件);

Array ( 
    [upload] => Array ( 
        [name] => Array ( 
            [0] => download (1).jpg 
            [1] => download.jpg 
            [2] => 
            [3] => 
            [4] => 
        ) 
        [type] => Array ( 
            [0] => image/jpeg 
            [1] => image/jpeg 
            [2] => 
            [3] => 
            [4] => 
        ) 
        [tmp_name] => Array ( 
            [0] => C:\Users\Kence\Desktop\UniServer\tmp\php474.tmp 
            [1] => C:\Users\Kence\Desktop\UniServer\tmp\php475.tmp 
            [2] => 
            [3] => 
            [4] => 
        ) 
        [error] => Array ( 
            [0] => 0 
            [1] => 0 
            [2] => 4 
            [3] => 4 
            [4] => 4 
        ) 
        [size] => Array ( 
            [0] => 6229 
            [1] => 6984 
            [2] => 0 
            [3] => 0 
            [4] => 0 
        ) 
    ) 
)
有人能指出我做错了什么地方吗?我已经试了3个小时了,但我仍然不知道我做错了什么,这让我发疯了

编辑:已解决


我发现了我的错误

我最初写的

if(!is_dir($upload_path))
{
    $upload_path=mkdir($upload_path,0644,true);
}
if(!is_dir($upload_path))
{
    $upload_path=mkdir($upload_path,0644,true);
}
我把它改成了

if(!is_dir($upload_path))
{
    mkdir($upload_path,0644,true);
}
这解决了我遇到的错误。我犯了一个非常愚蠢的错误。此处的“上载”
$\u FILES['upload']
指表单中文件字段的名称。
仅当名为upload的文件字段中的文件已上载时,您才进行检查


您是否对表单中的第一个上载字段进行了不同的命名?

查看您的转储,并与您的代码进行比较

您的代码访问所有上载字段,即使没有上载文件,例如在字段3、4和5中。因此,您的代码迭代所有这些文件,并在空字符串上使用
realpath()

这将触发错误消息“error.File not upload”。这是正确的,因为文件号3、4和5没有上载,它们被保留为空

你的代码必须处理这个问题


作为一项安全功能,不要使用
realpath()
,使用
is\u uploaded\u file()
。PHP有一个在上传过程中创建的文件名列表,应该只允许在上传脚本中处理这些文件。即使攻击者诱使PHP操纵“tmp\u名称”,您也不想触碰任何其他文件.

发现了我的错误

我最初写的

if(!is_dir($upload_path))
{
    $upload_path=mkdir($upload_path,0644,true);
}
if(!is_dir($upload_path))
{
    $upload_path=mkdir($upload_path,0644,true);
}
我把它改成了

if(!is_dir($upload_path))
{
    mkdir($upload_path,0644,true);
}
这解决了我遇到的错误。
我犯了一个非常愚蠢的错误。

我觉得这可能是因为您的
文件\u exists()
call。您是否尝试过先手动创建目录,然后再发送上传?另外,可能会有所帮助。如果您在开始和结束时清理括号的情况,会有很大帮助。请正确缩进。@Bojangles我尝试过查看提供的链接,并将文件\u更改为realpath,但不起作用。提交时在窗体上,只有第二个图像被上传。只有当我刷新窗体(从而使其上传两次)时,两个图像才会被保存。请添加
print\r($\u文件)
call以回答您的问题。@Sven您好,Sven,我编辑了我的帖子,以包括我的print\r($\u文件)的结果嗨,斯特拉顿,我已经在表单的代码中添加了,表单中的上传字段被正确命名为“上传”。嗨,斯文,谢谢你为我指出这一点,我已经更新了你的答案。我已经通过添加一个if(!empty($\u FILES['upload']['tmp\u name'][$i])来更正它。然而,即使如此,我的原始问题,即我的表单拒绝上载/保存通过它的第一个图像。(即,如果我只上载一个图像,if将失败,没有任何错误,并且如果我上载多个图像,只有第一个图像不会上载,而其余图像将成功保存。)但是,如果我要刷新页面(从而重新提交相同的表单),那么第一个图像将被保存。