繁体   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