所以,我有一个表格,它是文章形式,其中有标题,内容,图像(用于通过使用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];
}
那么如果没有输入,我还需要绕过错误检查?
答案 0 :(得分: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
$ image只是我的占位符,用于将图像名称/路径放到mysql数据库