PHP文件上传受挫

时间:2014-03-16 08:53:06

标签: php html

大家早上好

我一直在玩我的HTML表单上设置文件上传功能,并且无法将文件实际上传到我的服务器上。该目录名为/ uploads,位于我的根目录中,其中包含索引文件等。表单有正确的enctype等,PHP不会返回任何错误,我只是无法上传文件。请帮帮我......

我的输入字段的HTML是:

<td valign="top">File Upload</label></td>
 <td valign="top"><input name="file" type="file" /></td>

PHP是:

   $uploads_dir = "http://www.tenancydepositservice.co.uk/uploads";

    if ($_FILES["file"]["error"] == UPLOAD_ERR_OK) { $tmp_name = $_FILES["file"]["name"]; $name = $_FILES["file"]["name"]; move_uploaded_file($tmp_name, "$uploads_dir/$name");}

     $email_message .= "$uploads_dir/$name";

3 个答案:

答案 0 :(得分:0)

你的临时名称应该是错误的

$tmp_name = $_FILES["file"]["tmp_name"]

你有

$tmp_name = $_FILES["file"]["name"];

还要确保表单

enctype="multipart/form-data"

文件保存路径为

$uploads_dir = "path to upload" 

某事

$uploads_dir = "/var/www/project/upload_dir" 

upload_dir具有写入权限。

答案 1 :(得分:0)

我希望这有效:

php part / file:

<?php
if($_POST) {
    $uploads_dir = './uploads';
    $file_name = $_FILES['file']['name'];
    $file_type = $_FILES['file']['type'];
    $allowed_type = array('image/jpg', 'image/jpeg', 'image/png', 'image/gif'); //this is for images but can be changed to any type of file
    $file_tmp = $_FILES['file']['tmp_name'];
    $path = $uploads_dir.$file_name;

    if(isset($file_name) && !empty($file_name)) {
        if(in_array($file_type, $allowed_type)) {
            move_uploaded_file($file_tmp, $path);
            //if you need to store the file in a db use $path in your query
        }
    }
}
?>

答案 2 :(得分:-3)

从w3schools.com复制

   <?php


    if ($_FILES["file"]["error"] > 0)
       {
      echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
      }
      $allowedExts = array("gif", "jpeg", "jpg", "png");    //add your extension here
    $temp = explode(".", $_FILES["file"]["name"]);
    $extension = end($temp);
    if ((($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg")
    || ($_FILES["file"]["type"] == "image/jpg")            //add extension here as well
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/x-png")
    || ($_FILES["file"]["type"] == "image/png"))
    && ($_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"]))     //create a folder 
          {                                                    //named upload where your 
          echo $_FILES["file"]["name"] . " already exists. ";  //all php pages are stored
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
        }
        else
        {
         echo "Invalid file";
        }

   ?>

在代码中查看//