簡體   English   中英

輸入文件空處理

[英]input file empty handling

所以,我有一個表格,它是文章表格,其中有標題,內容,圖像(用於顯示使用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."
);

檢查我的輸入文件是否有錯誤,並且可以很好地添加新文章...

但是,如果我想編輯我的文章怎么辦? 通常我會重復使用相同的表格,但是回顯所選文章的值並顯示所有內容,它顯示標題,內容,圖像,但不包含輸入文件,並且我知道這是安全問題,因此,例如,我只想更改文章標題,因為我提交了包括輸入文件在內的所有表單內容,因此將出現錯誤“無文件” ...那么這種問題是否有最佳實踐? 也許有一些方法可以識別是否我沒有選擇任何圖像,那么我只會通過它而不顯示任何錯誤...

這是我如何處理錯誤

$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];
}

所以,如果沒有輸入,我需要怎么做才能繞過錯誤檢查?

所以我只是為此得到了最好的解決方案。。。。

$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

$ image只是我將圖像名稱/路徑放入mysql數據庫的占位符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM