Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-08-2005, 03:41 PM   PM User | #1
angst
Senior Coder

 
angst's Avatar
 
Join Date: Apr 2004
Location: Toronto, Ontario
Posts: 2,112
Thanks: 15
Thanked 122 Times in 122 Posts
angst is on a distinguished road
Question Uploading in PHP

ok, I've found a nice little script for uploading, and it does work.
but i have two questions about this script

PHP Code:

<?php

    $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>';
                break;

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

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

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

        }

        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!
-Ken
angst is offline   Reply With Quote
Old 07-08-2005, 08:38 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
For the uniqid question, simply give it a variable instead:
PHP Code:

$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.
PHP Code:

$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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:30 PM.


Advertisement
Log in to turn off these ads.