如何使用PHP上传文件

时间:2014-12-13 11:42:58

标签: php file upload

我是PHP的新手。从“上载”对话框中选择文件后,以下代码应显示“确定”。但令我惊讶的是屏幕上没有输出。请帮助!!!

  <?php
       if(isset($_FILES['file']['name']))
               $name = $_FILES['file']['name'];

       if(isset($_FILES['file']['tmp_name']))
               $tmp_name = $_FILES['file']['tmp_name'];

       if(isset($name))
       {
              if(!empty($name))
                  echo 'OK';
              else
                  echo 'Please chose a file';
       }

  ?>

   <form action="up.php" method="POST" encrypt="multipart/form-data">
          <input type="file" name="file"><br><br>
          <input type="submit" value="Submit">
   </form>

1 个答案:

答案 0 :(得分:1)

使用PHP,可以轻松地将文件上传到服务器。我试图涵盖所有事情并使其尽可能简单。

在你的&#34; php.ini&#34; file,搜索file_uploads指令,并将其设置为On:

file_uploads = On 

如果您尝试上传的文件大于允许的文件,您可能还需要更改php.ini文件中允许的文件大小:

post_max_size = 8M; //make larger if needed
upload_max_filesize = 8M; //make larger if needed

进行这些更改后,请务必重新启动您的网络服务器(apache / nginx。)

  • upload_max_filesize绝不应大于post_max_size

创建一个允许用户选择要上传的图像文件的HTML表单:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html> 

上面的HTML表单需遵循以下规则:

  • 确保表单使用method =&#34; post&#34;

  • 表单还需要以下属性:enctype =&#34; multipart / form-data&#34;。它指定在提交表单时使用的内容类型

如果没有上述要求,文件上传将无效。

其他需要注意的事项:

  • type =&#34; file&#34;标签的属性将输入字段显示为文件选择控件,使用&#34;浏览&#34;输入控件旁边的按钮

上面的表单将数据发送到名为&#34; upload.php&#34;的文件,我们将在下一步创建。

&#34; upload.php&#34; file包含上传文件的代码:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

一些变量解释:

$target_dir = "uploads/" - specifies the directory where the file is going to be placed
$target_file specifies the path of the file to be uploaded
$uploadOk=1 is used as a flag
$imageFileType holds the file extension of the file