PHP problem handling large PDF files

Asked by:
atef81 / 14 Points
Time:
2014-01-31 7:10 pm EST
Category:
Website Troubleshooting
Hits:
2,356
i have a php script that counts the number of dowlnoads of each file.
i have a large pdf file (716MB). the file in good and not corrupt.
when trying to download id through the php script i only get a file that is 192 bytes.
when i opened it using Notepad++, this is what is written in it:

<br />
<b>Fatal error</b>: Out of memory (allocated 786432) (tried to allocate 750213121 bytes) in <b>/home/coptic7/public_html/counter/download.php</b> on line <b>34</b><br />

I increaded the memory limit in my php.ini to 2048M, and have the suPHP_ConfigPath /home/username/public_html command in my .htaccess file
but i still can't get it to download properly.

i can download other files of size 400MB correctly.
Is there a way to prevent this parsing corruption?
I need to know how

To ask this user for more information, please first login.

To submit an answer, please login.

ANSWERS

0

johnpaulb-whh2
Staff
12,339 Points
2014-01-31 9:41 pm EST
Hello atef81,

Thank you for your question. First, keep in mind that you can easily view download counts in your cPanel with AWSTATS, or with a service such as Google Analytics.

This problem was occurring because your script for counting was copying the full PDF into memory and was trying to buffer it, which is causing problems.

I was able to get it working by disabling buffering in the PHP.ini with the following line:
output_buffering = Off

So that it would not interfere with your other websites, I copied a new php.ini file into your /counter folder.

If you have any further questions, feel free to post them below.
Thank you,

-John-Paul

To submit a comment on this answer, please first login.

Hi John,
Thank you very much for your professional answer.
I can see that it really does solve the problem.
Just so that i make sure that i understood correctly:
all what i need to do is to have always the copy of the php.ini & .htaccess placed in the counter folder and that's it?
The reason I am asking is because you said :"So that it would not interfere with your other websites"
I don't understand what other websites do i have?
Can you please explain more?
Thanks a lot
atef81
14 Points

2014-01-31 9:56 pm EST
Hello atef81,

You are very welcome! Yes, just leave the php.ini and .htaccess files in the Counter folder, so the rules will only affect files in the Counter folder.

For example: If you turned "buffering" off in the php.ini located in the public_html folder instead, it would turn buffering off for everything using that php.ini.

I noticed you have several Subdomains, and each one is essentially a separate website, and I wasn't sure if any of them require buffering.

If you have any further questions, feel free to post them below.
Thank you,

-John-Paul
johnpaulb-whh2
12,339 Points
Staff
2014-01-31 10:17 pm EST
Thank you very much again John,
Sorry for my beginner level questions:
1- do I need to change the coding of the counter? or how does it know that it should read the php.ini in its folder, not the one on the root?
2- Why do we generally need the buffering option to be on at all? i.e. what harm can happen if i turned it off to all my subdomains?
Thank you so much.
atef81
14 Points

2014-01-31 10:33 pm EST
Hello atef81,

No problem, I understand.


  • You do not need to change anything. The .htaccess file in your /counter folder is pointing to the php.ini in the /counter folder, by specifying the suPHP_ConfigPath.

  • Whether you need buffering depends on the type of queries you are running and how your site was coded. Due to this I do not know how it will affect your website as a whole. I recommend reading the Official PHP guide on Buffered and Unbuffered queries for additional detailed information.


If you have any further questions, feel free to post them below.
Thank you,

-John-Paul
johnpaulb-whh2
12,339 Points
Staff
2014-01-31 11:41 pm EST
Hello atef81,

You don't need to change any coding of any of your files in the /counter directory, if things continue to work with the output_buffering turned off for that folder.

You generally don't need the output_buffering option turned on, and it's off on the server by default. But it is necessary for some scripts in order to function properly.

It looks like you had a /public_html/php.ini file, and also already had set your php.ini file recursive.

In that /public_html/php.ini file you had the entry output_buffering = On, and because it was recursive because of the .htaccess rules, it was also carrying down to your /counter folder causing the issues you first reported.

Placing another php.ini file inside of the /counter folder overrides the one from the /public_html directory, fixing the problem in this case.

Typically you don't want to set the output_buffering setting to On, but rather a limited value such as 4096, this is mentioned in the PHP.net output_buffering documentation.

You might also want to be careful about loading large files through PHP instead of providing a direct link to a large file instead. You can monitor your CPU usage to make sure your PHP scripts aren't causing any issues with this.

- Jacob
JacobN
3,713 Points
Staff
2014-01-31 11:47 pm EST
Ok. Thanks everyone.
My last question is: can I then cleanup the situation by having only one php.ini?
Currently i have 3 copies, one one the root, one in the arabic folder and one in the counter folder.
Is this normal?
thanks
atef81
14 Points

2014-02-02 5:11 pm EST
Hello atef81,

You do not need several php.ini files, unless you have assigned multiple different configurations for different folders. If you want to remove them, I recommend renaming them to ensure that if anything goes wrong with your site, you will be able to restore them.

JeffMa
2,342 Points
Staff
2014-02-03 5:20 pm EST
Want to share this Question?

Related Articles

It looks like there are no related articles.
Would you like to ask a question about this page? If so, click the button below!
Need More Help?

Help Center Search

Current Customers

Email: support@WebHostingHub.com Ticket: Submit a Support Ticket
Call: 877-595-4HUB (4482)
757-416-6627 (Intl.)
Chat: Click To Chat Now

Ask the Community

Get help with your questions from our community of like-minded hosting users and Web Hosting Hub Staff.

Not a Customer?

Get web hosting from a company that is here to help.
}