我有一个编辑表单用户可以编辑他/她的信息。 它包含名字,姓氏,用户名,类别(其选择从类别表数据)和图像输入。我想要一个可以检查图像类型,大小,是否为空的函数。实际上,如果用户没有上传照片,然后并不需要检查,但如果用户上传照片,然后在功能检查它的文件类型(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)函数来检查用户是否上传了任何图像,然后相应地处理您的表单。 –
您必须告诉我们什么不起作用,请将其编辑到您的帖子中。 –