View Full Version : Uploading in PHP

07-08-2005, 04:41 PM
ok, I've found a nice little script for uploading, and it does work.
but i have two questions about this script


$uploadpath = 'C:/Program Files/Apache Group/Apache2/htdocs/pics/images/';
$source = $HTTP_POST_FILES['file1']['tmp_name'];
$dest = '';

if ( ($source != 'none') && ($source != '' )) {

$imagesize = getimagesize($source);

switch ( $imagesize[2] ) {

case 0:

echo '<BR> Image is unknown <BR>';

case 1:
echo '<BR> Image is a GIF <BR>';
$dest = $uploadpath.uniqid('img').'.gif';

case 2:
echo '<BR> Image is a JPG <BR>';
$dest = $uploadpath.uniqid('img').'.jpg';

case 3:
echo '<BR> Image is a PNG <BR>';
$dest = $uploadpath.uniqid('img').'.png';


if ( $dest != '' ) {

if ( move_uploaded_file( $source, $dest ) ) {

echo 'File successfully stored.<BR>';

} else {

echo 'File could not be stored.<BR>';



} else {

echo 'File not supplied, or file too big.<BR>';



First, this script uploading the file with a uniqid, what i want to know is how can i get this id while i'm uploading, so i can add the picture name to the db?

also it looks like this script allows files to be limited by size, but i don't see how to set the size limit. any ideas?

thanks in advance for your time!

07-08-2005, 09:38 PM
For the uniqid question, simply give it a variable instead:

$unique_id = uniqid('img');
$dest = $uploadpath . $unique_id . '{TYPE}';

$unique_id is now carrying your uniqid value.
As for your size, I don't see it implimented anywhere within the script. Implimentation would be extremely simple to do for you though, specify yourself a hardcoded or php variable -> do not rely on MAX_FILE_SIZE.

$max_upload_size = 1024; // 1kb, yeah I know... small lol.

if (($source != 'none' && $source != '' ) AND $_FILES['file1']['size'] <= $max_upload_size) {
/*processing code follows */

Hope that helps. Oh, btw, change your $HTTP_POST_FILES to $_FILES. Unless of course your php version is less than 4.1.0. Then leave it.