【问题标题】:PHP File Upload Does NothingPHP文件上传什么都不做
【发布时间】:2013-09-24 12:04:06
【问题描述】:

我之前有几个上传表单工作过,但是,即使在几乎复制了我以前的代码之后,这似乎也不起作用,我更喜欢在一个 php 脚本文件中完成所有这些工作,所以它都是在这个单一的文件。

我的表格:

<form action="" method="post" enctype="multipart/form-data">
    <ul>
        <li>
            <label for="file">File : </label>
            <input type="file" id="file" name="file" required="required" />
        </li>
        <li>
            <input type="submit" value="Upload" />
        </li>
    </ul>
</form>

我的php上传:

if(!empty($_POST['file']))
{
    echo "Found.";
    $exts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_FILES["file"]["name"]);
    $ext = end($temp);
    if((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_FILES["file"]["size"] < 20000)
    && in_array($ext, $exts))
    {
        if($_FILES["file"]["error"] > 0)
        {
            $result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else
        {
            $scandir = scandir("/images/news/");
            $newname = (count($scandir-2)) . $ext;
            move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
            $ulink = "/images/news/" . $newname;
            $result = "Success, please copy your link below";
        }
    }
    else
    {
        $result = "Error.";
    }
}

当我上传 .png 图像时,页面似乎只是刷新了,我已将 echo "Found."; 放在那里以检查它是否在 $_POST["file"] 中有任何内容,但它似乎没有任何内容。

我不明白为什么页面没有正确提交。我已将 action="" 更改为 action="upload.php" 以确保它指向同一页面但仍然没有。

【问题讨论】:

    标签: php forms file upload


    【解决方案1】:

    使用$_FILES['file'] 而不是$_POST['file']

    http://www.php.net/manual/en/features.file-upload.post-method.php了解更多关于$_FILES的信息

    【讨论】:

    • 我不敢相信它这么简单!谢谢你,我应该知道这一点。
    • 发生在我们最好的人身上。只需将此问题标记为已回答即可。
    • 网站允许我在 9 分钟内完成。
    【解决方案2】:

    $_POST['file'] 替换为$_FILES['file'] 并设置action=""

    【讨论】:

      【解决方案3】:

      试试这个.. 因为 $_POST 不适用于文件,对于文件我们使用 $_FILES..

      if(!empty($_FILES['file']))
      {
          echo "Found.";
          $exts = array("gif", "jpeg", "jpg", "png");
          $temp = explode(".", $_FILES["file"]["name"]);
          $ext = end($temp);
          if((($_FILES["file"]["type"] == "image/gif")
          || ($_FILES["file"]["type"] == "image/jpeg")
          || ($_FILES["file"]["type"] == "image/jpg")
          || ($_FILES["file"]["type"] == "image/pjpeg")
          || ($_FILES["file"]["type"] == "image/x-png")
          || ($_FILES["file"]["type"] == "image/png"))
          && ($_FILES["file"]["size"] < 20000)
          && in_array($ext, $exts))
          {
              if($_FILES["file"]["error"] > 0)
              {
                  $result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
              }
              else
              {
                  $scandir = scandir("/images/news/");
                  $newname = (count($scandir-2)) . $ext;
                  move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
                  $ulink = "/images/news/" . $newname;
                  $result = "Success, please copy your link below";
              }
          }
          else
          {
              $result = "Error.";
          }
      }
      

      【讨论】:

        【解决方案4】:

        我不会只检查 $_FILES 变量。我会命名提交输入并检查提交输入是否已提交。这样,您可以检查是否在未选择文件的情况下按下按钮并提示用户。

        像这样:

        <form action="" method="post" enctype="multipart/form-data">
            <ul>
                <li>
                    <label for="file">File : </label>
                    <input type="file" id="file" name="file" required="required" />
                </li>
                <li>
                    <input type="submit" value="Upload" name="upload"/>
                </li>
            </ul>
        </form>
        

        然后您可以检查该值的 post 变量。

        像这样:

        if(!empty($_POST['upload']))
        {
            echo "Found.";
            $exts = array("gif", "jpeg", "jpg", "png");
            $temp = explode(".", $_FILES["file"]["name"]);
            $ext = end($temp);
            if((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/jpg")
                || ($_FILES["file"]["type"] == "image/pjpeg")
                || ($_FILES["file"]["type"] == "image/x-png")
                || ($_FILES["file"]["type"] == "image/png"))
                && ($_FILES["file"]["size"] < 20000)
                && in_array($ext, $exts))
            {
                if($_FILES["file"]["error"] > 0)
                {
                    $result = "Error Code: " . $_FILES["file"]["error"] . "<br />";
                }
                else
                {
                    $scandir = scandir("/images/news/");
                    $newname = (count($scandir-2)) . $ext;
                    move_uploaded_file($_FILES["file"]["tmp_name"],"/images/news/" . $newname);
                    $ulink = "/images/news/" . $newname;
                    $result = "Success, please copy your link below";
                }
            }
            else
            {
                $result = "Error.";
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-12
          • 2012-07-21
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          相关资源
          最近更新 更多