I have a basic form allowing text and an image upload, which works fine, adding text to a database and uploading the image to a folder. I'd like to add an option so that, if a user doesn't want to add an image, they can click a checkbox stating "No Image", i.e....
<b>Add an Image</b><br>
( File must be a .jpg or .gif extension only )<br>
<input type="file" name="upfile"> <p>
If no image, <input type=checkbox name=upfile value=no_image>Check this box
( I'm not sure what name or value to give the checkbox. )
Then, in the upload script, I have this, which gives the photo name ( 'upfile' ) a random name....
What I'm not sure of on this part is where/what to place in an if/else statement so that, if no image is uploaded, a default image name is inserted into the database ( i.e., "no_image.gif", determined by the checkbox? ). Either what I've tried has been put into the wrong place, written incorrectly, or both. Like I said, basic, but I guess I'm missing or overlooking something.
This is the method I use, it's way better than putting a checkbox on your form.
I also use a function to upload the image incase I have several to upload.
If the upload function can help you let me know and I'll post it here.
I used 'img' for your field name.
PHP Code:
if (!empty($img)) {
$NewImage = uploadImage($img, $img_name);
}
else {
$NewImage = "no_image.gif";
}
// insert $NewImage into your database
include("siteinfo.php"); require("rand_num.php"); include("connect.php"); $conn = mysql_connect(localhost,"$dbuser","$dbpass") or die ("Could not connect MySQL"); mysql_select_db("$db") or die ("Could not open database"); $query = "INSERT INTO listings VALUES('','$mm', '$dd', '$yyyy', '$main_title','$category_name','$bus_name','$personal_name','$pword','$phone','$address','$city','IN','$zip_code','$email','$web_url','$mon_hours', '$tue_hours','$wed_hours','$thu_hours','$fri_hours','$sat_hours','$sun_hours', '$location','$desc','$info','$photo','$plan_agreement','$list_plan','N','0','$account','$confirm_question','$confirm_answer','0','0','0')"; $result=mysql_query($query); }
I want the image name to go into the '$photo' field in the database.
// Use this if you want to restrict image dimensions
$imagesize = getimagesize($imgfile);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
$maxwidth = 200;
$maxheight = 200;
if($imagewidth > $maxwidth || $imageheight > $maxheight)
{
//display error for image being too large
//remember to remove this condition if your not limiting the image dimesnions
}
elseif (($pext != "jpg") && ($pext != "jpeg") && ($pext != "gif"))
{
//display error for invalid image extension
}
else {
/*== setup final file location and name ==*/
/*== change spaces to underscores in filename ==*/
$rand_numb = md5(uniqid(microtime()));
$neu_name = "$rand_numb"."$imgfile_name";
$final_filename = str_replace(" ", "_", $neu_name);
$newfile = $uploaddir . "/$final_filename";
if (is_uploaded_file($imgfile))
{
/*== move file to proper directory ==*/
if (!copy("$imgfile","$newfile"))
{
/*== if an error occurs the file could not
be written, read or possibly does not exist ==*/
print "Error Uploading File.";
}
}
//if photo is supplied upload and get the new random name
//otherwise set $photo to default image
if (!empty($photo)) {
$photo = uploadImage($photo, $photo_name);
}
else {
$photo = "no_image.gif";
}
include("siteinfo.php");
require("rand_num.php");
include("connect.php");
$conn = mysql_connect(localhost,"$dbuser","$dbpass") or die ("Could not connect MySQL");
mysql_select_db("$db") or die ("Could not open database");
$query = "INSERT INTO listings VALUES('','$mm', '$dd', '$yyyy', '$main_title','$category_name','$bus_name','$perso nal_name','$pword','$phone','$address','$city','IN ','$zip_code','$email','$web_url','$mon_hours',
'$tue_hours','$wed_hours','$thu_hours','$fri_hours ','$sat_hours','$sun_hours',
'$location','$desc','$info','$photo','$plan_agreem ent','$list_plan','N','0','$account','$confirm_que stion','$confirm_answer','0','0','0')";
$result=mysql_query($query);