【发布时间】:2012-06-08 19:09:24
【问题描述】:
我有一个表单,用于提交包含图像的新帖子。如果用户没有选择要上传的图像,我不希望帖子继续向服务器和数据库提交数据。目前我所做的一切都是让代码继续处理表单。
这是我的一些代码:
//check if the submit button has been clicked
if( isset( $_POST['submit'] ) ){
//validate the title and description
$title = validate_title($_POST['title']);
$desc = validate_desc($_POST['desc']);
//Get other posted variables
$cat = $_POST['cat'];
$year = $_POST['year'];
if($title && $desc != false){
//check if an image has been submitted
if( $_FILES['files']['name'] != ""){...
我也尝试过使用以下方法,但是在没有选择文件时都不会停止代码:
if( $_FILES['files']['error'] == UPLOAD_ERR_OK){...
if( $_FILES['files']['name'] != UPLOAD_ERR_NO_FILE){...
【问题讨论】:
-
检查名称应该可以。 var_dump($_FILES) 在你检查你有什么之前。
-
我明白了:array(1) { ["files"]=> array(5) { ["name"]=> array(1) { [0]=> string(11) "d3beta1.png" } ["type"]=> array(1) { [0]=> string(9) "image/png" } ["tmp_name"]=> array(1) { [0]=> string(45) "C:\Users\chris\AppData\Local\Temp\php6B29.tmp" } ["error"]=> array(1) { [0]=> int(0) } ["size"] => 数组(1) { [0]=> int(1728245) } } }
-
即使我选择要上传的图像也会给我错误,也使用:
-
"if((!empty($_FILES["files"])) && ($_FILES['files']['error'] == 0)) {"
-
您缺少索引。 if ($_FILES['files']['name'][0] != "")
标签: php file-upload error-handling