2014-09-27 89 views
0

因此,我有一個表單,它是標題,內容,圖像(用於顯示通過使用javascript輸入選定圖像)和輸入(選擇圖像)的文章形式,並且我使用輸入文件空處理

$upload_errors = array(
    // http://www.php.net/manual/en/features.file-upload.errors.php 
    UPLOAD_ERR_OK    => "No errors.", 
    UPLOAD_ERR_INI_SIZE  => "Larger than upload_max_filesize.", 
    UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.", 
    UPLOAD_ERR_PARTIAL  => "Partial upload.", 
    UPLOAD_ERR_NO_FILE  => "No file.", 
    UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.", 
    UPLOAD_ERR_CANT_WRITE => "Can't write to disk.", 
    UPLOAD_ERR_EXTENSION => "File upload stopped by extension." 
); 

,以檢查是否有錯誤我的輸入文件,它工作正常添加新的文章...

但是,如果我想修改自己的文章是什麼?通常我會重複使用相同的表單,但回顯選定的文章值和所有顯示,它顯示標題,內容,圖像,但沒有輸入文件,我知道這是安全問題,所以例如,我只想改變文章標題,我由於我提交了包括輸入文件在內的所有表單內容,因此會出現「沒有文件」的錯誤...因此,是否有這種問題的最佳做法?也許某種方式indentify如果我不選擇任何圖像,然後我將只是通過它,尚未出現任何錯誤...

,這裏是我如何處理錯誤

$error = $_FILES['upload_file']['error']; 
if ($error != 1) { 
    $information->id = $_POST['id']; 
    $information->name = $_POST['name']; 
    $name = $_POST['name']; 
    $information->upload_image($_FILES['upload_file']['tmp_name']); 

    $content = $_POST['content']; 
    $entity_content = htmlentities($content); 
    $entity_content = stripslashes(str_replace('\r\n', '',$entity_content)); 
    $information->content = $entity_content; 
    try{ 
     if($information->save()){ 
      if(isset($_POST['simpan'])){ 
       redirect_to("show_information.php"); 
      } 
     }else{ 
      $message = "failed to change information"; 
     } 
    }catch(PDOException $e){ 
     $message = "failed to change information"; 
     error_notice($e); 
    } 
}else{ 
    $message = $upload_errors[$error]; 
} 

所以我需要什麼如果沒有輸入,是否繞過錯誤檢查?

+0

你可以添加一個複選框,如果它的檢查你運行一個不同的錯誤塊否則運行上面的一個可能嗎? – Parody 2014-09-27 16:28:25

+0

在您的測試中,檢查文章是否已經存在;如果是這樣,忘記輸入文件:「if($ error!= 1或article_exists($ _ POST ['id'])=== true)''假設''article_exists()''返回''true''如果文章存在。在這種情況下,不要''upload_image()''。 – Xenos 2014-09-27 16:42:47

+0

所以沒有辦法處理它沒有添加複選框或東西到我的表單?我只是想爲用戶使用我的網頁形式最簡單的方法.... :( – PUCUK 2014-09-28 00:15:59

回答

0

,所以我剛剛得到這個最佳的解決方案....其實很容易

$error = $_FILES['upload_file']['error']; 
if ($error == 0 || $error == 4) { 
    if($error != 4) 
    //do the uploading 
} 

,並在我的上傳/保存類我只需要檢查

if(!empty($image)) 
    // do move_uploaded_to 
    // do the saving with replacement of $image 
else 
    // do the saving without $image 

$圖像只是我的佔位符把圖像名稱/路徑到MySQL數據庫