问题
See my other QUESTION.
I've successfully set up a simple file transfer, but I've noticed that trying to echo anything on the page corrupts the file, making it unopenable.
Here's the code:
$filename = $_GET['filename'];
$dir = "C:/Users/Me/Desktop/";
$file = realpath($dir . $filename);
//echo "$filename</br>$dir</br>$file";
if (file_exists($file)) {
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
This works just fine, but if I uncomment the echo, the downloaded file is corrupted and won't open.
Is this because, somehow, the echo is being written to the file to be downloaded? Perhaps (probably) I don't fully the process taking place above.
Obviously, the echo won't be displayed if the above code executes successfully anyway, but what other debugging practices would you recommend? Clearly, echoing variable values won't always work. Any alternatives?
回答1:
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.
来源:https://stackoverflow.com/questions/36503964/php-file-transer-echo-breaks-things-alternate-for-debugging