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

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

问题描述

所以,在另一个问题中,我得到了这个答案:

  $ 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));

只需使用顶部标题行,我就能够为我的目的而工作:

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

我有一些关于整个解决方案的问题,以增加我的理解:



1。使用 basename()的目的是简单地从文件名中删除路径?



2。 realpath()的目的是什么?在我的使用中,似乎没有任何区别。根据我发现,似乎只是'标准化'文件路径输入。这是正确的吗?



3。我似乎不需要最后三行来完成这项工作:

  header(Content-Length:。filesize($ cf)); 

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

readfile(realpath($ cf));

我需要他们吗?他们在做什么?我应该注意到,我正在使用localhost进行测试,以防发生变化。



使用此方法提供的任何类型的安全注意事项文件下载?

解决方案


使用basename()的目的只是剥离
filename?


是的,这个标题是浏览器用来向用户呈现一个文件名来保存下载文件为。您不想向用户提供完整的文件路径,只是文件名,我不知道浏览器甚至会向用户呈现完整的文件路径。


realpath()的目的是什么?在我的使用中,似乎没有任何
的差异。根据我发现,似乎只需
'标准化'文件路径输入。这是正确的吗?


它解析了相对和符号链接到它们的绝对路径,这可能是标准化的意思。如果你只提供绝对路径,它什么都不做。


我似乎不需要最后三行来完成这项工作:



标题(Content-Length:。filesize($ cf));



头(Content-Type:application / octet-stream); / p>

readfile(realpath($ cf));



我需要他们吗?他们在做什么?我应该注意到,我正在使用localhost测试
,以防发生变化。


你应该保持他们所有。前两个标头告诉浏览器文件大小和文件类型。现在您使用的是通用媒体类型,但如果要发送PDF文件,则可以使用更具体的PDF媒体类型,浏览器会让用户知道他们正在下载PDF。 p>

我也不认为下载将无法使用最后一行...这是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天全站免登陆