开发者

Logging dynamically served files in APACHE

开发者 https://www.devze.com 2022-12-17 10:49 出处:网络
I\'m serving up Zip and PDF files on the fly via PHP using an output such as: header(\'Content-Disposition: a开发者_StackOverflow中文版ttachment; filename=\"\'.$project->name .\'.zip\"\');

I'm serving up Zip and PDF files on the fly via PHP using an output such as:

header('Content-Disposition: a开发者_StackOverflow中文版ttachment; filename="'.$project->name .'.zip"');
echo($zipfile->zl_pack());

I can't find any reference to these downloads in my APACHE logs though. Is it not logged due to being dynamic?

Do I need to actually write the file to the webserver and then serve the result up to get it logged or am I missing something?

Cheers, Niggles


Correct. httpd does not look at outgoing headers for logging. error_log() will send a message to httpd's error log, but there's no way to put something in the access log.


The request to the PHP program that generates that header should be logged. The filename mentioned in the content disposition header won't be.

I believe mod_perl would allow you to add custom logging, I don't know if mod_php provides a similar feature.


As a workaround you could use mod_rewrite to have the *.zip file logged and still served it through PHP without actually writing it to the filesystem, the client will have to send two requests though.

1) Change your current script so that it doesn't produce the file, but instead puts the parameters needed for the file creation in the session; instead of the current header and file content you would put header('Location: '.$project->name .'.zip');

2) This would cause the second request. Since the requested file doesn't exist yet, you would use mod_rewrite to change the request to a *.zip file to the same or some other PHP script that reads the parameters from the session and produces the file just like you're doing it now. If you use the same PHP script, you would additionally let mod_rewrite add some parameter like "?action=produceFile" to the request and then test for that parameter in the script.

This way you would have two nice entries in your Apache log without having to physically save the file (and probably delete it later).


FYI I found a really good work-around.

Part of the problem was that we wanted to force a "save as" dialogue as many of the users get confused as to where the file gets saved. So this is why I went the

Content-Disposition : attachment

method.

A better method which still invokes the dialogue is to add this to .htaccess

<Files *.zip>
ForceType application/octet-stream
Header set Content-Disposition attachment
</Files>

write the Zip to the fileserver and redirect the page to the zip.

Sure I have to cleanup every night, but it gets logged and it still forces them to choose where they download the file (on pretty much everything but Safari [ there's always one ]).

Cheers, Niggles

0

精彩评论

暂无评论...
验证码 换一张
取 消