PHP File Transer-回声破坏了事情,可以替代调试吗? [英] PHP File Transer - echo breaks things, alternate for debugging?

查看:169
本文介绍了PHP File Transer-回声破坏了事情,可以替代调试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅我的其他问题.

我已经成功设置了一个简单的文件传输,但是我注意到试图回显页面上的任何内容都会损坏文件,从而使其无法打开.

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.

代码如下:

$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?

推荐答案

是的,如果您回显一些数据,它将成为您正在发送的任何文件的一部分,并且很可能损坏了它.

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

我建议您查看 error_log 功能.它可以让您写入日志文件,而不会影响输出.您可以定期查看日志并检查是否有重要错误.

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.

这篇关于PHP File Transer-回声破坏了事情,可以替代调试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆