#1
  1. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,916
    Rep Power
    533

    Stategies to upload, save, and display files


    To upload and download files, I do as shown below the asterisk line.

    First question. Do you see any problems with my approach?

    Second question, I am having problems with Excel 2007 files. fileinfo provides the same mime types for both Excel 2003 xls, and Excel 2007 xlsx. Earlier versions of PHP use to provide different mimes, but it was changed PHP >= 5.3.11 and >= 5.4.1. So, when downloading a xlsx file, Excel warns the user that the file is corrupt since the mime is Excel 2003 instead of 2007. docx also have the wrong mime, but for some reason, MS Word doesn't indicate an error. I guess I can just create some one-off code for MS Word/Excel/PowerPoint files, but would rather not as I will need to constantly maintain if changes are ever made. Any recommendations?

    Thank you
    *********************************************

    To upload and save a file, I do the following:
    1. Upload a file.
    2. Use finfo to determine the file type.
      PHP Code:
      $finfo finfo_open(FILEINFO_MIME_TYPE);
      $mimi=finfo_file($finfo$filename); 
    3. Based on the file extension, determine the file type
      PHP Code:
      static public function getFileInfo($fileExt)
      {
        
      //Given file extention, returns array of MIMI, the icon image, and the icon filename
        
      $array=array();
        
      $array['icon']='generic.png';
        
      $array['mimi']='application/force-download';
        switch (
      strtolower($fileExt))
        {
          case 
      "pdf"$array['mimi']="application/pdf"$array['icon']='pdf.png';break;
          case 
      "jpeg":
          case 
      "jpg"$array['mimi']="image/jpg"$array['icon']='jpg.png';break;
          
      //etc, etc....
        
      }
        
      $array['icon']='lib/stdimages/icons/32x32/'.$array['icon'];
        
      $array['icon_html']='<img src="'.$array['icon'].'" width="32" height="32" alt="'.$array['icon'].'"/>';
        return 
      $array;        

    4. If the two methods above indicate the same file type, store the filename in a database, rename the filename to the record's autoincrementing PK, and move it to its permanent location. If different, quit.


    To download a file, I do not bother with checking file type since it was already confirmed when uploaded, and just set the mime based on the extension.
    PHP Code:
    public function dl_file($file_id,$file_name)
    {
      if (!
    is_file($file_id)) { die("<b>404 File not found!</b>"); }
      
    $fileInfo=library::getFileInfo(library::getExt($file_name));
      
    header('Pragma: public');
      
    header('Expires: 0');
      
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      
    header('Cache-Control: public');
      
    header('Content-Description: File Transfer');
      
    //Use the switch-generated Content-Type
      
    header('Content-Type: '.$fileInfo['mimi']);
      
    header('Content-Disposition: attachment; filename='.$file_name.';' );
      
    header('Content-Transfer-Encoding: binary');
      
    header('Content-Length: '.filesize($file_id));
      @
    readfile($file_id);

    Last edited by NotionCommotion; January 11th, 2013 at 06:59 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,916
    Rep Power
    533
    Any suggestions or comments would be appreciated!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,916
    Rep Power
    533
    I don't want to come across whiny but was really hoping for some help. If you didn't think my original question warranted a response, please let me know whine (I mean why ).
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,916
    Rep Power
    533
    Really, not one response?

IMN logo majestic logo threadwatch logo seochat tools logo