php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36734 fileopen through header
Submitted: 2006-03-14 11:10 UTC Modified: 2006-03-14 16:33 UTC
From: tgal at rdslink dot ro Assigned:
Status: Not a bug Package: HTTP related
PHP Version: 4.4.2 OS: WindowsXP
Private report: No CVE-ID: None
 [2006-03-14 11:10 UTC] tgal at rdslink dot ro
Description:
------------
Under php-4.3.10 this method works just fine. But with php-4.4.1 and php-4.4.2 the application crashes(mostly MS Word, because I try to open rtf documents and this is the default application).
The script uses header function to get the document from server.

Reproduce code:
---------------
to get the file:
<a href="fileopen.php?folder=subfolder&filename=file.rtf">file.rtf</a>

fileopen.php:
<?
	if(!isset($_GET["folder"])) $_GET["folder"]=NULL;
	if(!isset($_GET["subfolder"])) $_GET["subfolder"]=NULL;
	if(!isset($_GET["id"])) $_GET["id"]=NULL;
	if(!isset($_GET["filename"])) $_GET["filename"]=NULL;
	if($_GET["folder"]=="somatie")
	{
		$name="../".$_GET["folder"]."/".$_GET["filename"];
	}
	else
	{
		if($_GET["subfolder"]!=NULL)
			$name="../".$_GET["folder"]."/".$_GET["subfolder"]."/".$_GET["id"].str_replace("*","&",$_GET["filename"]);
		else
			$name="../".$_GET["folder"]."/".$_GET["id"].str_replace("*","&",$_GET["filename"]);
	}
	$file_extension=strtolower(substr(strrchr($_GET["filename"],"."),1));
	switch($file_extension)
	{
	case "pdf": $ctype="application/pdf"; break;
	case "exe": $ctype="application/octet-stream"; break;
	case "zip": $ctype="application/zip"; break;
	case "doc": $ctype="application/msword"; break;
	case "csv":
	case "xls": $ctype="application/vnd.ms-excel"; break;
	case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
	case "gif": $ctype="image/gif"; break;
	case "png": $ctype="image/png"; break;
	case "jpeg":
	case "jpg": $ctype="image/jpg"; break;
	case "mp3": $ctype="audio/mpeg"; break;
	case "wav": $ctype="audio/x-wav"; break;
	case "mpeg":
	case "mpg":
	case "mpe": $ctype="video/mpeg"; break;
	case "mov": $ctype="video/quicktime"; break;
	case "avi": $ctype="video/x-msvideo"; break;

	//The following are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
	case "php":
	case "htm":
	case "html":
	case "txt": die("<b>Cannot be used for ". $file_extension ." files!</b>"); break;

	default: $ctype="application/force-download";
	}
// send the right headers
	header("Pragma: no-cache");
	header("Expires: 0");
	header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
	header("Content-Description: File Transfer");
	header("Content-Type: $ctype");
	header('Content-Disposition: attachment; filename="'.$_GET["filename"].'";');
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ".filesize($name));
// dump the picture and stop the script
	@readfile($name);
	exit;
?>


Expected result:
----------------
The file should be opened with the default application.

Actual result:
--------------
Default application crashes.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-14 11:14 UTC] tony2001@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 [2006-03-14 16:33 UTC] tgal at rdslink dot ro
I think, this is a bug. But not the header() functions, it is a bug of readfile() function.I tested it over and over, no error is reported, not even the die() stops it, after that I've changed the readfile() with ` echo file_get_contents() ` and works just perfectly. I tried to compare the to results and they don't match, readfile() doesn't get all the data and get's it wrong too.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC