douliao8318 2012-12-16 20:35
浏览 9
已采纳

图片上传说它工作但没有文件

Im using a tutorial to upload imageas to my server only I can't seem to get it to actually place the image in my folder on my server?

I have the following...

if (isset($_POST['submit'])) { 

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("/upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "/upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "/upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }

  };

Can anybody see whats wrong? As ive said i receive a success message yet nothing seems to appear in the correct folder...


html

<form action="#" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>



Array
(
[file] => Array
    (
        [name] => Screen shot 2012-12-16 at 8.40.49 PM.png
        [type] => image/png
        [tmp_name] => /tmp/phpVr7glY
        [error] => 0
        [size] => 6289
    )

)
  • 写回答

1条回答 默认 最新

  • drv54591 2012-12-16 21:41
    关注

    This looks like a path-problem to me. Have you tried changing the path to "upload/":

    move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
    

    And ofcourse that the name is upload and not uploads or something like that.

    Relative and absolute paths can be somewhat tricky, and to be honest I don't really recall how for example apache/php handles absolute paths.

    Also this is asuming that you're on Linux and using apache, if you're on windows and using ISS I recall having problems with the temp-folder. Howvever I don't have a solution for you.

    Okay so after looking at this for a while and finally testing it. It seems that calling end on the returnvalue of explode is causing all the problems. So if you change it to something like this it should work:

    $parts = explode(".", $_FILES["file"]["name"]);
    $extension = $parts[sizeof($parts)-1];
    

    The error output was this:

    Strict standards: Only variables should be passed by reference in:

    So look over your error-reporting and try installing a debugger like xdebug.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器