Image upload and if / else

11-14-04, 10:24 AM
|
 |
Aspiring Coder
|
|
Join Date: Oct 2003
Posts: 499
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Image upload and if / else
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....
$max_size = 20000;
$filesize = $_FILES['upfile']['size'];
$filetype = $_FILES['upfile']['type'];
$upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/blah/blah/";
$photo = substr(sha1(rand(10, time())), 0, 8) . '.' . $file_types[$filetype];
if (move_uploaded_file($_FILES['upfile']['tmp_name'], $upload_dir . $photo))
{
insert query.....
}
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.
|

11-14-04, 01:53 PM
|
|
Newbie Coder
|
|
Join Date: Nov 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
Last edited by Dyllon; 11-14-04 at 01:55 PM.
|

11-14-04, 02:59 PM
|
 |
Aspiring Coder
|
|
Join Date: Oct 2003
Posts: 499
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Sorry, but it isn't working. Here is my upload script....
PHP Code:
$file_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', ); $mm=$_POST['mm']; $dd=$_POST['dd']; $yyyy=$_POST['yyyy']; $main_title=$_POST['main_title']; $category_name=$_POST['category_name']; $bus_name=$_POST['bus_name']; $personal_name=$_POST['personal_name']; $pword=$_POST['pword']; $phone=$_POST['phone']; $address=$_POST['address']; $city=$_POST['city']; $zip_code=$_POST['zip_code']; $email=$_POST['email']; $web_url=$_POST['web_url']; $mon_hours=$_POST['mon_hours']; $tue_hours=$_POST['tue_hours']; $wed_hours=$_POST['wed_hours']; $thu_hours=$_POST['thu_hours']; $fri_hours=$_POST['fri_hours']; $sat_hours=$_POST['sat_hours']; $sun_hours=$_POST['sun_hours']; $location=$_POST['location']; $desc=$_POST['desc']; $info=$_POST['info']; $plan_agreement=$_POST['plan_agreement']; $list_plan=$_POST['list_plan']; $confirm_question=$_POST['confirm_question']; $confirm_answer=$_POST['confirm_answer']; $bus_name=addslashes($bus_name); $location=addslashes($location); $desc=addslashes($desc); $info=addslashes($info); $max_size = 20000; $filesize = $_FILES['upfile']['size']; $filetype = $_FILES['upfile']['type']; $upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/blah/ad_images/";
$photo = substr(sha1(rand(10, time())), 0, 8) . '.' . $file_types[$filetype]; if (move_uploaded_file($_FILES['upfile']['tmp_name'], $upload_dir . $photo)) {
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.
|

11-14-04, 03:35 PM
|
|
Newbie Coder
|
|
Join Date: Nov 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
This should work
PHP Code:
/*== BEGIN FUNCTIONS ==*/
function getFileExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
function uploadImage($imgfile, $imgfile_name) {
/*== upload directory where the file will be stored
relative to where script is run ==*/
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . "/blah/ad_images/";
// Use this if you want to restrict image dimensions
$imagesize = getimagesize($imgfile);
$imagewidth = $imagesize[0];
$imageheight = $imagesize[1];
$maxwidth = 200;
$maxheight = 200;
/*== get file extension ==*/
$pext = getFileExtension($imgfile_name);
$pext = strtolower($pext);
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 (is_uploaded_file($imgfile))
{
$Image = $final_filename;
$Image = stripslashes($Image);
return $Image;
}
}
}
/*== END FUNCTIONS ==*/
$mm=$_POST['mm'];
$dd=$_POST['dd'];
$yyyy=$_POST['yyyy'];
$main_title=$_POST['main_title'];
$category_name=$_POST['category_name'];
$bus_name=$_POST['bus_name'];
$personal_name=$_POST['personal_name'];
$pword=$_POST['pword'];
$phone=$_POST['phone'];
$address=$_POST['address'];
$city=$_POST['city'];
$zip_code=$_POST['zip_code'];
$email=$_POST['email'];
$web_url=$_POST['web_url'];
$mon_hours=$_POST['mon_hours'];
$tue_hours=$_POST['tue_hours'];
$wed_hours=$_POST['wed_hours'];
$thu_hours=$_POST['thu_hours'];
$fri_hours=$_POST['fri_hours'];
$sat_hours=$_POST['sat_hours'];
$sun_hours=$_POST['sun_hours'];
$location=$_POST['location'];
$desc=$_POST['desc'];
$info=$_POST['info'];
$plan_agreement=$_POST['plan_agreement'];
$list_plan=$_POST['list_plan'];
$confirm_question=$_POST['confirm_question'];
$confirm_answer=$_POST['confirm_answer'];
$bus_name=addslashes($bus_name);
$location=addslashes($location);
$desc=addslashes($desc);
$info=addslashes($info);
//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);
|

11-14-04, 04:12 PM
|
 |
Aspiring Coder
|
|
Join Date: Oct 2003
Posts: 499
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Hybrid Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|