poleposters Posted March 4, 2008 Share Posted March 4, 2008 I am creating a form to add information to a business directory. The script displays the business name, a two line description, an address, phone ,category and a profile image. The script works great. Except I want the profile image upload to be optional. If i submit the form without uploading an image I get an error message. I've tried writing the form with various if-else conditionals but they keep coming up with the wrong result. Any ideas. Thanks. <?php if(isset($_POST['Submit'])) { $business_name=$_POST['business_name']; $line_one=$_POST['line_one']; $line_two=$_POST['line_two']; $address=$_POST['address']; $phone=$_POST['phone']; $cat_id=$_POST['cat_id']; $size = 150; // the thumbnail height $filedir = 'pics/'; // the directory for the original image $thumbdir = 'pics/'; // the directory for the thumbnail image $prefix = 'small_'; // the prefix to be added to the original name $maxfile = '2000000'; $mode = '0666'; $userfile_name = $_FILES['image']['name']; $userfile_tmp = $_FILES['image']['tmp_name']; $userfile_size = $_FILES['image']['size']; $userfile_type = $_FILES['image']['type']; $prod_img = $filedir.$userfile_name; $prod_img_thumb = $thumbdir.$prefix.$userfile_name; move_uploaded_file($userfile_tmp, $prod_img); chmod ($prod_img, octdec($mode)); $sizes = getimagesize($prod_img); $aspect_ratio = $sizes[1]/$sizes[0]; if ($sizes[1] <= $size) { $new_width = $sizes[0]; $new_height = $sizes[1]; }else{ $new_height = $size; $new_width = abs($new_height/$aspect_ratio); } $destimg=ImageCreateTrueColor($new_width,$new_height) or die('Problem In Creating image'); $srcimg=ImageCreateFromJPEG($prod_img) or die('Problem In opening Source Image'); if(function_exists('imagecopyresampled')) { imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing'); }else{ Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg)) or die('Problem In resizing'); } ImageJPEG($destimg,$prod_img_thumb,90) or die('Problem In saving'); imagedestroy($destimg); $coupon_image=$prod_img_thumb; echo ' <a href="'.$prod_img.'"> <img src="'.$prod_img_thumb.'" width="'.$new_width.'" heigt="'.$new_height.'"> </a>'; echo $business_name; echo $line_one; echo $line_two; echo $address; echo $phone; echo $coupon_image; echo $cat_id; }else{// display upload form Link to comment https://forums.phpfreaks.com/topic/94274-optional-image-upload-then-resize/ Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2008 Share Posted March 4, 2008 You need to test the ['error'] element of the uploaded $_FILES['image'] array for a value of 4 - UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded. Reference - http://www.php.net/manual/en/features.file-upload.errors.php Link to comment https://forums.phpfreaks.com/topic/94274-optional-image-upload-then-resize/#findComment-483047 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.