Viewed   88 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.

 Answers

1

Take a look at the ignore_user_abort and connection_abort function.

Wednesday, August 17, 2022
 
4

Is the purpose of using basename() simply to strip the path from the filename?

Yes, this header is what is used by the browser to present to the user a filename to save the downloaded file as. You wouldn't want to offer the user a full filepath, just the filename, and I'm not sure the browser would even present a full filepath to the user.

What is the purpose of realpath()? In my usage, it seems to make no difference whatsoever. Based on what I've found, it seems to just 'standardize' filepath inputs. Is that correct?

It resolves relative and symbolic links to their absolute paths, which is probably what you mean by 'standardizing'. If you only ever supply it absolute paths, it'll do nothing.

I don't seem to need the last three lines to make this work:

header("Content-Length: " . filesize($cf));

header("Content-Type: application/octet-stream");

readfile(realpath($cf));

Do I need them? What do they do? I should note that I'm testing just using localhost, in case that makes a difference.

You should keep them all. The first two headers tell the browser how big the file is and what type of file it is. Right now you're using a generic media type, but if you were to send, say a PDF file, you could use the more-specific PDF media type and the browser would let the user know they're downloading a PDF.

I also don't think downloading would work without the last line... That's what's actually reading the file by PHP and sending it to your browser. If you omit it, you'll probably end up downloading a blank file.

Monday, October 31, 2022
 
5

If you want to find out how many UTF-8 bytes a letter in a PHP string has then:

print strlen(mb_substr($string, 0, 1, "utf-8"));

strlen() returns the raw byte length, while mb_substr() returns a "character" according to the charset/encoding. In this example from position 0.

Wednesday, October 12, 2022
4

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.

Wednesday, November 2, 2022
 
1

You need just two simple ifs if you are interested on the common sizes only. Consider this (assuming that you actually have unsigned values):

if (val < 0x10000) {
    if (val < 0x100) // 8 bit
    else // 16 bit
} else {
    if (val < 0x100000000L) // 32 bit
    else // 64 bit
}

Should you need to test for other sizes, choosing a middle point and then doing nested tests will keep the number of tests very low in any case. However, in that case making the testing a recursive function might be a better option, to keep the code simple. A decent compiler will optimize away the recursive calls so that the resulting code is still just as fast.

Sunday, December 11, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :