The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Working with large files?
Discuss Working with large files? in the PHP Development forum on Dev Shed. Working with large files? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 12th, 2013, 12:56 PM
|
|
|
|
PHP-General - Working with large files?
So i have a large file, 14MB and i want to work on the content (emails on a separate line).
I have tried increasing the memory limit but it seems my host has set a maximum and i am unable to increase it as trying different values result in same amount of memory error i.e. tried to allocate 90MB~ blah blah..
so i have tried different ways:
1. file-get
2.fget to read one line at at ime
3.file("filename")
4. using form to input the email
but none of these work.. please let me know the best way short of splitting the file?
thanks
|

February 12th, 2013, 01:26 PM
|
|
|
|
"None of these work" is not much to go on. What did you try (post the code) and what didn't happen or what error did you get?
P.S. There is no limit per se in PHP. The total memory used by PHP is what is limited but that is changeable.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.
|

February 12th, 2013, 01:31 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
Show your fget code. fget (plus maybe fseek) is the only really correct solution.
__________________
HEY! YOU! Read the New User Guide and Forum Rules
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
|

February 12th, 2013, 03:15 PM
|
|
|
sorry, here is the code.
Code:
$file = file_get_contents($_GET['file'].".csv");
$emails = explode("\n",$file);
$i = 0;
$new_file = array();
foreach( $emails as $email ) {
$hashed_email = md5($email);
$new_file[] = $email.",".$hashed_email;
}
$new_file = implode("\r\n",$new_file);
file_put_contents($_GET['file']."-modified.csv",$new_file);
another way:
Code:
$handle = @fopen($_GET['file'].".csv", "r");
$emails = array();
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$emails[] = $buffer."<br/>";
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
The error is the PHP's maximum limit reached error:
Fatal error: Allowed memory size of 94371840 bytes exhausted (tried to allocate 71 bytes) in .... on line 8
|

February 12th, 2013, 03:40 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
The purpose of using fgets is so that you don't hold the whole file in memory. Both of your examples...hold the whole file in memory.
Do your processing inside this loop:
PHP Code:
$handle = @fopen($_GET['file'].".csv", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
mail($buffer, 'no-reply@yoursite.com', 'You were in our file!', 'Congratulations, you were in the file we were processing, have a nice day!');
}
fclose($handle);
}
|

February 12th, 2013, 03:54 PM
|
|
|
i dont see how your code is different than mine? but to give you the benefit, i tried the following and still got the fatal execution.. the thing is at HOME on my LAMP, i have increased the memory but still nothing is happening?
I have even tried to run the script on CLI which i thought was better? but still same error
Code:
<?php
$handle = @fopen($_GET['file'].".csv", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$hashed_email = md5($buffer);
$new_file[] = $buffer.",".$hashed_email;
}
fclose($handle);
}
$new_file = implode("\r\n",$new_file);
file_put_contents($_GET['file']."-modified.csv",$new_file);
?>
|

February 12th, 2013, 04:08 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
The difference is that his does the action for each individual line in the file as they're being read in, while yours tries to build a giant array of everything and then sends one email with them all.
Your script should only use one loop and only fopen/fread/fwrite/fclose. No file_get_contents(), no file_put_contents(), no array that you build up in one loop then tear down in another loop.
|

February 12th, 2013, 06:40 PM
|
 |
Lost in code
|
|
|
|
Inside your while(fgets) loop you cannot have anything that appends; ie, you cannot have:
or
Otherwise you will end up appending the entire contents of the source file into memory, which is why you are getting the memory error.
You need to overwrite all of the variables in the loop on every iteration of the loop.
In the followup code you posted you're still appending to $new_file inside the loop.
You could actually still use file_put_contents, but you have to use the FILE_APPEND flag, and you have to call it within the loop.
|

February 13th, 2013, 03:47 AM
|
|
|
|
cheers, now i understand.. i didnt realise that the error i was getting was due to me putting it all into an array and not because of fget...
thanks for explaining.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|