?
Solved

PHP file_get_contents failing of larger files

Posted on 2009-05-14
10
Medium Priority
?
421 Views
Last Modified: 2012-05-07
Hiya,

I'm using the PHP function file_get_contents() to open some files on my server. It works fine for small files but anything more than a few megebytes is failing. (Instant white screen)

Any idea how to resolve this?
0
Comment
Question by:Mr_Splash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 5

Expert Comment

by:dhobale
ID: 24388813
You will have to check with your server admin for php.ini configuration. If you increase the php file size on it, it will fix this issue. Usually its set up to 2mb on many entry level commercial host servers.
0
 
LVL 3

Author Comment

by:Mr_Splash
ID: 24388828
My host is pretty flexible and I have control over my php.ini

What's the name of the setting?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 24391148
The property is called "memory_limit".

You should however consider to look into other options first. Having a high memory limit and usage simply lowers the amount of concurrent users you can handle.

What are you doing with the contents? Is it a single pass script you might also be able to complete using fgets or other less memory consuming?
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 7

Expert Comment

by:ycTIN
ID: 24391774
you can increment the value of  memory_limit in php.ini
memory_limit = 8M  ; Maximum amount of memory a script may consume (8MB)

or

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
	@ob_start();
    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);
        echo $buffer;
        @ob_flush();
        @flush();
    }
    fclose($handle);
}
?>

Open in new window

0
 
LVL 3

Author Comment

by:Mr_Splash
ID: 24393297
My memory_limit is set to 300M in my php.ini

Basically I need to read the contents of the file (could be as big as 400M) and then pass it to a zipping class.

@ycTIN could you please explain what your code does? am I correct in thinking it is breaking down the file, processing a little bit at a time and the putting it together?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 24393313
Are you on windows or linux?

You might get the zipping done using exec() and a zip executable rather than manually using php zip classes.

0
 
LVL 3

Author Comment

by:Mr_Splash
ID: 24393326
@Roonaan: I'm on Linux, Apache
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 2000 total points
ID: 24393366
You might be able to call linux' zip using

<?php
  system('zip myfile.zip mylargefile.avi');
  //do something with the zip file
?>
0
 
LVL 3

Author Comment

by:Mr_Splash
ID: 24393400
Wow, that's much quicker and efficient.

I'm pretty sure it's worked, I'm downloading a 100MB file to make sure it extracts ok and will happily award the points if it does.

Thanks.
0
 
LVL 3

Author Closing Comment

by:Mr_Splash
ID: 31581640
Extracted perfectly. Thank you!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question