Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP file_get_contents failing of larger files

Posted on 2009-05-14
10
Medium Priority
?
429 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
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!

 
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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

609 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