我有一個編輯表單用戶可以編輯他/她的信息。 它包含名字,姓氏,用戶名,類別(其選擇從類別表數據)和圖像輸入。我想要一個可以檢查圖像類型,大小,是否爲空的函數。實際上,如果用戶沒有上傳照片,然後並不需要檢查,但如果用戶上傳照片,然後在功能檢查它的文件類型(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
首先,使用['is_uploaded_file()'](http://php.net/manual/en/function .is-uploaded-file.php)函數來檢查用戶是否上傳了任何圖像,然後相應地處理您的表單。 –
您必須告訴我們什麼不起作用,請將其編輯到您的帖子中。 –