doudouchan5830 2015-07-07 19:25
浏览 46

使用php进行简单文件上传无效

I Have this code:

$filename=$_FILES["fileToUpload"]["name"];
$extension=explode(".", $filename);
$newfilename=$rezeptID .".".$extension;


      if ($_FILES["fileToUpload"]["error"] > 0)
        {
        echo "Return Code: " . $_FILES["fileToUpload"]["error"] . "<br />";
        }
      else
        {
        echo "Upload: " . $_FILES["fileToUpload"]["name"] . "<br />";
        echo "Type: " . $_FILES["fileToUpload"]["type"] . "<br />";
        echo "Size: " . ($_FILES["fileToUpload"]["size"] / 1024) . " Kb<br />";
        echo "Temp file: " . $_FILES["fileToUpload"]["tmp_name"] . "<br />";

        if (file_exists("/Images/Rezepte/" . $_FILES["fileToUpload"]["name"]))
          {
          echo $_FILES["fileToUpload"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($filename,
          "/Images/Rezepte/" . $newfilename);
          echo "Stored in: " . "/Images/Rezepte/" .$newfilename;
          }
        }

The variable $rezeptinfo is just a number and is correct, if you wonder.

When I now submit my form, I get this info about the file:

"Upload: Type: Size: 0 Kb Temp file: Stored in: /Images/Rezepte/10."

Why are the extension of the file and all the basic information mssing. 10 is the value of $rezeptID. What am i doing wrong?

  • 写回答

1条回答 默认 最新

  • donglao4370 2015-07-07 20:16
    关注

    first, your way of getting the extension returns an array and so cant be concatenated to a string, you need to specify the index of the array where the extension is which is 1 :

    $newfilename=$rezeptID .".".$extension[1];
    

    or

    $newfilename=$rezeptID .".".end($extension);
    

    or

    $extension = pathinfo($filename, PATHINFO_EXTENSION);
    

    also, u should the move the uploaded file from the temp_file path

    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],"Images/" .$newfilename);
    

    You should also ensure that enctype="multipart/form-data" is included in your html form property

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大