Hey benrud,
Sorry I was in a rush when I last posted upload.php .. wasnt really taking any notice of what I was doing ..
Replace preupload.php and upload.php with the following codes .. Should work this time ..
preupload.php:
PHP Code:
<?php
include("security.inc.php");
include("config.inc.php");
// initialization
$photo_upload_fields = "";
$counter = 1;
// default number of fields
$number_of_fields = 5;
// If you want more fields, then the call to this page should be like,
// preupload.php?number_of_fields=20
if( $_POST['number_of_fields'] )
$number_of_fields = (int)($_POST['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>
Photo {$counter}:
<input name=' photo_filename[]' type='file' />
</td>
</tr>
<tr>
<td>
Caption:
<textarea name='photo_caption[]' cols='30' rows='1'></textarea>
</td>
</tr>
<td>
Photo Meta Key:
<textarea name='photo_meta_key[]' cols='30' rows='1'></textarea>
</td>
</tr>
<td>
Photo meta description:
<textarea name='photo_meta_description[]' cols='30' rows='1'></textarea>
</td>
</tr>
<td>
Photo Title:
<textarea name='photo_title[]' cols='30' rows='1'></textarea>
</td>
</tr>
<td>
Photo Products:
<textarea name='photo_products[]' cols='30' rows='1'></textarea>
</td>
</tr>
</tr>
</tr>
</tr>
__HTML_END;
$counter++;
}
// Final Output
echo <<<__HTML_END
<html>
<head>
<title>Lets upload Photos</title>
</head>
<body>
<form enctype='multipart/form-data' action='upload.php' method='post' name='upload_form'>
<table width='90%' border='0' align='center' style='width: 90%;'>
<tr>
<td>Select Category: <select name='category'>$photo_category_list</select></td>
</tr>
<tr>
<td>
<p> </p>
</td>
</tr>
<!-Insert the photo fields here -->
$photo_upload_fields
<tr>
<td>
<input type='submit' name='submit' value='Add Photos' />
</td>
</tr>
</table>
</form>
</body>
</html>
__HTML_END;
?>
upload.php:
PHP Code:
<?php
include("config.inc.php");
include("design.inc.php");
include("security.inc.php");
// initialization
$result_final = "";
$counter = 0;
// List of our known photo types
$known_photo_types = array(
'image/pjpeg' => 'jpg',
'image/jpeg' => 'jpg',
'image/bmp' => 'bmp',
'image/x-png' => 'png');
// GD Function Suffix List
$gd_function_suffix = array(
'image/pjpeg' => 'JPEG',
'image/jpeg' => 'JPEG',
'image/bmp' => 'WBMP',
'image/x-png' => 'PNG');
// Fetch the photo array sent by preupload.php
$photos_uploaded = $_FILES['photo_filename'];
$photoFileName= $_FILES['photo_filename'];
// Fetch the photo caption array
$photo_caption = $_POST['photo_caption'];
$photo_meta_key = $_POST['photo_meta_key'];
$photo_meta_description = $_POST['photo_meta_description'];
$photo_title = $_POST['photo_title'];
$photo_products = $_POST['photo_products'];
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`, `photo_caption`, `photo_meta_key`, `photo_meta_description`, `photo_title`, `photo_products`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($photo_meta_key[$counter])."', '".addslashes($photo_meta_description[$counter])."', '".addslashes($photo_title[$counter])."', '".addslashes($photo_products[$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 original image size
$size = GetImageSize( $images_dir."/".$filename );
// First Create Thumbnail!
// Thumbnail Settings
$Config_tbwidth_wide = 150; // width of wide image
$Config_tbheight_wide = 125; // height of wide image
$Config_tbwidth_tall = 125; // width of tall image
$Config_tbheight_tall = 150; // height of tall image
// The Code
if($size[0] > $size[1]){
$thumbnail_width = $Config_tbwidth_wide;
$thumbnail_height = (int)($Config_tbwidth_wide * $size[1] / $size[0]);
if($thumbnail_height > $Config_tbheight_wide){
$thumbnail_height = $Config_tbheight_wide;
$thumbnail_width = (int)($Config_tbheight_wide * $size[0] / $size[1]);
}
}else{
$thumbnail_width = (int)($Config_tbheight_tall * $size[0] / $size[1]);
$thumbnail_height = $Config_tbheight_tall;
if($thumbnail_width > $Config_tbwidth_tall){
$thumbnail_width = $Config_tbwidth_tall;
$thumbnail_height = (int)($Config_tbwidth_tall * $size[1] / $size[0]);
}
}
// Build Thumbnail with GD 2.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 a blank image for the thumbnail
$destination_handle = ImageCreateTrueColor ( $thumbnail_width, $thumbnail_height );
// Now we resize it
ImageCopyResampled( $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, 100 );
ImageDestroy($destination_handle );
// .................................................................................
// Lets resize the image if its width is greater than 500 pixels
// Resized image settings
if ($size[0] > '500'){
$Config_width_wide = 500; // width of wide image
$Config_height_wide = 475; // height of wide image
$Config_width_tall = 475; // width of tall image
$Config_height_tall = 500; // height of tall image
// The Code
if($size[0] > $size[1]){
$image_width = $Config_width_wide;
$image_height = (int)($Config_width_wide * $size[1] / $size[0]);
if($image_height > $Config_height_wide){
$image_height = $Config_height_wide;
$image_width = (int)($Config_height_wide * $size[0] / $size[1]);
}
}else{
$image_width = (int)($Config_height_tall * $size[0] / $size[1]);
$image_height = $Config_height_tall;
if($image_width > $Config_width_tall){
$image_width = $Config_width_tall;
$image_height = (int)($Config_width_tall * $size[1] / $size[0]);
}
}
// Build image with GD 2.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 a blank image for the image
$destination_handle = ImageCreateTrueColor ( $image_width, $image_height );
// Now we resize it
ImageCopyResampled( $destination_handle, $source_handle, 0, 0, 0, 0, $image_width, $image_height, $size[0], $size[1] );
}
// Let's save the image
$function_to_write( $destination_handle, $images_dir."/".$filename, 90 );
ImageDestroy($destination_handle );
}
$result_final .= "<img src='".$images_dir. "/tb_".$filename."' /><br />File ".($counter+1)." Added Successfully!<br /><br />";
}
}
$counter++;
}
// Print Result
echo <<<__HTML_END
<html>
<head>
<title>Photos uploaded</title>
</head>
<body>
$design_header<br>
$result_final<br>
<a href="adminindex.php">Back to Administration Page</a><br>
<a href="logout.php">Logout of Administration Area</a><br>
$design_footer
</body>
</html>
__HTML_END;
?>
Cheers
Bookmarks