File upload using PHP.

Discussion in 'Web Design & Programming' started by Matt555, Feb 8, 2006.

  1. Matt555

    Matt555 iMod

    Likes Received:
    98
    Trophy Points:
    48
    I'm trying to get a file upload system on my site to allow users to upload smileys/graphics to be displayed on the site (and then possibly give them a link to the uploaded file), I've tried a couple of tutorials on the net but to no avail...

    The code I'm using at the minute for the upload.php file is attached.

    I'm getting this error

    File: Size:
    Fatal error: Call to undefined function: verify_uploaded_file() in /home/matt555/public_html/upload.php on line 44

    Does anyone know what I can do to get it working?

    www.matt-brunt.com/upload.htm is the upload page used. At the minute it's only set to allow jpg and jpeg file types to be uploaded but gif's and png's are going to be added soon.

    Thanks in advance - Matt

    The code used is from a tutorial as I don't really know the first thing about php. :O

    PHP:
    <?php

    // Upload.php â€“ file upload script
    // The $MOVE_TO_PATH is the COMPLETE path to the directory
    // of where the file should go. (include trailing slash.)

    $MOVE_TO_PATH =

    '/home/matt555/public_html/images/userupload/';



    // The following values are used to verify_uploaded_file()
     // as the sizes and types that can be uploaded.



    $UPLOAD_TYPES['.jpg'] = 1// Allow .jpg files

    $UPLOAD_TYPES['.jpeg'] = 2// Allow .jpeg files

    $UPLOAD_SIZES['max'] = 102400// Make sure the file is under 1MB

    $UPLOAD_SIZES['min'] = 0// in size



    echo 'File: ' $HTTP_POST_FILES['upload_file']['name'] . '
     '
    .

    'Size: ' $HTTP_POST_FILES['upload_file']['size'] . '


     '
    ;



    // Verify the size and type of the file



    $intResult verify_uploaded_file(

    $HTTP_POST_FILES['upload_file']['name'],

    $HTTP_POST_FILES['upload_file']['size']);



    // The file is not acceptable

    if ($intResult != 1) {

    $msg_base $HTTP_POST_FILES['upload_file']['name'] .

    ' is unacceptable.


     '
    ;



    //die() and display error message



    if ($intResult == -1) {
     die(
    $msg_base 'Reason: File size is too large.');
     } elseif (
    $intResult == -2) {
     die(
    $msg_base 'Reason: File type is not allowed.');
     };



    // The file has been accepted; verify it and move it to the server :)

    if (! move_uploaded_file($HTTP_POST_FILES['upload_file']['tmp_name'], $MOVE_TO_PATH $HTTP_POST_FILES['upload_file']['name']) ) {
     die(
    'You did not upload a file or the file could not be moved to ' .

    $MOVE_TO_PATH $HTTP_POST_FILES['upload_file']['name']);
     } else {
     echo
    $HTTP_POST_FILES['upload_file']['name'] . ' was uploaded successfully.';
     };


     function
    verify_uploaded_file($strName$intSize) {

    /* $strName and $intSize are taken from
     the uploaded file's information.
     Also, make sure that the global variable $UPLOAD_SIZES
     and $UPLOAD_TYPES are defined prior to calling
     this function. */


     // Check file size



    if ($intSize $GLOBALS['UPLOAD_SIZES']['min'] || $intSize $GLOBALS['UPLOAD_SIZES']['max']) {
     return -
    1;
     };



    // Check file type

    $arrSegments split('[.]'$strName); // may contain multiple dots

    $strExtension $arrSegments[count($arrSegments) - 1];
     if (
    $GLOBALS['UPLOAD_TYPES'][strtoupper($strExtension)] != 1) {
     return -
    2// File type not defined/allowed

    };



    // All tests have passed; this file is valid

    return 1;
    }
    }
    ?>
     

    Attached Files:

  2. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    The error is suggesting you haven't declared the verify_uploaded_file() function in php, so you'll need to write it yourself or check your tutorials for any code you've missed.
     
  3. Matt555

    Matt555 iMod

    Likes Received:
    98
    Trophy Points:
    48
    Okay, I think I'll leave it for now, I've read many tutorials today and have had no success on any of them. I'll probably just leave it for now (unless anyone comes up with a solution)
     
  4. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    Actually I just saw your verify function, I'll have a look at it tomorrow. :)
     
  5. Matt555

    Matt555 iMod

    Likes Received:
    98
    Trophy Points:
    48
    Thanks very much, your help is greatly appreciated :)
     
  6. Sniper

    Sniper Administrator Staff Member

    Likes Received:
    59
    Trophy Points:
    63
    I think I am have simple upload script somewhere, will get back to you.
     
  7. Matt555

    Matt555 iMod

    Likes Received:
    98
    Trophy Points:
    48
    Thanks, that would help so much!
     
  8. Sniper

    Sniper Administrator Staff Member

    Likes Received:
    59
    Trophy Points:
    63
  9. Addis

    Addis The King

    Likes Received:
    91
    Trophy Points:
    48
    My PHP is rusty as I don't use it often, but if I'm correct the reason you're getting a function undefined error is because you haven't put the function code above the function call. The php processor executes the code line by line and so it would not know of the verify function. Just my thoughts as in C++ you would get the same type of error unless you used prototyping...

    So to summarise, try putting the function verify....{code} bit at the top of your code before its called so the processor knows what the function is.
     
  10. Matt555

    Matt555 iMod

    Likes Received:
    98
    Trophy Points:
    48
    Thanks for the input, I've tried a fair few tutorials and still no luck, I may just leave it for now, I think somewhere I saw a tutorial on sending files through a form that sends the results to your e-mail, I may try and find that soon.
     

Share This Page