fileupload在php中不起作用
我上传正确的jpg文件,但它显示抱歉,只允许jpg,JPEG,PNG和GIF文件我不知道我做错了什么,我想验证文件大小和文件类型后,移动文件夹上的图像,并更改文件名fileupload在php中不起作用,php,html,Php,Html,我上传正确的jpg文件,但它显示抱歉,只允许jpg,JPEG,PNG和GIF文件我不知道我做错了什么,我想验证文件大小和文件类型后,移动文件夹上的图像,并更改文件名 <?php $target_dir = "original-photo/"; $original_file=md5($_FILES["file"]["name"].time().rand(10,1000)).'.'.$imageFileType; $target_file = $target_dir .$original_f
<?php
$target_dir = "original-photo/";
$original_file=md5($_FILES["file"]["name"].time().rand(10,1000)).'.'.$imageFileType;
$target_file = $target_dir .$original_file;
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["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["file"]["size"] > 5000000) {
// echo "Sorry, your file is too large.";
header("Location:profilepic.php?filesize=error");
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
header("Location:profilepic.php?filetype=error");
//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["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
更换
//Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType!="jpeg" && $imageFileType != "gif" ) {
header("Location:profilepic.php?filetype=error");
//echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
用这个
$allowTheseImageTypes=array("image/gif", "image/pjpeg", "image/jpeg", "image/png", "image/x-png");
//test the image type here
if (!in_array($_FILES['file']['type'], $allowTheseImageTypes)){
header("Location:profilepic.php?filetype=error");
//echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
试试这个:
// Allow certain file formats
$arrayTypeAllowed = array("image/jpg", "image/jpeg", "image/pjpeg", "image/png");
$imageFileType = $_FILES["file"]["type"]
改变这个
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" )
用这个
if (!in_array($imageFileType,arrayTypeAllowed)) {
//echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0
}
您正在对整个文件执行md5。试试下面的代码可能会有帮助
<?php
$target_dir = "original-photo/";
$image_path = $target_dir . basename($_FILES["file"]["name"]);
$target_file = $image_path['filename'].'_'.time().'.'.$image_path['extension']
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["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["file"]["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["file"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
如果只需要文件扩展名,请使用 反而
$imageFileType=pathinfo($target\u文件,pathinfo\u扩展名)代码>
这:
$extension=trim(substr(strrchr($filename,“.”),1))代码>
这比使用pathinfo()并从数组中获取值快很多倍。所以我只需将扩展名改为.jpg就可以上传位图了?伟大的您告诉pathinfo在验证后查找要移动到的新文件,但由于验证未完成,该文件在新位置不存在。您需要使用$\u文件的临时路径。