2012-04-22 47 views
1

我下面有這段代碼,問題是它沒有上傳文件,底部的回調函數沒有顯示結果。現在,在我輸入if isset($ _POST [fileImage'])...代碼和$_SESSION['fileImage']['name'] = $_FILES['fileImage']['name'];之前,文件上傳得很完美。但是使用這兩段代碼,它已經停止了上傳。下面的isset()函數是否正確放置?

所以我相信問題是我上面使用的代碼已經放在了錯誤的地方。如何將下面的代碼正確放置在正確的位置以便上傳以重新開始工作?

下面是它想上傳的文件,但未能代碼:

<?php 

    $result = 0; 

     if (isset($_POST['fileImage']) && ((($_FILES["fileImage"]["type"] == "image/gif") 
    || ($_FILES["fileImage"]["type"] == "image/jpeg") 
    || ($_FILES["fileImage"]["type"] == "image/pjpeg") 
    || ($_FILES["fileImage"]["type"] == "image/jpg") 
    || ($_FILES["fileImage"]["type"] == "image/png") 
    || ($_FILES["fileImage"]["type"] == "image/tif"))){ 

    if(file_exists("ImageFiles/".$_FILES['fileImage']['name'])) { 

     $parts = explode(".",$_FILES['fileImage']['name']); 
     $ext = array_pop($parts); 
     $base = implode(".",$parts); 
     $n = 2; 

     while(file_exists("ImageFiles/".$base."_".$n.".".$ext)) $n++; 
     $_FILES['fileImage']['name'] = $base."_".$n.".".$ext; 

     move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
     "ImageFiles/" . $_FILES["fileImage"]["name"]); 
     $result = 1; 

    } 
     else 
      { 
      move_uploaded_file($_FILES["fileImage"]["tmp_name"], 
      "ImageFiles/" . $_FILES["fileImage"]["name"]); 
      $result = 1; 

      } 

      ?> 

<script language="javascript" type="text/javascript"> 
window.top.stopImageUpload(<?php echo $result;?>); 
</script> 

      <?php 

    } 

     $_SESSION['fileImage']['name'] = $_FILES['fileImage']['name']; 

    ?> 
+0

只是提醒:$ _FILES [「文件」] [「類型」]是不安全的,以確定文件類型。這些信息由瀏覽器提供。您應該部署額外的檢查以確保它是一個映像或保護放置該文件的文件夾以防執行。 – bardiir 2012-04-22 18:03:32

+2

我不能確定沒有看到你的表單,但我的猜測是'$ _POST ['fileImage']'沒有設置。我只是檢查'isset($ _ FILES ['fileimage'])'和$ _FILES ['fileimage'] ['error'] == 0'。如果你有一個文件輸入字段'fileimage',那麼'$ _POST ['fileimage']'不會被填充。只有'$ _FILES'。 – drew010 2012-04-22 18:07:06

回答

0

我看到您正在使用$_FILES["fileImage"]$_POST['fileImage'],我不確定是否正確。

你也寫不讓它乾淨,可以很容易地識別錯誤..你可以使用這個方法

嘗試

<?php 

$result = 0; 
$errors = array(); 
$dirImage = "ImageFiles/"; 

$allowedTypes = array (
     "image/jpeg", 
     "image/gif", 
     "image/pjpeg", 
     "image/jpg", 
     "image/png", 
     "image/tif" 
); 

if (isset ($_FILES ['fileImage']) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) { 

    $fileName = $_FILES ['fileImage'] ['name']; 
    $fileSize = $_FILES ['fileImage'] ['size']; 
    $fileTemp = $_FILES ['fileImage'] ['tmp_name']; 
    $fileType = $_FILES ['fileImage'] ['type']; 

    $fileExt = pathinfo ($fileName, PATHINFO_EXTENSION); 
    $fileExt = strtolower ($fileExt); 

    if (empty ($fileName)) 
     continue; 

    if (! in_array ($fileType, $allowedTypes)) { 
     $errors [] = "File Type is not accepted"; 

    } 

    if(!is_writable($dirImage)) 
    { 
     $errors [] = "File Destination not writeable"; 
    } 


    $fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName; 
    $filePrifix = basename ($fileName, "." . $fileExt); 
    $i = 0; 
    while (file_exists ($fileDst)) { 
     $i ++; 
     $fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt; 

    } 
    // Move the file 

    if (count ($errors) == 0) { 
     if (move_uploaded_file ($fileTemp, $fileDst)) { 
      $result = 1; 
     } 
    } 

} 

$_SESSION ['fileImage'] ['name'] = $_FILES ['fileImage'] ['name']; 
?> 

請注意,錯誤存儲在$errors變量,哪些我的建議是你ro將它轉換爲json可以通過你的JavaScript代碼調用它

0

考慮使用一個函數來處理上傳:

function handleImageUpload() 
{ 
    // was a file uploaded? 
    if (!isset($_FILES['fileImage'])) { 
     return; 
    } 
    if (!isImage($_FILES['fileImage'])) { 
     // consider throwing an exception here 
     return; 
    } 
    $savepath = getFileSavePath($_FILES['fileImage'], 'ImageFiles/') 
    // handle upload 
    // ... 
} 

function isImage($imagepath) 
{ 
    // return boolean 
} 

function getFileSavePath($uploadpath, $destinationdirectory) 
{ 
    // return path to save upload without overwriting any existing file 
} 

考慮使用pathinfo()以獲取有關文件的信息。 http://www.php.net/manual/en/function.pathinfo.php