【发布时间】:2015-08-29 09:38:14
【问题描述】:
我正在使用此 php 代码将图像上传到文件夹,但我也想允许上传 pdf 文件,所以我修改了一些代码:
<?php
$target_dir = "extra_images/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$textFileType = 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 "<div class=\"alert alert-success\" role=\"alert\"><strong><span class=\"glyphicon glyphicon-ok\" aria-hidden=\"true\"></span> Correct image type.</strong></div>";
$uploadOk = 1;
} else {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File is not an image.</strong></div>";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>File already exists.</strong></div>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 3750000) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Your file is too large.</strong></div>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" && $textFileType != "pdf" ) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>Only jpg, jpeg, png, gif and pdf (for the Plan Article) files are allowed.</strong></div>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "<div class=\"alert alert-danger\" role=\"alert\"><strong>The file was not uploaded.</strong></div>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "<div class=\"alert alert-success\" role=\"alert\">The file <strong>". basename( $_FILES["fileToUpload"]["name"]). "</strong> has been uploaded.</div><br>Please copy this filename: <span class=\"form-inline\"><input type=\"text\" value=\"". basename( $_FILES["fileToUpload"]["name"]). "\" class=\"form-control input-sm\" style=\"width:220px;\" /></span> And paste it in an empty Extra image field above and save the form.";
} else {
echo "<div class=\"alert alert-danger\" role=\"alert\">There was an error uploading your file.</div>";
}
}
echo "</br></br><p><button class=\"btn btn-default pull-right\" style=\"margin-right:5px;\" type=\"submit\" onclick=\"javascript:history.go(-1)\"><span class=\"glyphicon glyphicon-step-backward\" aria-hidden=\"true\"></span> Back</button></p>";
?>
我添加了这一点:
&& $textFileType != "pdf" and this: $textFileType = pathinfo($target_file,PATHINFO_EXTENSION);
但我所做的这些更改不起作用,它仍然返回 "this is not an image" 消息。
代码的哪一部分标识了文件类型? $imageFileType 是 php 用来识别文件类型的特殊变量吗?
我真的很困惑。有人可以帮忙吗?
【问题讨论】:
-
问题是,它是否在您添加新行之前就抛出了该错误?
-
这里的问题是,您需要对整行使用 OR
||运算符,而不是 AND&&运算符。您是在告诉 PHP 检查上传的文件是否为 JPG AND PNG AND GIF AND PDF。 -
不,脚本工作者适用于图像类型文件。我在这里得到它:w3schools.com/php/php_file_upload.asp
-
您在下面得到了答案。问他们
-
并让那个人知道它是否有效,在答案下。