SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Download class works fine, except for images

    Which it gives out a red x in the temp files it shows up (in ie8).

    Code PHP:
    <?php
     
     
    class Download {
     
     
    	public $path 				= NULL;
    	public $contentType 		= NULL;
    	public $contentDisposition 	= NULL;
     
     
    	function __construct($path, $filename) {
    		$this->path 				= $path;
    		$this->filename				= $filename;
    		$this->contentType 			= mime_content_type($path);#$contentType;
    		$this->contentDisposition 	= "attachment";#$contentDisposition;
    	}
     
     
    	public function Exists() {
     
    		if (file_exists($this->path)) { 
    			return true;
    		} else {
    			return false;
    		}
    	}
     
     
    	public function Size() {
     
    		if ($this->Exists()) {
    			return filesize($this->path);
    		} else {
    			return false;
    		}
    	}
     
     
    	public function Permit_Other() {
    		return substr(decoct(fileperms($this->path)), -1);
    	}
     
     
    	public function Init() {
     
    		if ($this->Exists() && $this->Permit_Other() >= 4) {
    			header("Content-type: ".$this->contentType);
    			header("Content-Disposition: ".$this->contentDisposition."; filename=\"".$this->filename."\"");
    			header("Content-Length: ".$this->Size());
    			$fp = readfile($this->path, "r");
    			return $fp;
    		} else { 
    			return false;
    		}
    	}
     
     
    }
     
     
    ?>

    Help?

  2. #2
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A couple trouble shooting questions:

    1. If you have the program return just the headers (as text) instead of the file, does it say what you expect?
    2. Are you saying those same images show up in Firefox?

  3. #3
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firefox downloads a broken image. IE opens up a broken image.

    PDF works fine, which is important, but occasionally it needs to display images.

  4. #4
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I noticed on the PHP "readfile" page that the argument "r" is not necessary and that they gave the Content-Type header as "application/octet-stream" for all attachments.

    http://www.php.net/readfile

  5. #5
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have updated the Init function. I still have the same issue: PDF work fine and images come corrupt.

    PHP Code:
        public function Init() {

            if (
    $this->Exists() && $this->Permit_Other() >= 4) {
                
    #header("Content-type: ".$this->contentType);
                #header("Content-Disposition: ".$this->contentDisposition."; filename=\"".$this->filename."\"");
                #header("Content-Length: ".$this->Size());
                #$fp = readfile($this->path, "r");
                #return $fp;

                
    ob_start();
                
    header("Cache-Control: public, must-revalidate");
                
    header("Pragma: no-cache");
                
    header("Content-Description: File Transfer");
                
    header('Last-Modified: '.date('r'));
                
    header('Content-type: '.$this->contentType);
                
    header("Content-Disposition: ".$this->contentDisposition."; filename=".$this->filename.";");
                
    header("Content-Transfer-Encoding: binary");
                
    header("Content-Length: ".$this->Size());
                
    ob_flush();
                exit();
            } else { 
                return 
    false;
            }
        } 

  6. #6
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    class Download
    {
        public 
    $path                NULL;
        public 
    $contentType         NULL;
        public 
    $contentDisposition  NULL;


        public function 
    __construct($path$filename)
        {
            
    $this->path                $path;
            
    $this->filename            $filename;
            
    $this->contentType         mime_content_type($path);#$contentType;
            
    $this->contentDisposition  "attachment";#$contentDisposition;
        
    }


        public function 
    Exists()
        {

            if (
    file_exists($this->path)) {
                return 
    true;
            } else {
                return 
    false;
            }
        }


        public function 
    Size()
        {

            if (
    $this->Exists()) {
                return 
    filesize($this->path);
            } else {
                return 
    false;
            }
        }


        public function 
    Permit_Other()
        {
            return 
    substr(decoct(fileperms($this->path)), -1);
        }


        public function 
    Init()
        {

            if (
    $this->Exists() && $this->Permit_Other() >= 4) {
                
    header("Content-type: ".$this->contentType);
                
    header("Content-Disposition: ".$this->contentDisposition."; filename=\"".$this->filename."\"");
                
    header("Content-Length: ".$this->Size());
                
    readfile($this->path);
                exit;
            } else {
                return 
    false;
            }
        }
    }

  7. #7
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Still corrupt. I've gone for a different solution, not ideal but i don't have much more time to work on this. Means to an end.

    PHP Code:
    <?php


    class Download {


        public 
    $path                 NULL;
        public 
    $contentType         NULL;
        public 
    $contentDisposition     NULL;


        function 
    __construct($path$filename) {
            
    $this->path                 $path;
            
    $this->filename                $filename;
            
    $this->contentType             mime_content_type($path);#$contentType;
            
    $this->contentDisposition     "attachment";#$contentDisposition;
        
    }


        public function 
    Exists() {

            if (
    file_exists($this->path)) { 
                return 
    true;
            } else {
                return 
    false;
            }
        }


        public function 
    Size() {

            if (
    $this->Exists()) {
                return 
    filesize($this->path);
            } else {
                return 
    false;
            }
        }


        public function 
    Permit_Other() {
            return 
    substr(decoct(fileperms($this->path)), -1);
        }


        public function 
    Init() {

            if (
    $this->Exists() && $this->Permit_Other() >= 4) {
                if (
    ereg ("^image"$this->contentType)) {
                    echo 
    '<img src="'.SITE_URL.$this->path.'" alt="" title="" />';
                    exit();
                } else {
                    
    ob_start();
                    
    header("Cache-Control: public, must-revalidate");
                    
    header("Pragma: no-cache");
                    
    header("Content-Description: File Transfer");
                    
    header('Last-Modified: '.date('r'));
                    
    header('Content-Type: '.$this->contentType);
                    
    header('Content-Disposition: '.$this->contentDisposition.'; filename="'.$this->filename.'"');
                    
    header("Content-Transfer-Encoding: binary");
                    
    header("Content-Length: ".$this->Size());
                    
    readfile($this->path);
                    
    ob_flush();
                    exit();
                }
            } else { 
                return 
    false;
            }
        }


    }


    ?>

  8. #8
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's strange, it worked for me. Is there a file name conflict?

  9. #9
    Non-Member
    Join Date
    Mar 2009
    Location
    Ontario
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are doing it in Windows or Linux or may be Mac? This is very important to understand because I faced the same problem, in windows my Images were fine but in Linux they were distorted. I was configuring script called VieleRETS

  10. #10
    SitePoint Addict Trent Reimer's Avatar
    Join Date
    Sep 2005
    Location
    Canada
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by adguru.org View Post
    You are doing it in Windows or Linux or may be Mac? This is very important to understand because I faced the same problem, in windows my Images were fine but in Linux they were distorted. I was configuring script called VieleRETS
    That's a good thing to consider. I was using Ubuntu Linux.

    I didn't mean to sound insulting with my last question, it's just that sometimes anyone will make a typo. For example, if I do something like

    PHP Code:
    $download = new Download('picture.gif''picture.jpg');
    $download->Init(); 
    The image will download but if I try to open the image with an application it doesn't necessarily know what to do with it as it may be looking at the file name extension.

  11. #11
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It finds the image definetely, it was just serving it corrupt.

    I think we're on Windows machines. The current solutions fine, I've been given my next project so I can't really fix this any further.

  12. #12
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I would guess you have erroneous white space located in your image data, other files are little more forgiving in my experience with this issue.

    Try this:-

    PHP Code:
    header("Cache-Control: public, must-revalidate");
    header("Pragma: no-cache");
    header("Content-Description: File Transfer");
    header('Last-Modified: '.date('r'));
    header('Content-Type: '.$this->contentType);
    header('Content-Disposition: '.$this->contentDisposition.'; filename="'.$this->filename.'"');
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".$this->Size());
    ob_start();
    readfile($this->path);
    $sFileData ob_get_contents();
    ob_end_clean();
    echo 
    $sFileData;
    exit(); 
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  13. #13
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That didnt work either.

    The image in question is: http://v2.myvouchercodes.co.uk/publi...hers/46222.png

    but all images come out broken anywho

  14. #14
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Is this file local or remote?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  15. #15
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    319
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Remote, it would be local to the script though.


Bookmarks

Posting Permissions

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