2016-09-22 89 views
1

我有下面的代碼,但即使當我上傳一個有效的圖像時,它會迴應「錯誤:您的文件無法上傳」。它通過了所有檢查,但保存到文件夾時出現問題。我在與php文件相同的目錄中有一個名爲'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"; 
    } 
} 
?> 

回答

2

您的解決方案是在這裏(它包含錯誤),

  1. 首先在Upload.html變化/文件上傳.php到fileupload.php。
  2. 在upload.html & fileUpload.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="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"; 
    } 
} 
?> 
+0

pa中的所有目錄th使用了對所有 – Brittany

+0

@Brittany設置的讀寫權限,請嘗試更新的解決方案。 –

+0

嘿,對不起,我不小心上傳了稍微老一點的版本到這個問題。我其實使用「fileUpload.php」,而不是「/fileUpload.php」。但這是在php文件中的小錯字,非常感謝! – Brittany

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 } 
+0

與文件類型及其驗證工作有關的所有事情,純粹是將文件上傳到目錄 – Brittany

0

試試這個$target_dir = "./uploads/";

也如果你在一個wamp環境下使用windows pc授予文件夾權限