Download large file via PHP stops before the end


I'm trying to download large files (the aim is to download films).
I'm just trying yet with a pdf file of 14Mo.
The download starts, and ends at 5Mo before the end of the file. Then the file downloaded is corrupted and can't be opened.
I've tried with 2 methods: readfile, fread with buffer.
None worked.

Have any idea?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

check the two variables for your php configuration


May be they must be getting exhausted

Or else check the error_log for the exact error message what you are getting
jean-sylvainAuthor Commented:
I put :


and in phpinfo i get:

max_execution_time: 30
memory_limit: 8M

Can u try increasing the memory to 128MB and then try
I think that can solve the problem
Get a highly available system for cyber protection

The Acronis SDI Appliance is a new plug-n-play solution with pre-configured Acronis Software-Defined Infrastructure software that gives service providers and enterprises ready access to a fault-tolerant system, which combines universal storage and high-performance virtualization.

jean-sylvainAuthor Commented:
I can't, my website is hosted.
try setting with this function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Hello !

I managed to download a 180Mo file with the code below.

Nevertheless, if the script takes longer than 30s and if you can't  increase max_execution_time, you are stuck. (look for a dedicated server if you can afford it)

But, downloading this 180Mo file took about 5 CPU seconds so it shoud work for a movie.


	fetch_url ( '', 'google.html' );

	// ---

	function fetch_url ( $url, $file ) {
		if ( ( $input = fopen ( $url, 'rb' ) ) === false ) {
			echo 'Unable to fopen url';
			return false;

		if ( ( $output = fopen ( $file, 'wb' ) ) === false ) {
			echo 'Unable to fopen file';
			return false;

		// ---

		while ( feof ( $input ) === false ) {
			if ( ( $buffer = fread ( $input, 8192 ) ) === false ) {
				echo 'Unable to fread url';
				return false;

			if ( fwrite ( $output, $buffer ) === false ) {
				echo 'Unable to fwrite file';
				return false;

		// ---

		fclose ( $input );
		fclose ( $output );

		return true;

Open in new window

jean-sylvainAuthor Commented:
Thanks for your help, the memory_limit was the point!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.