$_FILE in a function

Problems with the Windows version of XAMPP, questions, comments, and anything related.

$_FILE in a function

Postby ken_mayer » 14. October 2015 14:37

Briefly, have a function for file validation (that I grabbed from a website and tinkered with). The code works fine if I embed it in a form's PHP code to process things. If, however, I use the PHP include() function, the $_FILE global variable does not seem to work. If I put the function directly into the code (effectively destroying the usefulness of a function library) as pure non-function code, it appears to work. If I insert the function into the PHP file as a function it doesn't work. What seems to be at issue is that the $_FILE variable seems to be empty. To test this in the function I inserted this command:

Code: Select all
echo "isset function test: " . isset( $_FILES['filename'] ) . "<br />";


Which returns a blank value after the text, which tells me that nothing is there. The value of "filename" is passed as an argument to the function. The full form and PHP code in one .php file is below ...

I get a series of errors about an undefined index throughout the code:
Code: Select all
Notice: Undefined index: $filename in C:\xampp\htdocs\players\testFileUpload.php on line 19

Notice: Undefined index: $filename in C:\xampp\htdocs\players\testFileUpload.php on line 24

Notice: Undefined index: extension in C:\xampp\htdocs\players\testFileUpload.php on line 26

Notice: Undefined index: $filename in C:\xampp\htdocs\players\testFileUpload.php on line 35

Notice: Undefined index: $filename in C:\xampp\htdocs\players\testFileUpload.php on line 40

Warning: getimagesize(): Filename cannot be empty in C:\xampp\htdocs\players\testFileUpload.php on line 40

Notice: Undefined index: $filename in C:\xampp\htdocs\players\testFileUpload.php on line 46


Am hoping someone can point me in the right direction. As noted, the only way I can get this to work is to hardcode the filename and path to upload to, which defeats the purpose of re-usable code. Thanks in advance -- Ken

Here's the actual form and code (hope this isn't too long):

Code: Select all
 <?php

//   include("includes/fileUpload.php");
 function fileUpload( $filename = null, $uploadpath = null )
{
   echo "Inside function<br />";
   echo "filename argument: " . $filename . "<br />";
   echo "uploadpath argument: " . $uploadpath . "<br />";
   
   //Set default file extension whitelist
   $whitelist_ext = array('jpg','png','gif','bmp');
   //Set default file type whitelist
   $whitelist_type = array('image/jpeg', 'image/png','image/gif', 'image/bmp' );
   $max_size = 5000000; // 5MB
   $error_message = "";

echo "isset function test: " . isset( $_FILES['filename'] ) . "<br />";

   if( $_FILES['$filename']['size'] > $max_size )
   {
      $error_message .= "File size should not exceed 5MB <br />";
   }

   $file_info = pathinfo($_FILES['$filename']['name']);
   $name = $file_info['filename'];
   $ext = $file_info['extension'];

   //Check file has the right extension           
   if (!in_array($ext, $whitelist_ext))
   {
      $error_message .= "Invalid file Extension <br />";
   }
   
   //Check that the file is of the right type
   if (!in_array($_FILES['$filename']['type'], $whitelist_type))
   {
      $error_message .= "Invalid file Type <br />";
   }
   
   if (!getimagesize($_FILES['$filename']['tmp_name']))
   {
      $error_message .= "Uploaded file is not a valid image <br />";
   }
   
   // everything checks out:
   if ( move_uploaded_file($_FILES['$filename']['tmp_name'], $uploadpath.$filename ))
   {
      $error_message = "Success!";
   }
   else
   {
      $error_message .= "File didn't upload ...";
   }
   
   return $error_message;
}
   //paths for images:
   $armory_path = "../actor_armory/";
?>
<!-- test file upload code ... -->
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta http-equiv="Content-Type" content="text/html" charset=iso-8859-1" />
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
      <title>Test File Upload Function</title>     
     
            <!-- our personal style sheet -->
      <link rel="stylesheet" href="css/myStyles.css">

   </head>
   
   <body>

      <h2>Test file upload function</h2>
     
      <form class="form-horizontal profile_form" method="post" enctype="multipart/form-data" action="">
         <div class="form-group file-upload">
            <label for="armory">Armory:</label>
            <p class="help-block">Upload file for image of armory.</p>
            <input type="file" id="armory" name="armory" />
         </div>
         <br />
         <center>
            <input type="submit" name="submit" class="btn btn-success btn-lg" value="Save Changes" />
         </center>

      </form>
     
      <hr />
   </body>
   
</html>

<?php

   if( isset( $_POST['submit'] ) )
   {
     
      // image:
      $iArmory         = $_FILES['armory']['name'];
      $iArmory_temp    = $_FILES['armory']['tmp_name'];
     
   echo "Filename: " . $iArmory;
   
      // attempt upload:
      fileUpload( $iArmory, $armory_path );
      //$msg = fileUpload( $_FILES['armory']['name'], $armory_path );
      // display message:
//      echo $msg;
   }
?>
ken_mayer
 
Posts: 9
Joined: 14. October 2015 14:26
Operating System: Windows 10

Re: $_FILE in a function

Postby Nobbie » 14. October 2015 14:59

This is very basic PHP knowledge and a very simple error: you MUST NOT put variable names between single quotes, these are not replaced by their values but taken as a string:

WRONG:

Code: Select all
if ( move_uploaded_file($_FILES['$filename']['tmp_name'], $uploadpath.$filename ))


CORRECT:

Code: Select all
if ( move_uploaded_file($_FILES[$filename]['tmp_name'], $uploadpath.$filename ))
Nobbie
 
Posts: 13170
Joined: 09. March 2008 13:04

Re: $_FILE in a function

Postby ken_mayer » 14. October 2015 15:11

I'll take a look. I was using the code as provided at one point, and the person who posted it used the quotes.

For what it's worth, I took out the quotes, and I get the exact same errors.
ken_mayer
 
Posts: 9
Joined: 14. October 2015 14:26
Operating System: Windows 10

Re: $_FILE in a function

Postby Altrea » 14. October 2015 15:28

The exact same error messages? word by word?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: $_FILE in a function

Postby ken_mayer » 14. October 2015 15:29

Yes, word-for-word ...
ken_mayer
 
Posts: 9
Joined: 14. October 2015 14:26
Operating System: Windows 10

Re: $_FILE in a function

Postby Altrea » 14. October 2015 15:30

I would say this is impossible if you have done the suggested change at every occurance
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: $_FILE in a function

Postby ken_mayer » 14. October 2015 15:33

Of course I have. Here's the code. Please *try* it if you can ... it's simple enough:

Code: Select all
<?php

//   include("includes/fileUpload.php");
 function fileUpload( $filename = null, $uploadpath = null )
{
   echo "Inside function<br />";
   echo "filename argument: " . $filename . "<br />";
   echo "uploadpath argument: " . $uploadpath . "<br />";
   
   //Set default file extension whitelist
   $whitelist_ext = array('jpg','png','gif','bmp');
   //Set default file type whitelist
   $whitelist_type = array('image/jpeg', 'image/png','image/gif', 'image/bmp' );
   $max_size = 5000000; // 5MB
   $error_message = "";

echo "isset function test: " . isset( $_FILES[$filename] ) . "<br />";

   if( $_FILES[$filename]['size'] > $max_size )
   {
      $error_message .= "File size should not exceed 5MB <br />";
   }

   $file_info = pathinfo($_FILES[$filename]['name']);
   $name = $file_info['filename'];
   $ext = $file_info['extension'];

   //Check file has the right extension           
   if (!in_array($ext, $whitelist_ext))
   {
      $error_message .= "Invalid file Extension <br />";
   }
   
   //Check that the file is of the right type
   if (!in_array($_FILES[$filename]['type'], $whitelist_type))
   {
      $error_message .= "Invalid file Type <br />";
   }
   
   if (!getimagesize($_FILES[$filename]['tmp_name']))
   {
      $error_message .= "Uploaded file is not a valid image <br />";
   }
   
   // everything checks out:
   if ( move_uploaded_file($_FILES[$filename]['tmp_name'], $uploadpath.$filename ))
   {
      $error_message = "Success!";
   }
   else
   {
      $error_message .= "File didn't upload ...";
   }
   
   return $error_message;
}
   //paths for images:
   $armory_path = "../actor_armory/";
?>
<!-- test file upload code ... -->
<!DOCTYPE html>
<html lang="en">
   <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta http-equiv="Content-Type" content="text/html" charset=iso-8859-1" />
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
      <title>Test File Upload Function</title>     
     
            <!-- our personal style sheet -->
      <link rel="stylesheet" href="css/myStyles.css">

   </head>
   
   <body>

      <h2>Test file upload function</h2>
     
      <form class="form-horizontal profile_form" method="post" enctype="multipart/form-data" action="">
         <div class="form-group file-upload">
            <label for="armory">Armory:</label>
            <p class="help-block">Upload file for image of armory.</p>
            <input type="file" id="armory" name="armory" />
         </div>
         <br />
         <center>
            <input type="submit" name="submit" class="btn btn-success btn-lg" value="Save Changes" />
         </center>

      </form>
     
      <hr />
   </body>
   
</html>

<?php

   if( isset( $_POST['submit'] ) )
   {
     
      // image:
      $iArmory         = $_FILES['armory']['name'];
      $iArmory_temp    = $_FILES['armory']['tmp_name'];
     
   echo "Filename: " . $iArmory;
   
      // attempt upload:
      fileUpload( $iArmory, $armory_path );
      //$msg = fileUpload( $_FILES['armory']['name'], $armory_path );
      // display message:
//      echo $msg;
   }
?>
ken_mayer
 
Posts: 9
Joined: 14. October 2015 14:26
Operating System: Windows 10

Re: $_FILE in a function

Postby Altrea » 14. October 2015 15:36

The message is different, as i said:

Filename: 01a01 - starting_xampp_shell.PNGInside function
filename argument: 01a01 - starting_xampp_shell.PNG
uploadpath argument: ../actor_armory/
isset function test:

Notice: Undefined index: 01a01 - starting_xampp_shell.PNG in C:\xampp5611\htdocs\upload.php on line 19

Notice: Undefined index: 01a01 - starting_xampp_shell.PNG in C:\xampp5611\htdocs\upload.php on line 24

Notice: Undefined index: extension in C:\xampp5611\htdocs\upload.php on line 26

Notice: Undefined index: 01a01 - starting_xampp_shell.PNG in C:\xampp5611\htdocs\upload.php on line 35

Notice: Undefined index: 01a01 - starting_xampp_shell.PNG in C:\xampp5611\htdocs\upload.php on line 40

Warning: getimagesize(): Filename cannot be empty in C:\xampp5611\htdocs\upload.php on line 40

Notice: Undefined index: 01a01 - starting_xampp_shell.PNG in C:\xampp5611\htdocs\upload.php on line 46
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: $_FILE in a function

Postby ken_mayer » 14. October 2015 15:40

Okay, so it substituted the filename where it was showing "$filename" ... this doesn't exactly solve anything. The results from isset() are still blank, so somehow the $_FILE variable is not working properly inside the function. This seems to be the actual issue ...
ken_mayer
 
Posts: 9
Joined: 14. October 2015 14:26
Operating System: Windows 10

Re: $_FILE in a function

Postby Altrea » 14. October 2015 15:41

No, the issue is that you don't have any clue what is inside $_FILES and what you expect.
It is all working fine but you use it the wrong way.
print_r $_FILES in your fileUpload function and you will (hopefully) see what i mean.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: $_FILE in a function

Postby ken_mayer » 14. October 2015 16:06

Okay, but here's what I'm asking for: HOW DO I FIX IT??? I'm not asking to be told I don't know what I'm doing. I understand there is a problem. I am trying to understand and FIX the problem. WHAT IS WRONG IN MY CODE? HOW DO I FIX IT?
ken_mayer
 
Posts: 9
Joined: 14. October 2015 14:26
Operating System: Windows 10

Re: $_FILE in a function

Postby Altrea » 14. October 2015 16:10

use the correct array keys $_FILES contains.
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: $_FILE in a function

Postby ken_mayer » 14. October 2015 16:14

You're still being really vague here. I can see the output from print_r() ... I am using code that was written by someone else, from here (but simplified a little):

Code: Select all
http://www.tidy-designs.co.uk/website-development/php-secure-file-upload-script/


I am using the code with my variable name, and some parts of it removed to simplify, but other than that, I am using something that supposedly works. So what is wrong with my code? WHAT DO I NEED TO DO TO ACTUALLY FIX IT, rather than vague references like you keep throwing at me?
ken_mayer
 
Posts: 9
Joined: 14. October 2015 14:26
Operating System: Windows 10

Re: $_FILE in a function

Postby Altrea » 14. October 2015 16:18

ken_mayer wrote:You're still being really vague here.

Sure. We provide help so that you get the knowledge to solve such problems yourself for the future.
This board here is not meant to correct foreign code for you. If you want that, pay a freelance php programmer.

ken_mayer wrote:I can see the output from print_r()

Great, So how do the array look like and what does this tells you about the way you have to address this array?
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64

Re: $_FILE in a function

Postby ken_mayer » 14. October 2015 16:26

This is what I get:

Code: Select all
Array ( [armory] => Array ( [name] => CJ_and_Ken_Lainas_Party.jpg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\php6A3A.tmp [error] => 0 [size] => 60897 ) )


This doesn't tell me what to use to reference the information correctly. I tried changing the call to the isset() function:

Code: Select all
echo "isset function test: " . isset( $_FILES[$filename]['name'] ) . "<br />";


And I still get a blank value from that.

So what am I doing wrong? I understand you're trying to help solve the problem, but you're still not giving me enough information to figure anything out.
ken_mayer
 
Posts: 9
Joined: 14. October 2015 14:26
Operating System: Windows 10

Next

Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 142 guests