I need to process a CSV file and create a new CSV file. In order to get a proof of concept, I placed the CSV on the server and was able to process it fine. Then I setup a form in order to allow the client to upload the file. I don't even want to store the file on my server, just have the user submit it via the form, process it and provide the new file for download. The problem I am running into is when I run the $_FILES["file1"] through my function, I get this error "Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 32 bytes)". The file is only 3Kb. I don't get this error when I process the same file when it is located in my uploads/ directory. Here's the code I'm using.

the form

<table width="600px">
                  <form action="dataupload.php" method="POST"  accept-charset="UTF-8" enctype="multipart/form-data" autocomplete="off" novalidate>
                     <tr>
                        <td width="20%">File #1</td>
                        <td width="80%"><input type="file" name="file1" id="file1" /></td>
                     </tr>
                     <tr>
                        <td>Submit</td>
                        <td><input type="submit" name="submit" value="Submit" /></td>
                     </tr>   
                </form>
</table>

the PHP code

function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
    while (!feof($file_handle) ) {
        $line_of_text[] = fgetcsv($file_handle);
    }
    fclose($file_handle);
    return $line_of_text;
}

$csvFile1 = $_FILES["file1"];

$csv = readCSV($csvFile1);

Can anyone help me figure out how to fix this? If I can't do it without just uploading the file to the server, that's fine. I've also had a bit of trouble getting that to work, so I could use help in that case.

Thanks

Justin_14, this is a setting in php.ini file. If you have controll of the php.ini file, change memory limit form 64M or 32M to 128M(Maximum Limit).

If not try overriding the php.ini configurations with you .htaccess file in you public folder. Use this instead

php_memory_value_limit 128M

Thanks @Gideon_1. I've seen this suggested before on other sites, and I tried a similar attempt by adding code to my script to increase limit to no avail. PHP increased the allocated memory and it still failed. It did take much longer to fail and the error message showed that it tried to allocate more memory. This led me to think it's not a memory issue, but something wrong with how I am tyring to process the file. In any case, why would the file being submitted via a form exceed the limit, but the same file on my server not? I haven't tried editing the .htaccess file, I will give that a shot. Just thought I would ask what is different in the two methods I've explained

What exactly are you trying to open to read ? $_FILES["file1"] is not a file path at any matter , it could be (if you really uploading a file) an array containg info about the uploaded file , where $_FILES['file1']['error'] is the error (status) and $_FILES['file1']['tmp_name'] the file path if the file uploaded. Take a look at http://php.net/manual/en/features.file-upload.php

I think you have an issue with the uploads. try changing this

$csvFile1 = $_FILES["file1"];

to

$csvFile1 = $_FILES["file1"]["tmp_name"];

@Gideon_1 and @jkon,

Thanks for your help. I used @jkon's mention that i was using the wrong reference to the file. Added the ['tmp_name'] to the end and it worked. The funny thing is I had tried that before, but must have had another issue with the code. I then come here to give props and find that @Gideon_1 had done done that and added more for clarification. So, I'm giving both of you +1's for your help. Thank you guys for helping out.

You are welcome, don't forget to mark discussion as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.