GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   PHP Gurus (https://gfy.com/showthread.php?t=633489)

qw12er 07-15-2006 07:29 AM

PHP Gurus
 
I'm using this function to send a file to my users :

function send_file($name) {
ob_end_clean();
$path = $name;

if (!is_file($path) or connection_status()!=0) return(FALSE);
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)(filesize($path)));
header("Content-Disposition: inline; filename=$name");
header("Content-Transfer-Encoding: binary\n");

if ($file = fopen($path, 'rb')) {
while(!feof($file) and (connection_status()= =0)) {
print(fread($file, 1024*8));
ob_flush();
}
fclose($file);
}
return((connection_status()= =0) and !connection_aborted());
}


But for some reason if I try to open it without saving it on my disk it will causes an error. (Invalid Zip file) But if I save the file first and then open it everything works just fine.

How can I fix that ?

Damian_Maxcash 07-15-2006 07:33 AM

Quote:

Originally Posted by qw12er
I'm using this function to send a file to my users :

function send_file($name) {
ob_end_clean();
$path = $name;

if (!is_file($path) or connection_status()!=0) return(FALSE);
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Content-Type: application/octet-stream");
header("Content-Length: ".(string)(filesize($path)));
header("Content-Disposition: inline; filename=$name");
header("Content-Transfer-Encoding: binary\n");

if ($file = fopen($path, 'rb')) {
while(!feof($file) and (connection_status()= =0)) {
print(fread($file, 1024*8));
ob_flush();
}
fclose($file);
}
return((connection_status()= =0) and !connection_aborted());
}


But for some reason if I try to open it without saving it on my disk it will causes an error. (Invalid Zip file) But if I save the file first and then open it everything works just fine.

How can I fix that ?


Im not totally sure - but I think it could be the mime type setting in your apache config file that is the issue. That could be a start for you to Google from - but I could be way off.

StuartD 07-15-2006 07:36 AM

Quote:

Originally Posted by qw12er
header("Content-Type: application/octet-stream");

Try changing this to

header("Content-Type: application/zip");

A list of other content types can be found just over half way down the page here:
http://www.expertsrt.com/tutorials/M...P_headers.html

qw12er 07-15-2006 07:55 AM

Was a nice Idea but it doesn't works either :-(

Ace_luffy 07-15-2006 07:57 AM

bump.....

duckduckgoose 07-15-2006 09:09 AM

If I open a zip file when prompted by my browser during testing (rather than save it), it opens fine for me (using WinRAR as my archiver on my pc here). The same zip file also opens fine if I actually save it to disk when prompted by the browser. This is using Firefox v1.5.0.4. The code does not work properly at all for me in IE6, but rather gives a browser error after trying to send the page (whatever.php where your function is sitting) itself.

My guess is that your specific combination of headers used so as not to cache the file being downloaded are the problem for you. You're telling the browser not to cache a copy of the file, which is fine if you're saving it to disk locally. However, if you try and have the browser just open the file without having cached it, it will probably choke depending on browser behaviour. There are varying combinations of http headers you can use in your PHP scripts to avoid having files be cached on the local browser; some of them are browser specific (IE one way, most other browsers another way), so on past scripts i can remember doing a check for IE before sending appropriate headers for tools like yours.

Read through the PHP documentation on headers for more info :
http://ca3.php.net/header

qw12er 07-15-2006 11:00 AM

Your right ! it seems to works fine with FireFox.

It try a lot of cache-related header but I can't seem to get IE to works. I just don't get it !

mrthumbs 07-15-2006 11:06 AM

why not a simple redirect to a physical file location?

qw12er 07-15-2006 11:19 AM

Quote:

Originally Posted by mrthumbs
why not a simple redirect to a physical file location?

because I want to delete the file as soon as it has been downloaded.
Impossible to do if you redirect to the file...

Big_Red 07-15-2006 11:21 AM

bump. php pro's time to step up! :thumbsup good luck qw12er. :)

qw12er 07-15-2006 01:37 PM

thanks for all the bump.

bump for myself :-)

qw12er 07-15-2006 06:37 PM

bump
5678

qw12er 07-16-2006 07:24 AM

Sorry to bump this an other time but I realy need a solution :-(

Damian_Maxcash 07-16-2006 07:47 AM

Quote:

Originally Posted by qw12er
Sorry to bump this an other time but I realy need a solution :-(

Did you check out the mime type thing?

The more I think about it the I think that is the issue.

If you dont have access to your Apache configs then try this in a .htaccess file

AddType zip file zip

BigBen 07-16-2006 01:25 PM

Quote:

Originally Posted by qw12er
Sorry to bump this an other time but I realy need a solution :-(

Try the forums at devshed or webmasterworld.

duckduckgoose 07-16-2006 05:04 PM

If you replace all of the header() lines you used with the following ones, it works correctly in IE6 and Firefox.

header('Cache-Control: ');
header('Pragma: ');
header("Content-Type: application/force-download");
header("Content-length: " . (string)(filesize($path)));
header("Content-Disposition: attachment; filename=" . $path);


Test it yourself and see how it works out for you.

qw12er 07-16-2006 07:30 PM

I don't understand why but it does works !!!

Thanks DuckDuckGoose.


All times are GMT -7. The time now is 03:24 PM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123