Strict Standards - Error

Hello,

I am using this code to upload a file to my server. The code works in the sense that it is uploading the file successfully, including preventing uploads with wrong extensions … however every time I upload a file I get the following error

Strict Standards: Only variables should be passed by reference in C:\upload.php on line 8

Below is my code, I have marked line 8. Any ideas??? Thanks in advance :slight_smile:

<?php

$message = '';
$target_path = "C:\\mypathhere";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
$file = $_FILES['uploadedfile']['name'];
$allowed_ext = array('csv','txt','rtf');
$actualext = end(explode(".", $file)); // Line 8

	if( $file > 1111111){
			$message = 'File over 1111111 bytes';
		} if ($message == NULL && !in_array($actualext, $allowed_ext)){
			$message = 'File extension not allowed';
		} if($message == NULL){
			if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
				echo "The file ".  basename($file)." has been uploaded";
			} else{
				echo "There was an error uploading the file, please try again!";
			}
		}
echo $message;
 ?>

 

This probably starts to fall on the technical side of PHP, but if I had to guess, the explode() function returns a reference in memory pointing to the array that it generated. By wrapping that function inside end() you are asking it to send the reference directly to another function (which is fine).

The reason you get this error though is the fact that it is preferred to have a variable to help prevent memory leaks. If you didn’t use the returned value of explode() you will have memory being set that is never utilized/cleared out (until Garbage Collection occurs).

To get around the error, use the following:

$extensions = explode(".", $file);
$actualext = end($extensions);

Hi,

Thank you very much; it works now !!! :slight_smile:

Better yet:


$actualext = pathinfo($file,  PATHINFO_EXTENSION);