2014-09-27 77 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数据库