2012-12-28 46 views
0

我的代碼:與PHP的問題上傳

if(isset($_FILES['image'])){ 
    $allowedExts = array('jpg', 'gif', 'png'); 
    $extension = end(explode('.', $_FILES['image']['name'])); 
    if(in_array($extension, $allowedExts)){ 
     if($_FILES['image']['size'] < 50000){ 
      if ($_FILES['image']['error'] > 0){ 
       $uploaderror = $_FILES['image']['error']; 
      }else{ 
       $uploaderror = 'FALLBACK ERROR'; 
       if(file_exists('..images/'.$_FILES['image']['name'])){ 
        $uploaderror = 'The file <strong>'.$_FILES['image']['name'].'</strong> already exists in the images directory.'; 
       }else{ 
        move_uploaded_file($_FILES['file']['tmp_name'], '..images/'.$_FILES['file']['name']); 
        $uploadsuccess = $_FILES['file']['name']; 
       } 
      } 
     }else{$uploaderror = 'The image is too large.';} 
    }else{$uploaderror = 'Only images (.jpg, .png, and .gif) are allowed.';} 
}else{$uploaderror = 'No attempt';} 



輸出: $uploaderror回報FALLBACK ERROR$uploadsuccess沒有設置。該文件沒有出現在指定的目錄中,我無法在服務器上找到它。請告訴我我做錯了什麼。謝謝!

+2

哇,這是一些嘈雜的代碼。 – BastiBen

+1

你的表單是什麼樣的?在某些地方你使用'$ _FILES ['image']'和其他你使用'$ _FILES ['file']' –

+2

這種格式是錯誤的:''..images /'.$_ FILES ['file'] ['' name']'你丟失''''''''''''後面的'''' –

回答

1

你失蹤後/..images前,要解決這個問題只是改變這一點:

move_uploaded_file($_FILES['file']['tmp_name'], '..images/'.$_FILES['file']['name']); 

以下幾點:

move_uploaded_file($_FILES['file']['tmp_name'], '../images/'.$_FILES['file']['name']); 

與您的代碼通過一個終端,你可以運行得到以下回應:

..images:沒有這樣的文件或目錄

編輯

我發現了另一個地方,你忘了/,這是你的file_exists()檢查。

我已經清理你的代碼也使其更具可讀性:

<?php 
$errors = array(); 
$allowedExts = array('jpg', 'gif', 'png'); 
$extension = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION); 
if(!isset($_FILES['image'])){ 
    $errors[] = "No attempt."; 
} 
if(in_array($extension, $allowedExts)){ 
    $errors[] = "Only images (.jpg, .png, and .gif) are allowed."; 
} 
if($_FILES['image']['size'] > 50000){ 
    $errors[] = "The image is too large."; 
} 
if ($_FILES['image']['error'] <= 0){ 
    $errors[] = $_FILES['image']['error']; 
} 
if(file_exists('../images/'.$_FILES['image']['name'])){ 
    $errors[] = 'The file <strong>'.$_FILES['image']['name'].'</strong> already exists in the images directory.'; 
} 

// No errors found! 
if(count($errors) == 0){ 
    move_uploaded_file($_FILES['file']['tmp_name'], '../images/'.$_FILES['file']['name']); 
    $uploadsuccess = $_FILES['file']['name']; 
} 
+0

謝謝!使用數組的錯誤是好的,但不適用於'if(isset($ uploaderror))',我稍後使用它。 – Mooseman

+1

你可以做'if(!empty($ errors))'這意味着它包含錯誤 –