Limiting file Upload Size

 
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: Sat Apr 21, 2007 4:59 pm    Post subject: Limiting file Upload Size Reply with quote

Hi

I have the 2 sections of code that work together wonderfully. However I can not work out how to restrict the size of the files that are being uploaded.

Can anybody help with any suggestions of code?

Many Thanks
Neil

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;

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'];

$limit_size=50000;

   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 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 />";
if ($uploaded_size > 350000)
      {
      echo "Your file is too large.<br>";
      $ok=0;
      }    
         }
      }
   $counter++;
   }

   // Print Result
echo <<<__HTML_END

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

__HTML_END;
?>
Back to top View user's profile Send private message Send e-mail
AbyssUnderground
-


Joined: 31 Dec 2004
Posts: 3855

PostPosted: Sat Apr 21, 2007 5:13 pm    Post subject: Reply with quote

From what I can remember, a hidden field can restrict the upload size.

Code:
<input type="hidden" name="MAX_FILE_SIZE" value="10000000">


But this can be easily overridden. I think the value is in bytes.
_________________
Andy (AbyssUnderground) (previously The Inquisitor)
www.abyssunderground.co.uk
Back to top View user's profile Send private message Visit poster's website
cmxflash
-


Joined: 11 Dec 2004
Posts: 872

PostPosted: Sat Apr 21, 2007 5:37 pm    Post subject: Reply with quote

Just edit your php.ini-file, usually the upload limit is set to 2 MB.
Back to top View user's profile Send private message
Flux
-


Joined: 13 Oct 2006
Posts: 48

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

You could also program the post-upload script to check the size.
Like, if file x > 20MB, cancel upload, etc. (Using, as I'm sure you already know, $_FILES['x']['size'])
_________________
My signature is so lame.

I think phpBB forums hates my laptop '.';
Back to top View user's profile Send private message
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