xmshanx
September 9, 2011, 10:00am
21
This is how I added what you gave me early
<?php
include("config.inc.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'
);
// Fetch the photo array sent by preupload.php
$photos_uploaded = $_FILES['photo_filename'];
// Fetch the photo caption array
$photo_caption = $_POST['photo_caption'];
echo "photo_caption: "; print_r($photo_caption); echo "<br />";
echo "counter: $counter<br />";
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_category`) VALUES('0', '".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;
}
// 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 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);
ImageDestroy($destination_handle );
$result_final .= "<img src='".$images_dir. "/tb_".$filename."' /> File ".($counter+1)." Added<br />";
}
}
$counter++;
}
// Print Result
?>
<html>
<head>
<title>Photos uploaded</title>
</head>
<body>
<?php echo $result_final;?>
</body>
</html>
I had you echo out the wrong array
It should have been
echo "photos_uploaded: "; print_r($photos_uploaded ); echo "<br />";
But anyway, looking at the output you posted, it seems that the error occurs in the first passing of the while loop.
$counter is 0, and the error says the undefined offset is 5, so the error shouldn’t have anything to do with $counter.
The error is displayed before ‘File 1 added’, so it must be somewhere in this part of the code:
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_category`) VALUES('0', '".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;
}
// 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 );
// 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."' /> File ".($counter+1)." Added<br />";
}
I can’t see the error right away. Do you mind adding the line I posted at the beginning ($photos_uploaded) and repost the result here?
xmshanx
September 9, 2011, 9:20am
23
photo_caption: Array ( [0] => [1] => [2] => [3] => [4] => )
counter: 0
Notice: Undefined offset: 5 in E:\\Usb Web Server v7.0\\Root\\gallery\\upload.php on line 37
File 1 Added
I put some code tags around the code you posted. It makes it easier to read. Please do so next time you have to post code.
Please add the following lines (indicated in red) to your upload.php, test it, and post the result here.
// Fetch the photo caption array
$photo_caption = $_POST['photo_caption'];
[B][COLOR="Red"]echo "photo_caption: "; print_r($photo_caption); echo "<br />";[/COLOR][/B]
while( $counter <= count($photos_uploaded) )
{
[B][COLOR="Red"]echo "counter: $counter<br />";[/COLOR][/B]
if($photos_uploaded['size'][$counter] > 0)
xmshanx
September 9, 2011, 7:53am
25
here is the TUTORIAL Build An Automated PHP Gallery System In Minutes » SitePoint I didnt change a single thing but I just PREUPLOAD.PHP because there are some problems
<?php
include 'config.inc.php';
// initialization
$photo_upload_fields = '';
$counter = 1;
// If we want more fields, then use, preupload.php?number_of_fields=20
$number_of_fields = (isset($_GET['number_of_fields'])) ?
(int)($_GET['number_of_fields']) : 5;
// Lets build the Image Uploading fields
while($counter <= $number_of_fields) {
$photo_upload_fields .="
<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>
";
$counter++;
}
// Final Output
?>
<html>
<head>
<title>Lets upload Photos</title>
</head>
<body>
<!-- CATEGORY CREATING PART-->
<form name="form" method="post" >
<table border='0' align='center' >
<tr> <td>Category Name:</td><td>
<input type="text" name="name" />
<input type="submit" name="Submit" value="Submit">
</td></tr></table>
</form>
<!-- CATEGORY CREATING PART-->
<?php
//CATEGORY CREATING PART
if(isset($_POST['Submit'])){
$name = $_POST['name'];
if (empty($name)) {
echo('Please Fill the Category <meta http-equiv=Refresh content="2;url="">');
}else{
// Refresh page 1 time
if (!isset($_GET['reload'])) {
echo '<meta http-equiv=Refresh content="0;url=">';
}
$sql="INSERT INTO gallery_category(`category_name`) VALUES('$name')";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful Category Created";
}else {
echo "ERROR";
}
}
}
?>
<form enctype='multipart/form-data' action='upload.php' method='post' name='upload_form'>
<table border='0' align='center'>
<tr><td>
Select Category
<select name='category'>
<?php
$result = mysql_query('SELECT category_id,category_name FROM gallery_category');
while($row = mysql_fetch_array($result)) {
echo "<option value='$row[0]'>$row[1]</option>";
}
mysql_free_result( $result );
?>
</select>
</td></tr>
<?php echo $photo_upload_fields ;?>
<tr><td>
<input type='submit' name='submit' value='Add Photos' />
</td></tr>
</table>
</form>
</body>
</html>
<?php echo // close connection
mysql_close(); ?>
xmshanx
September 9, 2011, 7:51am
26
<?php
include("config.inc.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'
);
// Fetch the photo array sent by preupload.php
$photos_uploaded = $_FILES['photo_filename'];
// Fetch the photo caption array
$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`, `photo_caption`, `photo_category`) VALUES('0', '".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;
}
// 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 );
// 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."' /> File ".($counter+1)." Added<br />";
}
}
$counter++;
}
// Print Result
?>
<html>
<head>
<title>Photos uploaded</title>
</head>
<body>
<?php echo $result_final;?>
</body>
</html>
here is my whole code
Welcome to the SP forums.
Can you post more code?
And try to do a print_r($photos_uploaded); just before that if
xmshanx
September 12, 2011, 11:35am
28
Then why its not working and displaying an error according to the tutorial there is no such two codes but in his archive files… How to SOLVE this???
xmshanx
September 13, 2011, 8:16pm
29
my problem is solved by myself
$cid = (int)(isset($_GET['cid']));
$pid = (int)(isset($_GET['pid']));
used “isset”