Photogallery tutorial [PROBLEM]

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 :blush:
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] &gt; 0)

        {
            if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))  
   
            { 
                $result_final .= "File ".($counter+1)." is not a photo&lt;br /&gt;";
            }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] &gt; $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 .= "&lt;img src='".$images_dir. "/tb_".$filename."' /&gt; File ".($counter+1)." Added&lt;br /&gt;";
            }

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?

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)

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


&lt;?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 &lt;= $number_of_fields) {  
    $photo_upload_fields .="  
&lt;tr&gt;&lt;td&gt;  
  Photo {".$counter.":  
 &lt;input name='photo_filename[]'type='file' /&gt;  &lt;/td&gt;&lt;/tr&gt;  
&lt;tr&gt;&lt;td&gt;  
  Caption:  &lt;textarea name='photo_caption[]' cols='30'  rows='1'&gt;&lt;/textarea&gt;  
&lt;/td&gt;&lt;/tr&gt;  
";  
    $counter++;  
  }  

  // Final Output  
  ?&gt;
&lt;html&gt;  
&lt;head&gt;  
&lt;title&gt;Lets upload Photos&lt;/title&gt;  
&lt;/head&gt;  
&lt;body&gt;  

&lt;!-- CATEGORY CREATING PART--&gt;


&lt;form name="form" method="post" &gt;
 &lt;table border='0'  align='center' &gt;  
    &lt;tr&gt; &lt;td&gt;Category Name:&lt;/td&gt;&lt;td&gt;  
&lt;input type="text" name="name" /&gt;
&lt;input type="submit" name="Submit" value="Submit"&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;

&lt;/form&gt;



&lt;!-- CATEGORY CREATING PART--&gt;
&lt;?php 


//CATEGORY CREATING PART

if(isset($_POST['Submit'])){
	
$name = $_POST['name'];	
	
if (empty($name)) {
echo('Please Fill the Category &lt;meta http-equiv=Refresh content="2;url=""&gt;');

}else{
	

// Refresh page 1 time	
if (!isset($_GET['reload'])) {
echo '&lt;meta http-equiv=Refresh content="0;url="&gt;';
}	




	

$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";
}
}
}
?&gt;






&lt;form enctype='multipart/form-data'   action='upload.php' method='post'  name='upload_form'&gt;  
  &lt;table  border='0'  align='center'&gt;  
    &lt;tr&gt;&lt;td&gt;  
      Select Category  
      
&lt;select name='category'&gt;


&lt;?php  

$result = mysql_query('SELECT category_id,category_name FROM gallery_category');  
  while($row = mysql_fetch_array($result)) { 
  
   echo "&lt;option value='$row[0]'&gt;$row[1]&lt;/option&gt;"; 
   
    }  
  mysql_free_result( $result );  
  
   ?&gt;
   
   
   
   &lt;/select&gt; 
   
    &lt;/td&gt;&lt;/tr&gt;  
	&lt;?php echo  $photo_upload_fields ;?&gt;
    &lt;tr&gt;&lt;td&gt;  
      &lt;input type='submit' name='submit'  value='Add Photos' /&gt; 
    
      
      

    &lt;/td&gt;&lt;/tr&gt;  
  &lt;/table&gt;  
&lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;  

&lt;?php echo // close connection
mysql_close(); ?&gt;
&lt;?php
	include("config.inc.php");

	// initialization
	$result_final = "";
	$counter = 0;

	// List of our known photo types
	$known_photo_types = array( 
						'image/pjpeg' =&gt; 'jpg',
						'image/jpeg' =&gt; 'jpg',
						'image/gif' =&gt; 'gif',
						'image/bmp' =&gt; 'bmp',
						'image/x-png' =&gt; 'png'
					);
	
	// GD Function List
	$gd_function_suffix = array( 
						'image/pjpeg' =&gt; 'JPEG',
						'image/jpeg' =&gt; 'JPEG',
						'image/gif' =&gt; 'GIF',
						'image/bmp' =&gt; 'WBMP',
						'image/x-png' =&gt; '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 &lt;= count($photos_uploaded) )
	{

		if($photos_uploaded['size'][$counter] &gt; 0)

		{
			if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))  
   
			{ 
				$result_final .= "File ".($counter+1)." is not a photo&lt;br /&gt;";
			}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] &gt; $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 .= "&lt;img src='".$images_dir. "/tb_".$filename."' /&gt; File ".($counter+1)." Added&lt;br /&gt;";
			}
		}
	$counter++;
	}

	// Print Result
?&gt;

&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Photos uploaded&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php 	echo $result_final;?&gt;
&lt;/body&gt;
&lt;/html&gt;

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

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???

my problem is solved by myself


$cid = (int)(isset($_GET['cid']));
$pid = (int)(isset($_GET['pid']));

used “isset”