【问题标题】:Multiple image inputs within one form?一种形式中的多个图像输入?
【发布时间】:2016-06-08 07:11:22
【问题描述】:

我看到的大多数问题都与 ONE 图像输入有关。但是如果你有 2 个或 3 个或更多呢?

我的一个图像输入脚本有效,但我试图在我的表单中输入一个单图像输入和一个多图像输入。我正在测试我的表单上有 2 个单图像输入但不工作。

这是一个单一图像输入的 HTML(假设它在

标签内):
  <input type="hidden" name="size" value="350000"/>
  <input type="file" name="schoollogo"/><br/>

PHP:

  $target = "images/logo/";
  $target = $target . basename( $_FILES['schoollogo']['name']);

  $schoollogo = $_FILES['schoollogo']['name'];

  //Writes the information to the database

  mysql_query("INSERT INTO Colleges (schoollogo) VALUES ('$schoollogo')") or die(mysql_error()) ;

  //Writes the logo to the server
  if (move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) {
      //Tells you if its all ok
      echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
  } else {
      //Gives and error if its not
      echo "Sorry, there was a problem uploading your file.";
  }

对于第二个单图输入,我只是添加了第二个输入代码...

 <input type="hidden" name="size" value="350000"/>
 <input type="file" name="otherimage"/><br/>

然后将变量添加到PHP中:

  $targetotherimage = "images/otherimage/";
  $targetotherimage = $targetotherimage . basename( $_FILES['otherimage']['name']);

  mysql_query("INSERT INTO Colleges (schoollogo,otherimage) VALUES ('$schoollogo','$otherimage')") or die(mysql_error()) ;

  //Writes the logo to the server
  if (move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)) {

但是当将该代码添加到 PHP 中时,该代码不起作用。没有错误消息或任何东西。页面是空白的。

顺便说一句,here is my other question 我正在尝试使用已经工作的单图像上传输入将多图像上传输入添加到我的表单中。

【问题讨论】:

标签: php forms image-uploading multiple-file-upload


【解决方案1】:

if 条件的错误关闭。

  if(move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target)) && (move_uploaded_file($_FILES['otherimage']['tmp_name'], $targetotherimage)) {
                                                                  ^^^^// In your code if condition close here

它被用作

  if(move_uploaded_file($_FILES['schoollogo']['tmp_name'], $target) 

&& (move_uploaded_file($_FILES['otherimage']['tmp_name'],  $targetotherimage)))//if condition close here 
{
// rest of code

}

【讨论】:

  • 我有,但没有任何改变。没有错误。做出您建议的更改后,页面仍然空白。
  • 使用ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);检查页面错误
  • 工作,谢谢!现在我有 2 个单文件上传输入工作,我必须弄清楚如何将第二个输入转换为多文件上传输入。
  • 另一个答案很好,但您的答案帮助直接解决了我的代码,而无需更改整个代码。
【解决方案2】:

这对你有用,用这个替换你的表单

<form method="post" enctype="multipart/form-data" action="upload.php">
      <input type="file" name="file['schoollogo']" >
      <input type="file" name="file['otherimage']" >
      <input type="submit" value="submit">
</form>

您将获得这种格式的文件:

Array
(
    [file] => Array
        (
            [name] => Array
                (
                    ['schoollogo'] => 1465386599_Location_Outline-36.png
                    ['otherimage'] => STONE_Website_v7E.jpg
                )

            [type] => Array
                (
                    ['schoollogo'] => image/png
                    ['otherimage'] => image/jpeg
                )
        ) 
) 

在upload.php文件中

<?php 
if (isset($_FILES['file'])) 
{
     $schoollogo=$_FILES['file']['name']['schoollogo'];
     $target = "images/logo/".basename($schoollogo);;

     $otherimage = $_FILES['file']['name']['otherimage'];
     $targetotherimage = "images/otherimage/".basename($otherimage);

      $tmp_name_school = $_FILES['file']['tmp_name']['schoollogo'];
      $tmp_name_other = $_FILES['file']['tmp_name']['otherimage'];

      if ($tmp_name_school != "" && $tmp_name_other !== "")
      {
        //Upload the file 
        if(move_uploaded_file($tmp_name_school,$target) && move_uploaded_file($tmp_name_other, $targetotherimage)) 
        {
          mysql_query("INSERT INTO Colleges (schoollogo,otherimage) VALUES ('$schoollogo','$otherimage')") or die(mysql_error()) ;
          echo'Done!';
        }
        else
        {
            echo "Not Moved";
        }
      }
}   
?>

【讨论】:

    猜你喜欢
    • 2020-02-25
    • 2015-12-06
    • 1970-01-01
    • 2012-01-05
    • 2015-08-06
    • 1970-01-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多