PHP File Transer - echo breaks things, alternate for debugging?

﹥>﹥吖頭↗ 提交于 2019-12-11 18:07:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!