Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post

    Simple script stupid results

    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.
    Code:
    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

  • #2
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    Try adding
    PHP Code:
    header("Content-Type: image/png"); 

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    30
    Thanks
    10
    Thanked 1 Time in 1 Post
    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

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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 Code:
    <?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($fd2048);
            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
    Code:
    <img src="display.php?file=HorebLogoLtBlueRed.png" alt="">
    Edit: 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
    PHP Code:
    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
    PHP Code:
    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 
    Last edited by _Aerospace_Eng_; 08-24-2009 at 08:15 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • Users who have thanked _Aerospace_Eng_ for this post:

    in2mobi (08-24-2009)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •