Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 463
  • Last Modified:

PHP file_get_contents failing of larger files

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
Mr_Splash
Asked:
Mr_Splash
1 Solution
 
dhobaleCommented:
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
 
Mr_SplashAuthor Commented:
My host is pretty flexible and I have control over my php.ini

What's the name of the setting?
0
 
RoonaanCommented:
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ycTINCommented:
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
 
Mr_SplashAuthor Commented:
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
 
RoonaanCommented:
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
 
Mr_SplashAuthor Commented:
@Roonaan: I'm on Linux, Apache
0
 
RoonaanCommented:
You might be able to call linux' zip using

<?php
  system('zip myfile.zip mylargefile.avi');
  //do something with the zip file
?>
0
 
Mr_SplashAuthor Commented:
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
 
Mr_SplashAuthor Commented:
Extracted perfectly. Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now