意外的输出文件上传PHP

时间:2016-09-22 04:07:44

标签: php html file-upload

我有以下代码,但即使我上传了有效的图片,它也会回复"错误:您的文件无法上传"。它通过了所有检查,但保存到文件夹出错了。我有一个名为' uploads'的目录。在与php文件相同的目录中,所以我不确定它为什么不在那里保存图像。我知道我可以用数据库来做,但我不太清楚如何。我只是希望能够在某处保存上传的图像,并在页面上显示最近上传的图像。

有关如何使其发挥作用的任何提示都将非常感激。 谢谢!

upload.html

 <!DOCTYPE html>
    <html>
    <head>
        <link rel="shortcut icon" href="">
        <link rel="stylesheet" type="text/css" href="css/style.css">
        <title>File Upload</title>
    </head>
    <body>
        <header>
            <h1>File Upload</h1>
        </header>
        <form action="/fileUpload.php" method="post" enctype="multipart/form-data">
            <br>
            Select image to upload: 
            <input type="file" name="fileToUpload" id="fileToUpload">

            <input type="submit" value="Upload Image" name="submit">
        </form>    
    </body>
    </html>

fileUpload.php

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileUpload"]["name"]);
$uploadCheck = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileUpload"]["tmp_name"]);
    if($check !== false) {
        $uploadCheck = 1;
    } else {
        echo "Error: the file you attempted to upload is not an image";
        $uploadCheck = 0;
    }
}

if (file_exists($target_file)) {
    echo "Error: file already exists.";
    $uploadCheck = 0;
}

if ($_FILES["fileUpload"]["size"] > 500000) {
    echo "Error: file is too large.";
    $uploadCheck = 0;
}

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Error: incompatible type. only JPG, JPEG, PNG & GIF files.";
    $uploadCheck = 0;
}

if ($uploadCheck == 0) {
    echo "Error: your file was not uploaded.";

} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "Success! Your file - ". basename( $_FILES["fileToUpload"]["name"]). " - has been uploaded.";
    } else {
        echo "Error: your file could not be uploaded";
    }
}
?>

3 个答案:

答案 0 :(得分:2)

您的解决方案在这里(它包含错误),

  1. 首先在Upload.html中将/fileUpload.php更改为fileupload.php。
  2. 在upload.html&amp; fileUpload.php,file的name属性应该相同。
  3. upload.html

     <!DOCTYPE html>
         <html>
         <head>
             <link rel="shortcut icon" href="">
             <link rel="stylesheet" type="text/css" href="css/style.css">
             <title>File Upload</title>
         </head>
         <body>
             <header>
                 <h1>File Upload</h1>
             </header>
             <form action="fileUpload.php" method="post" enctype="multipart/form-data">
                 <br>
                 Select image to upload: 
                 <input type="file" name="fileUpload" id="fileUpload">
    
                 <input type="submit" value="Upload Image" name="submit">
             </form>    
         </body>
         </html>
    

    fileUpload.php

     <?php
     $target_dir = "uploads/";
     $target_file = $target_dir . basename($_FILES["fileUpload"]["name"]);
     $uploadCheck = 1;
     $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    
     if(isset($_POST["submit"])) {
         $check = getimagesize($_FILES["fileUpload"]["tmp_name"]);
         if($check !== false) {
             $uploadCheck = 1;
         } else {
             echo "Error: the file you attempted to upload is not an image";
             $uploadCheck = 0;
         }
     }
    
     if (file_exists($target_file)) {
         echo "Error: file already exists.";
         $uploadCheck = 0;
     }
    
     if ($_FILES["fileUpload"]["size"] > 500000) {
         echo "Error: file is too large.";
         $uploadCheck = 0;
     }
    
     if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
     && $imageFileType != "gif" ) {
         echo "Error: incompatible type. only JPG, JPEG, PNG & GIF files.";
         $uploadCheck = 0;
     }
    
     if ($uploadCheck == 0) {
         echo "Error: your file was not uploaded.";
    
     } else {
         if (move_uploaded_file($_FILES["fileUpload"]["tmp_name"], $target_file)) {
             echo "Success! Your file - ". basename( $_FILES["fileUpload"]["name"]). " - has been uploaded.";
         } else {
             echo "Error: your file could not be uploaded";
         }
     }
     ?>
    

答案 1 :(得分:0)

而不是使用它:

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

尝试使用:

list($name,$ext)=explode(".",$_FILES["fileUpload"]["name"]);

然后尝试验证扩展部分:

if($ext!= "jpg" && $ext!= "png" && $ext!= "jpeg"&& $ext!= "gif" )

我建议您使用in_array()进行上述验证,如下所示:

$img_ext=array("jpg","png","jpeg","gif");
if(!in_array($ext,$img_ext)){ //Your code here }

答案 2 :(得分:0)

试试这个-i

如果你在wamp环境下使用windows pc授予文件夹权限