Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •