Getting Values from uploaded file

 
Post new topic   Reply to topic    Aprelium Forum Index -> PHP
View previous topic :: View next topic  
Author Message
vigar_neil
-


Joined: 10 May 2006
Posts: 5
Location: manchester

PostPosted: Sun Apr 22, 2007 12:16 pm    Post subject: Getting Values from uploaded file Reply with quote

Hi

I have the two sections of code below, which I am thrying to us to upload a file to my server. This works ok but I also want it to recognise the file name, size and type so that I can give appropriate responces to my customers. However it is not echoing the values for name, size ot type and I don't know why.

Can anybody see where I may be going wrong?

Any help would be greatly appriciated.

Thanks
Neil

Preupload code
Code:

<?php
 include 'config.inc.php';
include 'design.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']) : 2;

 // Firstly Lets build the Category List
                   
$clubname45 = $clubinfo['ClubName'];


// gallery_photos WHERE photo_category='".addslashes($cid)."'" );
 $result = mysql_query("SELECT clubid,clubname FROM clubs WHERE clubname='".addslashes($clubname45)."' ");
//$sql = 'SELECT * FROM `gallery_photos` WHERE `photo_category` = 1 LIMIT 0, 30 ';
 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 Image 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>
 Photo Title:
 <textarea name="photo_tittle[]" cols="30"
   rows="1"></textarea>
</td></tr>
<tr><td>
 Description:
 <textarea name="photo_description[]" cols="30"
   rows="3"></textarea>
</td></tr>
<tr><td>
Questions:
 <textarea name="questions[]" cols="30"
   rows="3"></textarea>
</td></tr>
<tr><td>
 Posted by:
 <textarea name="posted_by[]" cols="30"
   rows="1"></textarea>


<br/><br/></td></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>
   <!-Insert the image 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 code

Code:

<table width="50%"  border="5" cellspacing="0" cellpadding="10">
                <tr>
                  <td colspan="2"><?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_tittle'];
   $photo_description = $_POST['photo_description'];
   $questions = $_POST['questions'];
   $posted_by = $_POST['posted_by'];

echo "<tr><td>Client Filename: </td><td>" . $_FILES['photo_filename']["name"] . "</td></tr>";
echo "<tr><td>File Type: </td><td>" . $_FILES['photo_filename']["type"] . "</td></tr>";
echo "<tr><td>File Size: </td><td>" .$_FILES['photo_filename']["size"] . " Kb</td></tr>";

   while( $counter <= count($photos_uploaded) )
   {
   
if($photos_uploaded['size'][$counter] > 0)
      {
         if(!array_key_exists($photos_uploaded['type'][$counter], $known_photo_types))
         {
            $result_final .= "photo_filename".($counter+1)." is not a photo please go back to the photos upload page and try again.<br />";
         }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//if($_FILES['photo_filename']["size"] > 1000000000)
  //{
      //$result_final .= "File ".($counter+1)." is too big please go back to the photos upload page and try again.<br />";

  //}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

         else
         {
            mysql_query( "INSERT INTO gallery_photos(`photo_filename`, `photo_tittle`, `photo_description`, `posted_by`, `questions`, `photo_category`) VALUES('0', '".addslashes($photo_caption[$counter])."', '".addslashes($photo_description[$counter])."', '".addslashes($posted_by[$counter])."', '".addslashes($questions[$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
echo <<<__HTML_END

<html>
<head>
   <title>Photos uploaded</title>
</head>
<body>
   $result_final
</body>
</html>

__HTML_END;
?>
</td>
Back to top View user's profile Send private message Send e-mail
Flux
-


Joined: 13 Oct 2006
Posts: 48

PostPosted: Sun Apr 22, 2007 5:40 pm    Post subject: Reply with quote

(nevermind)
_________________
My signature is so lame.

I think phpBB forums hates my laptop '.';
Back to top View user's profile Send private message
aprelium
-


Joined: 22 Mar 2002
Posts: 6800

PostPosted: Mon Apr 23, 2007 5:05 pm    Post subject: Re: Getting Values from uploaded file Reply with quote

vigar_neil,

You script does no error checks. The upload could have failed and these variable were not filled.

Test $_FILES['userfile']['error'] before proceeding with echoing the uploaded file information (see http://www.php.net/features.file-upload for more details).
_________________
Support Team
Aprelium - http://www.aprelium.com
Back to top View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    Aprelium Forum Index -> PHP All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB phpBB Group