fopen corrupt file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ShadoFlameX
Forum Newbie
Posts: 2
Joined: Sat Jul 10, 2004 12:52 am

fopen corrupt file

Post by ShadoFlameX »

Hello

I am trying to get a script to download files from a "hidden" location working. My problem is that if the file I want to fopen is located in a directory other than where this script php file is located I get a corrupted file which will not open. For instance if I change "$fp2 = fopen("/Users/smith/Sites/main/vSignup_2/".$filename, 'rb');" to "$fp2 = fopen("/Users/smith/Sites/main/vSignup_2/pdf/".$filename, 'rb');" then it will not work. (Yes I have the file in the right location for each instance) My code is as follows:

Code: Select all

if ($_GETї"id"]) {
	$pdfID = $_GETї"id"];
	$userID = $checkї"id"];
	$result = mysql_query("	SELECT pdfName, pdfLoc
							FROM pdffile INNER JOIN pdfpurchase ON pdffile.id = pdfpurchase.pdffileID
							WHERE pdfpurchase.authuserID='$userID' AND pdfpurchase.pdffileID='$pdfID'	");
	$row = mysql_fetch_array($result);
	if ($row != NULL) {
		$filename = $rowї'pdfLoc'];
		header("Content-type: application/octet-stream");
		header("Content-Length: ".filesize($filename));
		header("Content-Disposition: attachment; filename=$filename");
		$fp2 = fopen("/Users/smith/Sites/main/vSignup_2/".$filename, 'rb');
		fpassthru($fp2);
		fclose($fp2);
	}
	else {
		echo "No MySQL row returned";
	}
}
else {
	echo "Page entered w/o ID num";
}
Any help would be great
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Do you have permission to read in that dir and the file itself? If the file is in the directory below the script that you can just reference it with "pdf/file.pdf";

Try to place the file name into a string first and then echo it out to make sure everything is ok.
ShadoFlameX
Forum Newbie
Posts: 2
Joined: Sat Jul 10, 2004 12:52 am

Post by ShadoFlameX »

Yes, there was a minor problem with the file path afterall, however, I had disabled display errors and with this off the file was downloading and then trying to open. For some reason a lot of binary data (78k) was getting DLed which led to the confusion. Thanks for the help tho, Cheers
Post Reply