spacepoet Posted February 4, 2012 Share Posted February 4, 2012 Hello: I am working with a script that I am using to make a player roster - the user can add a photo, player title, number, years played, and a bio. The data comes from a submit form, then goes to the code below to process everything. It works fine if a photo is added, but if a photo is not added, the script does not work. Any idea why, and how I can fix it? <?php // initialization $result_final = ""; $counter = 0; // List of our known photo types $known_photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); // GD Function List $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); $photos_uploaded = $_FILES['photo_filename']; $player_title = $_POST['player_title']; $player_number = $_POST['player_number']; $years_played = $_POST['years_played']; $photo_caption = $_POST['photo_caption']; while( $counter <= count($photos_uploaded) ) { if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) { $result_final .= "File ".($counter+1)." is not a photo<br />"; } else { //mysql_query( "INSERT INTO gallery_photos(`photo_filename`,`player_title`,`player_number`,`years_played`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); mysql_query( "INSERT INTO gallery_photos(`photo_filename`,`player_title`,`player_number`,`years_played`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($player_title[$counter])."', '".addslashes($player_number[$counter])."' , '".addslashes($years_played[$counter])."', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); // Store the orignal file copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename); // Let's get the Thumbnail size $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) //{ //$thumbnail_width = 100; //$thumbnail_height = (int)(100 * $size[1] / $size[0]); //} //else //{ //$thumbnail_width = (int)(100 * $size[0] / $size[1]); //$thumbnail_height = 100; //} { //$thumbnail_width = 690; //$thumbnail_height = (int)(500 * $size[1] / $size[0]); $old_width = $size[0]; $old_height = $size[1]; $thumbnail_width = 690; $thumbnail_height = ($old_height * $thumbnail_width / $old_width); } else { $thumbnail_width = (int)(690 * $size[0] / $size[1]); $thumbnail_height = 500; } // Build Thumbnail with GD 1.x.x, you can use the other described methods too $function_suffix = $gd_function_suffix[$filetype]; $function_to_read = "ImageCreateFrom".$function_suffix; $function_to_write = "Image".$function_suffix; // Read the source file $source_handle = $function_to_read ( $images_dir."/".$filename ); if($source_handle) { // Let's create an blank image for the thumbnail //$destination_handle = ImageCreate ( $thumbnail_width, $thumbnail_height ); $destination_handle = imagecreatetruecolor( $thumbnail_width, $thumbnail_height ); // Now we resize it ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] ); } // Let's save the thumbnail $function_to_write( $destination_handle, $images_dir."/tb_".$filename ); ImageDestroy($destination_handle ); // $result_final .= "<img src='".$images_dir. "/tb_".$filename."' style='margin-right: 20px; width: 100px;' />"; } } $counter++; } // Print Result echo <<<__HTML_END $result_final __HTML_END; ?> Thanks! Link to comment https://forums.phpfreaks.com/topic/256370-form-does-not-work-if-photo-is-not-added/ Share on other sites More sharing options...
El Chupacodra Posted February 4, 2012 Share Posted February 4, 2012 If you don't get an error message I would guess a parenthesis in the wrong place making the script only run when a photo is uploaded. It's easier to troubleshoot if you break it our into a separate function and then just call that function in your script. Do you get an error message? What happens with and without the photo? Link to comment https://forums.phpfreaks.com/topic/256370-form-does-not-work-if-photo-is-not-added/#findComment-1314371 Share on other sites More sharing options...
spacepoet Posted February 4, 2012 Author Share Posted February 4, 2012 Hi: That's part of the problem - I do not get an error message. If I add just a photo or a photo with 1 or all fields filled-in, it works fine. If I do not add a photo, nothing gets uploaded ... Any ideas? Thanks much. Link to comment https://forums.phpfreaks.com/topic/256370-form-does-not-work-if-photo-is-not-added/#findComment-1314482 Share on other sites More sharing options...
noXstyle Posted February 4, 2012 Share Posted February 4, 2012 Of course you don't, since if there is no files the '$_FILES['photo_filename']' will return null. Therefore your code will never run the while loop: 'while( $counter <= count($photos_uploaded) )' is equal to 'while(0<=0)'. Link to comment https://forums.phpfreaks.com/topic/256370-form-does-not-work-if-photo-is-not-added/#findComment-1314496 Share on other sites More sharing options...
spacepoet Posted February 4, 2012 Author Share Posted February 4, 2012 Ohhh .. OK, thanks for spotting that. What would be a suggestion to fix it? Thanks for pointing this out - really been stumped on this. Link to comment https://forums.phpfreaks.com/topic/256370-form-does-not-work-if-photo-is-not-added/#findComment-1314581 Share on other sites More sharing options...
noXstyle Posted February 5, 2012 Share Posted February 5, 2012 Oh wait, I was wrong, pardon me. Apparently the $_FILE will still pass the empty data and therefore keep the indexes intact. All you have to move the insert query between the while loop and $photos_uploaded['size'][$counter] > 0 statement. After that it will insert the entry even though there wouldn't be an uploaded image. Link to comment https://forums.phpfreaks.com/topic/256370-form-does-not-work-if-photo-is-not-added/#findComment-1314601 Share on other sites More sharing options...
floridaflatlander Posted February 5, 2012 Share Posted February 5, 2012 I use: if (isset($_FILES['image']) && ($_FILES['image']['error'] == UPLOAD_ERR_OK)) // image = your photo_filename (I think) Link to comment https://forums.phpfreaks.com/topic/256370-form-does-not-work-if-photo-is-not-added/#findComment-1314612 Share on other sites More sharing options...
spacepoet Posted February 5, 2012 Author Share Posted February 5, 2012 Hi ... Still no luck ... it is writing 5 blank entries each time, now that I have move the INSERT statement Maybe I should have posted and ask: "How can I revise the current form to do what I want" .. I'm stuck. The form has the option to upload up to 5 photos at once, but I only want to use 1 (I think I need to get rid of the "counter" portion of this code, on the form and the upload page): <?php // initialization $photo_upload_fields = ""; $counter = 1; // default number of fields $number_of_fields = 1; // If you want more fields, then the call to this page should be like, // preupload.php?number_of_fields=20 if( $_GET['number_of_fields'] ) $number_of_fields = (int)($_GET['number_of_fields']); // Firstly Lets build the Category List $result = mysql_query( "SELECT category_id,category_name FROM gallery_category" ); while( $row = mysql_fetch_array( $result ) ) { $photo_category_list .=<<<__HTML_END <option value="$row[0]">$row[1]</option>\n __HTML_END; } mysql_free_result( $result ); // Lets build the Photo Uploading fields while( $counter <= $number_of_fields ) { $photo_upload_fields .=<<<__HTML_END <tr> <td width='50'> Photo: <td width='550'> <input name='photo_filename[]' id='my_photo' type='file' /> </td> </tr> <tr> <td> Name: </td> <td> <input type='text' name='player_title[]' id='my_player_title' onblur='javascript:myWhite()' size='65' maxlength='65'> </td> </tr> <tr> <td> Number: </td> <td> <input type='text' name='player_number[]' size='10' maxlength='10'> </td> </tr> <tr> <td> Years Played: </td> <td> <input type='text' name='years_played[]' size='10' maxlength='10'> </td> </tr> <tr> <td> Details: </td> <td> <textarea name='photo_caption[]' cols='50' rows='10'></textarea> </td> </tr> __HTML_END; $counter++; } // Final Output echo <<<__HTML_END <form enctype="multipart/form-data" action="a_Photo_Upload.php" method="post" name="upload_form" onsubmit="return validate_form();"> <table width='600' border='0' id='tablepadding' align='center'> <tr style="display: none;"> <td style="display: none;"> Select Category </td> <td style="display: none;"> <select name='category'> $photo_category_list </select> </td> </tr style="display: none;"> <!-Insert the photo fields here --> $photo_upload_fields <tr> <td> <input type='submit' name='submit' value='Add Bio' /> </td> </tr> </table> </form> __HTML_END; ?> And then it goes to the current page I have posted: <?php $result_final = ""; $counter = 0; $known_photo_types = array( 'image/pjpeg' => 'jpg', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/x-png' => 'png' ); $gd_function_suffix = array( 'image/pjpeg' => 'JPEG', 'image/jpeg' => 'JPEG', 'image/gif' => 'GIF', 'image/bmp' => 'WBMP', 'image/x-png' => 'PNG' ); $photos_uploaded = $_FILES['photo_filename']; $player_title = $_POST['player_title']; $player_number = $_POST['player_number']; $years_played = $_POST['years_played']; $photo_caption = $_POST['photo_caption']; while( $counter <= count($photos_uploaded) ) { mysql_query( "INSERT INTO gallery_photos(`photo_filename`,`player_title`,`player_number`,`years_played`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($player_title[$counter])."', '".addslashes($player_number[$counter])."' , '".addslashes($years_played[$counter])."', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); if($photos_uploaded['size'][$counter] > 0) { if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types)) { $result_final .= "File ".($counter+1)." is not a photo<br />"; } else { //mysql_query( "INSERT INTO gallery_photos(`photo_filename`,`player_title`,`player_number`,`years_played`, `photo_caption`, `photo_category`) VALUES('0', '".addslashes($player_title[$counter])."', '".addslashes($player_number[$counter])."' , '".addslashes($years_played[$counter])."', '".addslashes($photo_caption[$counter])."', '".addslashes($_POST['category'])."')" ); $new_id = mysql_insert_id(); $filetype = $photos_uploaded['type'][$counter]; $extention = $known_photo_types[$filetype]; $filename = $new_id.".".$extention; mysql_query( "UPDATE gallery_photos SET photo_filename='".addslashes($filename)."' WHERE photo_id='".addslashes($new_id)."'" ); copy($photos_uploaded['tmp_name'][$counter], $images_dir."/".$filename); $size = GetImageSize( $images_dir."/".$filename ); if($size[0] > $size[1]) { $old_width = $size[0]; $old_height = $size[1]; $thumbnail_width = 690; $thumbnail_height = ($old_height * $thumbnail_width / $old_width); } else { $thumbnail_width = (int)(690 * $size[0] / $size[1]); $thumbnail_height = 500; } $function_suffix = $gd_function_suffix[$filetype]; $function_to_read = "ImageCreateFrom".$function_suffix; $function_to_write = "Image".$function_suffix; $source_handle = $function_to_read ( $images_dir."/".$filename ); if($source_handle) { $destination_handle = imagecreatetruecolor( $thumbnail_width, $thumbnail_height ); ImageCopyResized( $destination_handle, $source_handle, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $size[0], $size[1] ); } $function_to_write( $destination_handle, $images_dir."/tb_".$filename ); ImageDestroy($destination_handle ); $result_final .= "<img src='".$images_dir. "/tb_".$filename."' style='margin-right: 20px; width: 100px;' />"; } } $counter++; } echo <<<__HTML_END $result_final __HTML_END; ?> What would be a better way to do this? I really just want the user to be able to upload 1 photo, player name, years played, number, and bio. I would like to be able to edit/update it as well. Stumped on this one. Any ideas? Link to comment https://forums.phpfreaks.com/topic/256370-form-does-not-work-if-photo-is-not-added/#findComment-1314834 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.