【发布时间】:2017-11-28 07:43:32
【问题描述】:
我想知道如何更改它以上传任何类型的文件(不仅仅是图像文件)。我正在我的测试项目中使用这些代码集。
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>
<?php
if ($_POST['submit']) {
$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
$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;
}
//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.";
}
}
}
?>
任何人都可以帮助我,我尝试删除检查是否实际图像,但它没有帮助我。
【问题讨论】:
-
只是提醒一下。这是一个安全风险!..木马和其他有害文件也可以轻松上传
-
谢谢先生,我会在上传 pdf 文件后添加一些安全措施。谢谢
-
。很明显你没有写上面的代码/理解。如果您可以编写代码为文件上传添加限制,那么您应该能够允许任何类型的文件
-
是的,先生,我在网上找到的,
标签: php file-upload upload