Solved

PHP file_get_contents failing of larger files

Posted on 2009-05-14
10
407 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PHP and Soap 3 27
linux apache localhost dev laptop 5 33
php documentation 4 19
confirm popup before record delete takes place 87 22
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 look for a specific file type in a local or remote server directory using PHP.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now