I have a PHP script that uploads a CSV file and then saves some of the contents of the csv file to a database. Everything on the script works fine when I use a file that is about 40 kb, about 100 lines. But when I try to upload a similar structured file that is 850 kb and larger, I get this error message:
Array ( [type] => 2048 [message] => Only variables should be assigned
by reference [file] => /home/dir_name/uploader.php [line] => 10 )
Which is output from the error_get_last() function.
Here is the script I am using:
<?php
/*
* Config
*/
mysql_connect(/* creds */) or die(mysql_error());
mysql_select_db('db_name');
$user =& JFactory::getUser();
$user_id = $user->get('id');
ini_set('error_log','/home/dir_name/error.log');
ini_set('upload_max_filesize', '50M');
ini_set('memory_limit','32M');
ini_set('post_max_size','32M');
/*
* Upload CSV File
*/
if (isset($_FILES['uploadedfile'])){
if (substr($_FILES['uploadedfile']['name'],-3,3) == "csv"){
echo $_FILES['uploadedfile']['name']."<br/>";
$thefile = fopen($_FILES['uploadedfile']['tmp_name'],'r');
if ($thefile === false){
print_r(error_get_last());
exit;
}
while (($data = fgetcsv($thefile, 0, ";",'"')) !== FALSE) {
if($data[0] == "Item Number"){
continue;
}
/*
* Do all the stuff to data and save to db
*/
...
...
}
//header("Location: ".$_SERVER['PHP_SELF']);
}
}
?>
<form enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose your CSV file of inventory: <input name="uploadedfile" type="file" />
<input type="submit" value="Upload File" />
</form>
As you can see I have manually set the upload_max_filesize, memory_limit, and post_max_size variables because I thought that might be the problem… but it didn’t seem to change anything. Any help with this problem would be greatly appreciated. I want to be able to upload BIG files with this script. 🙂
** EDIT **
Remember that this script works well when the file was small enough. I tried it again with a small file (100 lines), and its good. I removed the & from 10th line, and now I get this error when trying a bigger file:
Array ( [type] => 2048 [message] => Only variables should be assigned
by reference [file] =>
/home/dir_name/plugins/content/jw_simpleImageGallery/jw_simpleImageGallery.php
[line] => 32 )
This error is being thrown by the function error_get_last on line 28, which means that the fopen function returned FALSE. It is definitely a problem with opening the uploaded file. For some reason the error_get_last function is showing errors (I think they are warnings) that don’t have to do with the file upload.
** EDIT 2 **
I checked the vales of the PHP ini variables like this:
ini_set('error_log','/home/pkind2/tim/error.log');
ini_set('upload_max_filesize', '50M');
ini_set('memory_limit','32M');
ini_set('post_max_size','32M');
echo ini_get('upload_max_filesize').'<br>';
echo ini_get('memory_limit').'<br>';
echo ini_get('post_max_size').'<br>';
and the output is:
100 M
50 M
100 M
I guess I do not have the privileges to change these variables, but by default they are set very high already, so it doesn’t look like these variables are getting in the way of an 850 k file.
** EDIT 3 **
I checked the value of $_FILES['uploadedfile']['tmp_name'], and for the small fiel ( ~ 37 kb ) I get this output:
/tmp/php7yto3f
But when I try the 850 KB file it doesn’t output anything for that variable, and then I get the crash. SO it seems that the file isn’t being saved to the temporary location for the bigger file size.
** EDIT 4 **
The problem turned out to be the <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> That must by 100,000 bytes, or 100 kb, so i increased this value and the bigger file worked… :-/
I think the problem is on line 10:
Try to remove the “&”.