2012-10-21 249 views
0

我的任務需要我讓用戶將.gif文件擴展名上傳到文件夾,並在上傳後顯示它們。PHP。上傳.gif文件到服務器

我得到了表格和處理頁面,但我似乎無法讓他們工作。我將目錄上的權限設置爲rwxrwxrwx來測試它。

上傳文件代碼:

<html> 
    <head> 
<title>Assignment 7 Part II -- Art Gallery</title> 
    </head> 
    <body> 
    <form method="post" action="a7a2.php" enctype="multipart/form-data"> 
    <input type="hidden" name="MAX_FILE_SIZE" value="50000"> 
    Your file: <input type="file" name="uploadFile" /><br /> 
    <input type="submit" value="Upload it"> 
    </form> 
</body> 
</html> 

代碼:

<html> 
     <head> 
     <title>Assignment 7 Part II -- Art Gallery Results</title> 
     </head> 
     <body> 

     <?php 
     $uploadDir = 'images/'; 
     $uploadFileDir = $uploadDir . basename($_FILES['uploadFile']['name']);             
     if(isset($_FILES['uploadFile'])) { 
      if($_FILES['uploadFile']['error'] != UPLOAD_ERR_OK || 
      $_FILES['uploadFile']['mime'] != "image/gif") { 
      print "<p>File not uploaded successfully!</p>"; 
      print "<p>Please make sure the file has the correct file extension: .gif</p>"; 
      print "<p><a href='a7p2.php'>Try uploading again</a>"; 
      var_dump($_FILES['uploadFile']['error']); 
      die; 
      } else { 
      move_uploaded_file($_FILES['uploadFile']['name'], $uploadFileDir) or die("Can't move file to $uploadFileDir"); 
      print "<p>Success!</p>"; 
      } 
     } 

     $uploadedFiles = glob("/students/ryan/php/images/*.gif"); 
     if($uploadedFiles != false) { 
      print "<p>Here are pictures from the art gallery: </p>"; 
      foreach($uploadedFiles as $file) { 
      $url = "http://hills.ccsf.edu/~ryan/images/" . substr($file, strrpos($file, '/') + 1); 
      print "<p><img src=\"$url\"></p>"; 
      } 
     } else { 
      print "There are no pictures in the art gallery"; 
     } 
     ?> 

     </body> 
    </html> 

或相同的代碼,鏈接下面的代碼:

http://pastebin.com/JNxwy323

+0

你能否詳細說明爲** **如何這不是加工? – Archimaredes

+0

它一直說文件沒有成功上傳。請確保文件具有正確的文件擴展名。嘗試再次上傳 – yan

+0

嘗試在整個'$ _FILES ['uploadFile']上執行'var_dump''當您看到設置。看看它包含什麼。 – LSerni

回答

3

它是不是安全靠瀏覽器告訴你文件的MIME類型。相反,你應該檢查文件是否應該是。在這種情況下:

if([email protected]($_FILES['uploadFile']['tmp_name'])) { 
    // file is not a valid GIF image 
} 
-1

您需要使用tmp_name的值,而不是名稱爲move_uploaded_file應該

move_uploaded_file($_FILES['uploadFile']['tmp_name'], $uploadFileDir) or die("Can't move file to $uploadFileDir"); 

代替$ _FILES [ 'uploadFile']也[ '默'] =「圖像/ GIF「){

它應該是$ _FILES [ 'uploadFile'] [ '型']!= 」圖像/ GIF「){

+0

Nauto mo ako kupal –