如何使用可选的上传文件提交表单数据。我的意思是,我可以将表单数据与上传的文件一起提交吗?但我想让上传文件成为可选的。这是我的代码:
<?php
define ("MAX_SIZE","5000");
$errors=0;
if(($_SERVER["REQUEST_METHOD"] == "POST") && isset($_FILES["file"]["size"]) && ($_FILES["file"]["size"] > 0))
{
$image =$_FILES["file"]["name"];
$uploadedfile = $_FILES['file']['tmp_name'];
if ($image)
{
$filename = stripslashes($_FILES['file']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))
{
$change='<div class="msgdiv">Unknown Image extension </div> ';
$errors=1;
}
else
{
$size=filesize($_FILES['file']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
$change='<div class="msgdiv">You have exceeded the size limit!</div> ';
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" )
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefromjpeg($uploadedfile);
}
else if($extension=="png")
{
$uploadedfile = $_FILES['file']['tmp_name'];
$src = imagecreatefrompng($uploadedfile);
}
else
{
$src = imagecreatefromgif($uploadedfile);
}
echo $scr;
list($width,$height)=getimagesize($uploadedfile);
$newwidth=500;
$newheight=($height/$width)*$newwidth;
$tmp=imagecreatetruecolor($newwidth,$newheight);
$newwidth1=300;
$newheight1=($height/$width)*$newwidth1;
$tmp1=imagecreatetruecolor($newwidth1,$newheight1);
imagealphablending($tmp1, false);
imagesavealpha($tmp1, true);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);
$image_name=time().'_'.$_FILES['file']['name'];
$filename = "images/". $image_name;
$filename1 = "images/small_". $image_name;
$uid=$_SESSION['uid'];
imagejpeg($tmp,$filename,100);
imagejpeg($tmp1,$filename1,100);
imagedestroy($src);
imagedestroy($tmp);
imagedestroy($tmp1);
}}
}
if(isset($_POST['Submit']) && !$errors )
{
$category_group= $_POST['category_group'];
$title=$_POST['title'];
$details=$_POST['details'];
$ad_keywords=$_POST['ad_keywords'];
$category_state=$_POST['category_state'];
$category_city=$_POST['category_city'];
$address=$_POST['address'];
$telephone=$_POST['telephone'];
$email=$_POST['email'];
$website=$_POST['website_link'];
$price=$_POST['price'];
$filename= ($_FILES["file"]["size"] > 0) ? $filename : '';
$filename1= ($_FILES["file"]["size"] > 0) ? $filename1 : '';
$url=time().'-'. createUrl($title);
if(!$category_group || !category_city || !$title || !$details || !$category_state ){
echo "enter state and city name";
}else{
try{
$sth = $dbh->prepare("
INSERT INTO
advertisement(user_id,ad_image_big,ad_image_small,ad_cat_group,ad_title,ad_details,ad_state,ad_city,ad_address,ad_telephone,ad_email,ad_website,ad_price,ad_slug,ad_keywords)
VALUES(:field1,:field2,:field3,:field4,:field5,:field6,:field7,:field8,:field9,:field10,:field11,:field12,:field13,:field14,:field15)
");
$sth->execute(array(':field1' => $uid, ':field2' => $filename, ':field3' => $filename1, ':field4' => $category_group, ':field5' => $title, ':field6' => $details, ':field7' => $category_state,':field8'=> $category_city, ':field9' => $address, ':field10' => $telephone,':field11'=> $email,':field12'=> $website, ':field13' => $price, ':field14' => $url , ':field15'=> $ad_keywords));
}catch (PDOException $ex){
echo "An Error occured while inserting data to database";
some_logging_function($ex->getMessage());
}
header('location:success.php');
}
}
?>
每当我尝试在没有上传文件的情况下提交表单时,我都会收到此错误:
“将数据插入数据库时发生错误”
这是在捕获部分。如果我上传文件,我会收到一条成功消息,没有任何错误。请帮帮我。