Hi,

function showImage($img){

print $_files['$img']['name'];

}

if ( isset($_FILES['fupload']['name']) ) {

$imgName=$_FILES['fupload']['name'];
showImage($imgName);

}

throwing error undefined index $img. How can i access $_files properties within a function.

fupload is name file type in form.

Thanks in advance.

    add

    global $_FILES['$img']['name'];

    at the top, inside your function.

    i.e

    <?php
    function showImage($img){ 
         global $_FILES['$img']['name'];
         print $_FILES['$img']['name']; 
    } 
    
    if ( isset($_FILES['fupload']['name']) ) { 
         $imgName=$_FILES['fupload']['name']; 
         showImage($imgName); 
    } 
    
    ?>

      Actually, $FILES is a superglobal array, like $POST and $GET
      $
      files on the other hand, isnt.

        So, since $FILES and $POST etc... are superglobals, they dont need to be called for by global in functions like regular variables?

          That's correct. But, like all variables, they are case-sensitive. Which is why joe2's function won't work.

          Actually, I don't think his code as a whole will work, because the first index of "$FILES" is passed wrongly to the function. Not to mention the image probably hasn't been moved to permanent storage yet, or that no path to the temporary storage directory is supplied, or that it will be stored there under the name "$FILES['fupload']['tmp_name']".

            Write a Reply...