<?
if(isset($_POST['upload'])) {
if(empty($_FILES['image'])) {
echo "<p class=\"error\">Please upload an image...</p>\n";
} else {
$file_name= $_FILES['image']['name']; // original name
$file_type = $_FILES['image']['type'];
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name']; // temporary location on server
$file_error = $_FILES['image']['error']; // reference to future error
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
if($file_ext != "jpeg" || "jpg" || "gif" || "png") {
echo "<p class=\"error\">Please upload an image that has the extension .jpg/jpeg, .gif, or .png.</p>\n";
} else {
if($file_size > (10000000)) {
echo "<p class=\"error\">Please upload an image that does not exceed the file size of 10MB.</p>\n";
} else {
if($file_error) {
echo "<p class=\"error\">Sorry, an unexpected error occurred: ".$file_error."</p>\n";
} else {
$f_rename = "pfp/".time().".png";
move_uploaded_file($file_tmp,$f_rename);
}
}
}
}
}
?>
<!doctype html>
<html>
<head>
<title>Upload</title>
</head>
<body>
<form action="upload_test_image.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit" name="upload" value="Upload"/>
</form>
</body>
</html>
我无法上传任何图片,因为我无法通过文件扩展名检查。在我写这篇文章时,我也试图解决问题,如果它正是扩展验证的问题(可能是因为它只是打印出我发出的错误声明,因为它是第一个,如果...其他声明)。任何人都可以帮助在该代码或此代码中找到问题吗?
答案 0 :(得分:0)
pathinfo()将需要文件的路径而不仅仅是文件名。 要获得扩展,您可以尝试这样做:
$extension = preg_replace('@.+\.@', '', $_FILES['image']['name'])
或者你可以查看$ _FILES ['image'] ['type'],因为它可能包含你正在寻找的类型。 finfo也可能有用: http://php.net/manual/en/function.finfo-file.php
答案 1 :(得分:0)
我认为你不能在PHP中做这样的布尔条件。
如何做,声明一组允许的扩展名:
$extensions = array("jpeg", "jpg", "gif","png");
然后检查文件扩展名是否在数组中:
if(!in_array($file_ext, $extensions)) {
//Please upload an image that has the extension .jpg/jpeg, .gif, or .png.
}