使用PHP,可以轻松地将文件上传到服务器。
然而,容易带来危险,所以在允许文件上传时一定要小心!
首先,确保 PHP 配置为允许文件上传。
在您的 "php.ini" 文件中,搜索file_uploads
指令,并将其设置为 On:
file_uploads = On
接下来,创建一个 HTML 表单,允许用户选择他们想要上传的图片文件:
<!DOCTYPE html>
<html>
<body>
<form action="upload.html" 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 表单需要遵循的一些规则:
如果没有上述要求,文件上传将无法进行。
其他需要注意的事项:
上面的表单将数据发送到名为 "upload.html" 的文件,我们接下来将创建该文件。
"upload.html" 文件包含上传文件的代码:
<?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脚本解释:
笔记:您需要在 "upload.html" 文件所在的目录中创建一个名为 "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;
}
完整的 "upload.html" 文件现在如下所示:
<?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 文件系统参考。
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!