PHP 文件上传

创建于 2024-12-03 / 28
字体: [默认] [大] [更大]

通过 PHP,可以把文件上传到服务器。

然而,危险也随之而来,因此在允许文件上传时请务必小心!


配置 "php.ini" 文件

首先,确保 PHP 配置为允许文件上传。

在您的"php.ini"中 文件,搜索 file_uploads 指令,并将其设置为 On:

file_uploads = On

创建一个文件上传表单

接下来,创建一个 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="post"
  • 表单还需要以下属性: enctype="multipart/form-data"。它指定提交表单时要使用的内容类型

如果没有上述要求,文件上载将无法工作。

其他注意事项:

  • <input> 标签的 type="file" 属性规定了应该把输入作为文件来处理。举例来说,当在浏览器中预览时,会看到输入框旁边有一个浏览按钮。

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



创建上传脚本

"upload.php" 文件含有供上传文件的代码:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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;
  }
}
?>

PHP脚本说明:

  • $target_dir = "uploads/" - 指定要放置文件的目录
  • $target_file 指定要上载的文件的路径
  • $uploadOk=1 尚未使用(稍后将使用)
  • $imageFileType 保存文件的文件扩展名(小写)
  • 接下来,检查图像文件是实际图像还是伪图像

注释: 您需要在 "upload.php" 文件所在的目录中创建一个名为 "uploads" 的新目录。上传的文件将保存在那里。


检查文件是否已经存在

现在我们可以添加一些限制。

首先,我们将检查文件是否已经存在于"uploads" 文件夹中。如果是,将显示一条错误消息,$uploadOk 设置为 0:

// Check if file already exists
if (file_exists($target_file)) {
  echo "Sorry, file already exists.";
  $uploadOk = 0;
}

限制文件大小

上面HTML表单中的文件输入字段名为 "fileToUpload"

现在,我们要检查文件的大小。如果文件大于500KB,将显示错误消息,$uploadOk 设置为0:

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
  echo "Sorry, your file is too large.";
  $uploadOk = 0;
}

限制文件类型

下面的代码只允许用户上传JPG、JPEG、PNG和GIF文件。所有其他文件类型在将 $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;
}

完整的上传文件PHP脚本

完整的 "upload.php" 文件现在如下所示:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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 ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
  } else {
    echo "Sorry, there was an error uploading your file.";
  }
}
?>

PHP Filesystem 参考手册

如需完整的 PHP 文件系统参考手册,请访问 W3Schools 提供的 PHP Filesystem 参考手册



0 人点赞过