$_FILE in a function
Posted: 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:
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:
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
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;
}
?>