...

View Full Version : Simple script stupid results



in2mobi
08-23-2009, 04:20 AM
I have searched these forms up and down but can't seem to find the answer to what I am doing wrong in my script. What I am trying to accomplish is for my script to grab files that are outside of document root. Now, I have been able to grab text based files that reside outside of document root, but when I try to grab binary files that reside outside document root all I get in return is a screen full of funny looking characters. Here goes the simple script that I have created, because I have had no luck with adding to much logic to it because I seem to get the same results.


function outPutFile(){
$my_file = 'c:\wamp\www\horebgroup\Graphics\PNG\HorebLogoLtBlueRed.png';
if(file_exists($my_file)){
$file_open = fopen($my_file, "rb");
while(!feof($file_open))
echo fgets($file_open);
}else{
echo "File Doesn't Exists";
}
}//closes outPutFile()


I use this function farther down on the page, and it seems that when it goes to the browser it throws a fit. In previous code I had a header method that would let the browser know that the file was of image/png, but then the script stopped working all together. Any pointers on how to make this work better is greatly appreciated. I would like to be able to store my site graphics outside of document root and just have a script that calls the pieces from their folder as opposed to storing the files inside document root.
Thank You

seco
08-23-2009, 04:45 AM
Try adding


header("Content-Type: image/png");

in2mobi
08-23-2009, 10:51 AM
Seco, I added it, but nothing changed. It actually made the script time out. I guess I will just have to go about this issue another way. Thanks for the help Seco

_Aerospace_Eng_
08-23-2009, 05:57 PM
Are you just trying to get them to show on your page or are you trying to get them to open the download prompt?

This might help you. This code will pull files from whatever directory you wish to use and display them in the browser accordingly. Change the $path to whatever directory you want to use.

<?php
// place this code inside a php file and call it f.e. "display.php"
$path = "c:/wamp/www/horebgroup/Graphics/PNG/"; // play with the path if the document root does noet exist
$fullPath = $path.$_GET['file'];

if ($fd = fopen ($fullPath, "r")) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "png":
header("Content-type: image/png"); // add here more headers for diff. extensions
break;
case "jpg":
header("Content-type: image/jpg"); // add here more headers for diff. extensions
break;
case "gif":
header("Content-type: image/gif");
break;
case "mp3":
header("Content-type: audio/mpeg");
break;
case "pdf":
header("Content-type: application/pdf");
break;
default;
header("Content-type: application/octet-stream");
}
header("Content-length: $fsize");
header("Cache-control: private"); //use this to open files directly
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
// example: place this kind of link into your document where the download is shown:
// <a href="display.php?file=some_file.pdf">Download here</a>
?>

Then you can use this so for example your png graphic would be like this

<img src="display.php?file=HorebLogoLtBlueRed.png" alt="">

If you wanted to create a script that prompted the open/save box for the user then you need to simply add one line. You would change this part

switch ($ext) {
case "png":
header("Content-type: image/png"); // add here more headers for diff. extensions
break;
case "jpg":
header("Content-type: image/jpg"); // add here more headers for diff. extensions
break;
case "gif":
header("Content-type: image/gif");
break;
case "mp3":
header("Content-type: audio/mpeg");
break;
case "pdf":
header("Content-type: application/pdf");
break;
default;
header("Content-type: application/octet-stream");
}
to this

switch ($ext) {
case "png":
header("Content-type: image/png"); // add here more headers for diff. extensions
break;
case "jpg":
header("Content-type: image/jpg"); // add here more headers for diff. extensions
break;
case "gif":
header("Content-type: image/gif");
break;
case "mp3":
header("Content-type: audio/mpeg");
break;
case "pdf":
header("Content-type: application/pdf");
break;
default;
header("Content-type: application/octet-stream");
}
header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachement' to force a download



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum