referencing $_FILES inside a custom file upload function - help please!

Discussion in 'PHP' started by gordi555, Apr 4, 2009.

  1. #1
    I'm trying to reference the $_FILES inside a function I've created like the following... (shoterned)

    
    function item_picture_upload($file, $itemid, $alt)
    {
        if($file)
        {
            $filename = $_FILES['$file']['name'];
    	echo $filename;
        }
    }
    
    PHP:
    And I call it like this...

    
    $retval = item_picture_upload($_FILE['file1'], 50, "the picture alt");
    
    PHP:
    I'm trying to reference it but not working :( I'm trying to upload the file, rename and store permantly. The syntax must be wrong I think. Any input please?
     
    gordi555, Apr 4, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $_FILES is not the same as $_FILE. That little squiggly thing at the end is a letter "S", not a worm that happened to crawn on your monitor.
     
    SmallPotatoes, Apr 4, 2009 IP
  3. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Ok, copied the source wrong, it's correct in my code - anyone any help please?
     
    gordi555, Apr 4, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Here you are passing $_FILES['file1'] which is an array:
    $retval = item_picture_upload($_FILE['file1'], 50, "the picture alt");
    Code (markup):
    And then here you are treating it like a string:
    $filename = $_FILES['$file']['name'];
    Code (markup):
    Except that '$file' (name of string inside single quotes) doesn't do anything useful at all.

    How about this:

    $retval = item_picture_upload($_FILES['file1'], 50, "the picture alt");
    Code (markup):
    function item_picture_upload($file, $itemid, $alt)
    {
        if(is_array($file) && count($file))
        {
            $filename = $file['name'];
            echo $filename;
        }
    }
    Code (markup):
     
    SmallPotatoes, Apr 4, 2009 IP
  5. ultrasonic

    ultrasonic Peon

    Messages:
    30
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You need to set this ($_FILES['$file']['name'];) outside of the function and pass it in as another variable also.
     
    ultrasonic, Apr 4, 2009 IP
  6. gordi555

    gordi555 Active Member

    Messages:
    537
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Thank you :) One of those days! Works fine now.

    Thanks again!
     
    gordi555, Apr 4, 2009 IP