2012-04-12 148 views
0

我有一個名爲「Mobile_App」的文件夾,該文件夾是一個包含文件輸入,上傳文件的php腳本和稱爲「ImageFiles」的文件夾的html頁面。這個上傳腳本是否正確?

問題是文件沒有被上傳到「ImageFiles」中。我想知道的是,當將文件上傳到「Imagefiles」文件夾時,下面的代碼是正確的,還是不正確,我如何才能使它正常工作?

下面是PHP腳本上傳文件(文件輸入被稱爲「fileImage」):

<?php 

    $destination_path = $_SERVER['DOCUMENT_ROOT']."/ImageFiles"; 

    $result = 0; 

    $target_path = $destination_path . basename($_FILES['fileImage']['name']); 

    if(move_uploaded_file($_FILES['fileImage']['tmp_name'], $target_path)) { 
     $result = 1; 
    } 

    sleep(1); 

?> 
+0

那麼運行腳本時會發生什麼? – acrosman 2012-04-12 18:08:51

+0

當我運行腳本時,它只是一個空白頁 – user1324106 2012-04-12 18:11:50

+0

你試過'error_reporting()'? – 2012-04-12 18:20:32

回答

0

首先,文件夾名稱和文件名之間沒有/。其次,你不需要擁有文檔根目錄。更正後的代碼:

<?php 

    $destination_path = "/ImageFiles"; 

    $result = 0; 

    $target_path = $destination_path . "/" . basename($_FILES['fileImage']['name']); 

    if(move_uploaded_file($_FILES['fileImage']['tmp_name'], $target_path)) { 
     $result = 1; 
    } 

    sleep(1); 

?> 
-1

號您還沒有在所有如果上傳成功或失敗的檢查。您也不會檢查文件名衝突,您的腳本將很高興地用新文件覆蓋任何現有文件。

<?php 

if ($_FILES['fileImage']['error'] !== UPLOAD_ERR_OK) { 
    die("Upload failed with error code " . $_FILES['fileImage']['error']); 
} 

... 
$target_path = etc... 
... 
if (file_exists($target_path)) { 
    die("$target_path exists already"); 
} 
// got here, must be ok to proceed 
if (move_uploaded_file(....)) { 
etc... 

上傳錯誤代碼是defined here