fileupload在php中不起作用

时间:2015-12-03 11:30:13

标签: php html5

我上传了正确的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_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.";
    }
    }
    ?>

4 个答案:

答案 0 :(得分: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;
}

有了这个......

$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;
}

答案 1 :(得分: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
}

答案 2 :(得分: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.";
    }
}
?>

答案 3 :(得分:0)

如果您只想要文件扩展名,请使用

代替

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

这样:

$extension = trim(substr(strrchr($filename, "."), 1));

这比使用pathinfo()并从数组中获取值要快许多倍。