Asked  7 Months ago    Answers:  5   Viewed   41 times

Is it possible in PHP to get a count of the number of bytes transmitted to the client? For example, if I'm outputting a 10 MB file, is there a way to find out if all 10 MB were sent to the client, or to see if the client interrupted the transfer partway? I know Apache will log this afterwards, but I'd like to access the data in PHP.



Take a look at the ignore_user_abort and connection_abort function.

Wednesday, March 31, 2021
answered 7 Months ago

Try setting a global variable prior to utilizing the class.


then use it to store the count and simply check it after your functions and classes have been called.

Saturday, May 29, 2021
answered 5 Months ago

FTP will work, but:

  • If the server and the client are on the same network, you can use a network share. Going this route, you can use the normal filesystem functions available in PHP (docs). Just use the network name and share path, flip the slashes to forward: "//servername/path/to/share" Here is a tutorial on setting up a network share, assuming Windows is the client OS.

  • If network share is out of the question, you can set up a local FTP server. Here is a Lifehacker article on how to set this up, assuming you have a Windows OS. There are similar tutorials for Mac and *nix, there are so many you'd have to try NOT to find them on Google. :) There are many other routes to set up the FTP server, it isn't that important which server you use, just make sure you aren't leaving yourself wide open to the internet. After you set up the local FTP server, use PHP's FTP functions to connect from the server to your local machine.

It really is a better route to use the network share if you can, but of course that isn't an option if the server is not in your local network.

Saturday, May 29, 2021
answered 5 Months ago

Yes, if you echo some data it will become part of whatever file you are sending and probably corrupt it.

I would recommend you look at the error_log function. It will let you write to a log file, which won't affect output. You can look that the log periodically and check for any important errors.

Saturday, May 29, 2021
answered 5 Months ago

Your server is already doing that, it is called the log file. If you grep it for the given page and count the number of log enteries, you will have the number of vistors who viewed that page. This, however, is inefficient and doesn't "work with CPanel".

Next, you could write a simple script to insert an entry to a database every time someone visits that page. Then your answer could be queried by custom PHP you add to CPanel. This is more effiecient, but would be reinventing the wheel.

But, the real answer is get analytics software. There are a number of products that can do this for you:

  • Google Analytics - This is a Google Product that collects lots of different types of user information, generates powerful reports, and is freely available online. It has the downside of giving that data to google - so if you have a privacy policy, you'd want to specifically call out the Google Analytics tracking you're doing.

  • Piwik - This you can either get cloud-hosted, or host it yourself. It's free open-source software that does most of what Google Analytics does. You'd own your data and that data would reside on your server, there are widgets that could be embedded on pages so with some work could be integrated into CPanel. The reports are not as polished looking as Google Analytics, but that might be worth the trade-off.

Saturday, May 29, 2021
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :