Solved

PHP file_get_contents failing of larger files

Posted on 2009-05-14
10
411 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
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
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

 
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 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

808 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