2016-12-26 35 views
0

我有一個編輯表單用戶可以編輯他/她的信息。 它包含名字,姓氏,用戶名,類別(其選擇從類別表數據)和圖像輸入。我想要一個可以檢查圖像類型,大小,是否爲空的函數。實際上,如果用戶沒有上傳照片,然後並不需要檢查,但如果用戶上傳照片,然後在功能檢查它的文件類型(JPG,PNG,JPEG),並檢查文件大小不超過刨絲1MB。

其實我的代碼工作時,所有的輸入填寫和圖像uploaded.but如果用戶沒有上傳照片,然後將出現<此消息>。這是更新的形式,所以也許用戶上傳照片前,他/她並不想改變這種狀況photo.so我的問題是用戶提交斜面他/她的信息,直到上傳圖片。

$_FILES['img']['size'] 

檢查,如果它是圖像文件或沒有或圖像文件格式,你可以通過getimagesize()這樣的檢查$_FILES['img']['tmp_name']

if (isset($_POST['submit'])) 
{ 
$Title = test_input($_POST['Title']); 
$Category = $_POST['Category']; 
$Content = test_input($_POST['Post']); 
$Author = test_input($_POST['Author']); 
$ref = test_input($_POST['reference']); 

$image= $_FILES["img"]["name"]; 
$imgSize = $_FILES["img"]["size"]; 
$image_tmp= $_FILES["img"]["tmp_name"]; 

$traget_dir = "../files/img/content/"; 
$target_file = $target_dir.basename($_FILES["img"]["name"]); 
$ImageFileType=pathinfo($target_file,PATHINFO_EXTENSION); 



if(empty($Title)) 
{ 
    $msg=" * title couldn't be empty"; 
} 
else if(empty($Content)) 
{ 
    $msg=" * Content couldn't be empty"; 
} 
else if(empty($Author)) 
{ 
    $msg=" * Author couldn't be empty"; 
} 
else if($image =="") 
{ 
    echo $msg=" * you must choose a photo"; 
    $uploadOK=1; 
} 

else if($ImageFileType !="jpg" && $ImgeFileType != "gif" &&$ImageFileType  !="png") 
{ 
    echo $msg=" * only JPG, PNG, GIF are allowed"; 
} 

else if($imgSize > 1024000) 
{ 
    echo $msg= " * the photo shouldn't be greater that 1MB"; 
} 



else if(move_uploaded_file($_FILES["img"]["tmp_name"],$target_file)) 
{ 


    echo $msg1= " * the file ".basename($_FILES['img']['name'])." has been uploaded"; 


    $uploadOk = 0; 
} 

$query= "UPDATE content SET (`Title`='$Title' ,`Text`= '$Content' ,`Writer`='$Author',`Reference`='$ref',`Image`= '$image') where Content_ID='$id' "; 

if(mysqli_query($_SESSION['con'],$query)) 
{ 
     $msg1="post published"; 

} 
else 
    { 
     echo $msg="there was an error occured  <br>".mysqli_error($_SESSION['con']); 
    }here 
+0

首先,使用['is_uploaded_file()'](http://php.net/manual/en/function .is-uploaded-file.php)函數來檢查用戶是否上傳了任何圖像,然後相應地處理您的表單。 –

+1

您必須告訴我們什麼不起作用,請將其編輯到您的帖子中。 –

回答

1

你可以得到的文件大小以字節爲單位php how to use getimagesize() to check image type on upload

檢查如果文件上傳或沒有,你可以檢查它與error。錯誤4分文件的手段不上傳:

if ($_FILES['img']['error']==4){ 
    echo 'Not uploaded'; 
} 

在這裏你可以看到更多有關Error Messages Number

+0

我想,如果沒有上傳不顯示任何消息,只是繼續其他代碼或提交form.and檢查大小的文件,文件的類型用戶上傳文件時 – Mo0rteza