上传图片在php中失败

时间:2015-05-18 15:30:14

标签: php image upload

我正在尝试使用php上传文件。但是,即使只有两条路径,我也没有收到echo的反馈,原因有些奇怪。

<?php
   $allowedExts = array("jpg");
      $temp = explode(".", $_FILES["file"]["name"]);
      $extension = end($temp);
      if (
      ($_FILES["file"]["type"] == "image/jpg")
      && ($_FILES["file"]["size"] < 100000)
      && in_array($extension, $allowedExts))
        {
        echo "Storing";

$temp = explode(".",$_FILES["file"]["name"]);
$newfilename = "yo.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" . $newfilename;

echo "Stored";


        }
      else
        {
        echo "Invalid file";
        }
        ?>

1 个答案:

答案 0 :(得分:0)

我最终在3小时后搞清楚了。

$target_dir = "upload/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $uploadOk = 1;
    } else {
        $uploadOk = 0;
    }
}
 if ($_FILES["fileToUpload"]["size"] > 5000000) {
    $uploadOk = 2;
}
if($imageFileType != "jpg"  && $imageFileType != "jpeg") {
    $uploadOk = 0;
}
if ($uploadOk == 0) {
//file is not the proper image format  
} 
else if ($uploadOk == 2) {
//too big   
} 
else {       
$target_file = $target_dir . 'goofy' .'.jpg';  
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
}