2013-04-05 166 views
1

我的HTML表單:PHP zip文件上傳不起作用

<form action='' method="post" enctype="multipart/form-data"> 
<input type="file" name="file" id="file"> 
<input type="submit" name="submit" value="Submit"> 
</form> 

我的PHP文件:

if ($_POST['submit'] == "Submit") { 
      echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
      echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
      echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 
      if (file_exists("/downloads/" . $_FILES["file"]["name"])) 
      { 
       echo $_FILES["file"]["name"] . " already exists. "; 
      } 
      else 
      { 
       move_uploaded_file($_FILES["file"]["tmp_name"], 
       "/downloads/" . $_FILES["file"]["name"]); 
       echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"]; 
      } 
} 

有在我的public_html 2個文件夾:tmpdownloads,既777個權限(剛測試)

此文件位於下載(public_html/downloads/new/update.php).htaccess受保護的文件夾中 ,我希望zip文件爲b上傳到下載目錄中。

此代碼不會給我任何錯誤,但不會上傳文件。爲什麼?

+3

可怕的代碼。你假設上傳成功。你直接在文件系統操作中使用用戶提供的文件名(惡意用戶現在可以在你的服務器上隨意塗寫),並且你簡單地假設移動操作成功。 – 2013-04-05 21:24:57

+0

問題在於「/ downloads /」。 $ _FILES [「文件」] [「名稱」]'已被張貼爲答案 – craig1231 2013-04-05 22:11:16

+0

不擔心安全,因爲這個文件是在一個受保護的目錄,只有我可以訪問... – Laurent 2013-04-06 08:15:45

回答

0

這是我如何解決它:

HTML:

<form enctype="multipart/form-data" method="post" action=""> 
<input type="hidden" name="MAX_FILE_SIZE" value="300000" /> 
<input type="file" name="file" id="file"> 
<input type="submit" name="submit" value="Submit"> 
</form> 

PHP:

if($_POST['submit'] == "Submit") { 
    $filename = $_FILES["file"]["name"]; 
    $source = $_FILES["file"]["tmp_name"]; 
    $type = $_FILES["file"]["type"]; 
    $name = explode(".", $filename); 
    $target_path = "../".$filename; 
    if(move_uploaded_file($source, $target_path)) { 
     $message = "Your .zip file was uploaded"; 
    } else {  
     $message = "ERROR"; 
    } 
     if($message) echo $message; 
} 
2

嘗試增加

if(move_uploaded_file(...)){ 
echo "it works"; 
} else { 
echo "NOPE"; 
} 

而且 取代:

move_uploaded_file($_FILES["file"]["tmp_name"], "/downloads/" . $_FILES["file"]["name"]); 

有:

move_uploaded_file($_SERVER['DOCUMENT_ROOT'].'/'.$_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT']."/downloads/" . $_FILES["file"]["name"]); 
+0

不,它返回不 – Laurent 2013-04-06 08:19:33

1

PHP需要MAX_FILE_SIZE接收上傳的文件

<form action='' method="post" enctype="multipart/form-data"> 
<input type="hidden" name="MAX_FILE_SIZE" value="10240000"> 
<input type="file" name="file" id="file"> 
<input type="submit" name="submit" value="Submit"> 
</form> 

這種形式,您可以上傳與10 MB或更小的拉鍊檔案館...

你還必須設置max_post_size並在php.ini的upload_max_filesize相同或更高的值來上傳文件

使用這個PHP代碼

if ((isset($_POST['submit']) && $_POST['submit'] == "Submit") AND isset($_FILES)) { 
     echo "Upload: " . $_FILES["file"]["name"] . "<br>"; 
     echo "Type: " . $_FILES["file"]["type"] . "<br>"; 
     echo "Size: " . ($_FILES["file"]["size"]/1024) . " kB<br>"; 
     echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>"; 
     if (file_exists("/downloads/" . $_FILES["file"]["name"])) 
     { 
      echo $_FILES["file"]["name"] . " already exists. "; 
     } 
     else 
     { 
      if(move_uploaded_file($_FILES["file"]["tmp_name"], 
      "/downloads/" . $_FILES["file"]["name"])) 
        echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"]; 

      else echo "file could not be processed"; 
     } 

}