Solved

PHP file_get_contents failing of larger files

Posted on 2009-05-14
10
410 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php function to remove a file 31 40
echo paypal data on screen 5 52
000webhost.com default error log 1 28
PHP curl issue VERBOSE output 18 43
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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 …

777 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