PHP文件传输 - 这是如何工作的? [英] PHP File Transfer - How does this work?

查看:98
本文介绍了PHP文件传输 - 这是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,在另一个问题,我得到了这样的回答:

  $文件名=filetodownload.xyz;
$ CF =真实路径(/非webaccessible文件夹/\".$文件名);
$文件= $ CF;标题(内容处置:附件;文件名='基名($ CF)。'');
标题(内容长度:文件大小($ CF));
标题(内容类型:应用程序/八位字节流);
ReadFile的(真实路径($ CF));

我能够通过仅仅使用顶端标题行得到它的工作为我的目的:

 头('内容处置:附件;文件名='。基本名($ CF)'');

有一些问题我对整体解决方案虽然增加我的理解:

1 中使用基本名()简单地剥去文件名路径的目的是什么?

2 什么是真实路径的目的()?在我的使用,似乎没有什么区别任何责任。依据是什么,我发现,它似乎只是规范文件路径输入。这是否正确?

3 我似乎并不需要最后三行,使这项工作:

 标题:(Content-Length的文件大小($ CF)。)标题(内容类型:应用程序/八位字节流);ReadFile的(真实路径($ CF));

我需要他们吗?他们在做什么?我要指出,我测试只使用本地主机,如果有差别。

有没有任何形式的安全性考虑使用这种方法提供文件下载时,我应该使?


解决方案

  

时使用的basename()仅仅是为了剥去路径的目的
  文件名?


是的,这头被什么所使用的浏览器present给用户一个文件名保存下载的文件。你不会希望为用户提供一个完整的文件路径,只是文件名,我不知道浏览器甚至会present一个完整的文件路径给用户。


  

什么是真实路径的目的()?在我的使用,这似乎使不
  任何区别。依据是什么,我发现,它似乎只是
  规范文件路径输入。这是否正确?


它解决了相对和符号链接到他们的绝对路径,这可能是你说'标准化'的意思。如果你永远只能提供它的绝对路径,它会做什么。


  

我似乎并不需要最后三行,使这项工作:


  
  

标题(内容长度:文件大小($ CF));


  
  

标题(内容类型:应用程序/八位字节流);


  
  

ReadFile的(真实路径($ CF));


  
  

我需要他们吗?他们在做什么?我要指出,我只是测试
  使用本地主机,如果有差别。


您应该保留他们。前两个头告诉多大的文件是浏览器,什么类型的文件,它是。现在,您使用的是通用的媒体类型,但如果你要送,说一个PDF文件,可以使用更具体的PDF媒体类型,浏览器将让用户知道他们正在下载一个PDF文件。

我也并不认为无需下载的最后一行将工作......这就是实际上读取由PHP文件并将其发送到浏览器。如果你忽略它,你可能最终会下载一个空白文件。

So, in another QUESTION, I got this answer:

$filename="filetodownload.xyz";
$cf = realpath("/non-webaccessible-folder/".$filename);
$file=$cf;

header('Content-Disposition: attachment; filename="' . basename($cf) . '"');
header("Content-Length: " . filesize($cf));
header("Content-Type: application/octet-stream");
readfile(realpath($cf));

I was able to get it working for my purposes by just using the top header line:

header('Content-Disposition: attachment; filename="' . basename($cf) . '"');

There are some questions I have about the whole solution though, to increase my understanding:

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

2. 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?

3. 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.

Is there any kind of security considerations I should make when using this method for providing file downloads?

解决方案

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.

这篇关于PHP文件传输 - 这是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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