HELP needed with file upload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RossBryan
Forum Newbie
Posts: 19
Joined: Fri Aug 19, 2011 3:03 pm

HELP needed with file upload

Post by RossBryan »

Can anybody help with this little problem i have. Im pretty new to PHP im trying to develop a file upload site, and have bumped into a problem when trying to apply, I think the end() function; not quite sure for certain. The script isnt complete but in essence at this stage im trying to varify if the file extension the user has choosen is one of the allowed extensions.

Heres the script followed by the error message displayed:

Code: Select all

<?php require ("design/top.php"); 

if (!(isset($_SESSION['user'])&& $_SESSION['user']!=''))
{
	header("Location: loginC2S.php");	
}

?>
	
<title>boxeD:IN - Upload</title>
<div id= 'left'> <br />
    
        
<?php
		
if (isset ($_FILES['userfile']))
{
	$errors = array(); //list of errors
	$allowed_ext = array('jpg','jpeg','gif','png','mp3');
			
	$file_name = $_FILES['userfile']['name']; 
	$file_ext = strtolower (end(explode ('.', $file_name)));		
	$file_size = $_FILES ['userfile']['size'];	
	$file_tmp = $_FILES ['userfile']['tmp_name'];	
			
	if (in_array($file_ext, $allowed_ext) ===false)	
	{
		$errors[] = "Extension not allowed!!!";
	}

}
		
    	
?>
        
<form action ='upload.php' method ='POST' enctype ='multipart/form-data'> 
	<table>
		<tr>
			<td>Select file to upload:</td>
		</tr>
		<tr>
			<td><input type='file' name='userfile'/></td>
		</tr>
		<tr>
			<td><input type='submit' value='Upload'/></td>
		</tr>
	</table>		
</form>

</div>
    
<?php require ("design/bottom.php"); ?>
Error Message:
Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\Care2Share\upload.php on line 22
The error message is refering to:

Code: Select all

$file_ext = strtolower (end(explode ('.', $file_name)));
Any help would be much appreciated. Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: HELP needed with file upload

Post by requinix »

As I said here,
You're correct, the problem is with end(). It needs a variable - a real, actual variable - because it does more than just return the last element in an array.

However pathinfo is better for getting file extensions.

Code: Select all

$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
RossBryan
Forum Newbie
Posts: 19
Joined: Fri Aug 19, 2011 3:03 pm

Re: HELP needed with file upload

Post by RossBryan »

Thanks ended up resolving the problem using pathinfo as advised:

Code: Select all

$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
The following also worked great:

Code: Select all

$array = explode ('.', $file_name); 
$file_ext = strtolower (end($array)); 
Post Reply