Hi, I am trying to create a file uploading script so that people can upload a file to a url, and give it to their friends or access it from another computer. I also want them to be able to view ALL uploads from everyone. It is done by showing the contents of the folder that files are uploaded to. Can someone possibly find me a script that can tell what type (jpg, mp3, txt, etc) the files are in the folder, and based on that, give a preview...
EXAMPLE:
File = test.jpg
Result = <img src="test.jpg" width="50" height="50">
File = test.mp3
Result = <embed src="test.mp3">

Thanks!

    This is a snippet from a project I'm working on. This serves files from a given directory and should get you quite far.

    Tom

    
    public function externalAction() {
        $name = basename($this->_request->getParam('name'));
        $group = basename($this->_request->getParam('group'));
        $file = basename($this->_request->getParam('file'));
    
        $file = basename($file); # for security, only use the path name below
        $path = APPLICATION_PATH . "/Plugins/$group/$name/External/" . $file;
    
        if (!file_exists($path)) $path = APPLICATION_PATH . '/../public/images/1px.gif';
        header('Content-Length:' . @filesize($path));
        header('Content-Type: ' . $this->get_mime_type($path));
        header('Content-Disposition:inline; filename="' . basename($path) . '"');
        // Give correct mime type
    
        if ($this->config->useSendfile) {
            header("X-Sendfile:" . $path);
        } else {
            readfile($path);
        }
    }
    
    /**
     * From: http://www.darian-brown.com/php-function-to-get-file-mime-type/
     *
     * There is certainly a better way to get this.  Using php finfo_file didn't
     * find text/css for .css
     */
    private function get_mime_type($file)
    {
    
        // our list of mime types
        $mime_types = array(
                "pdf"=>"application/pdf"
                ,"exe"=>"application/octet-stream"
                ,"zip"=>"application/zip"
                ,"docx"=>"application/msword"
                ,"doc"=>"application/msword"
                ,"xls"=>"application/vnd.ms-excel"
                ,"ppt"=>"application/vnd.ms-powerpoint"
                ,"gif"=>"image/gif"
                ,"png"=>"image/png"
                ,"jpeg"=>"image/jpg"
                ,"jpg"=>"image/jpg"
                ,"mp3"=>"audio/mpeg"
                ,"wav"=>"audio/x-wav"
                ,"mpeg"=>"video/mpeg"
                ,"mpg"=>"video/mpeg"
                ,"mpe"=>"video/mpeg"
                ,"mov"=>"video/quicktime"
                ,"avi"=>"video/x-msvideo"
                ,"css"=>"text/css"
                ,"jsc"=>"application/javascript"
                ,"js"=>"application/javascript"
                ,"php"=>"text/html"
                ,"htm"=>"text/html"
                ,"html"=>"text/html"
        );
    
        $parts = explode('.',$file);
        $end = end($parts);
        $extension = strtolower($end);
    
        // Use default
        return ($mime_types[$extension]) ?: 'application/octet-stream';
    }
    

      Toma, Could you explain how I could get the previews fo the files? Thanks, and sorry, i just can't seem to figure it out..

      Thank you for the help!

        To generate previews of images look up imagemagick and/or gd php functions.

        If it's just a file uploaded from one person for download by others I think a preview function is way too much functionality and if you really want it do it later.

          4 days later

          Okay man when you were asking about how to get the previews for the files I hope you didn't mean how do I get that stuff to actually show up on your website... I can help you with that but if you meant how do you use his code in your code... you have to copy and paste the code after this paragraph into your .php file, your "index.php" file, or your "whatever_you_put_here.php" file. The fiel preview function is included in my code... if you want to view a test of the code I created for you, it is here:

          http://dylanmhulderman.no-ip.info/test.php

          <?php
          
          $error_message = '';
          $upload_foldername = 'uploads';
          if(isset($_FILES['uf'])) {
           if(!file_exists($upload_foldername)) {
            mkdir($upload_foldername);
           }
           else if(!is_dir($upload_foldername)) {
            mkdir($upload_foldername);
           }
           if(stristr('.avi.mov.mpe.mpg.mpeg.txt.jpg.gif.png.jpeg.bmp.dcs.eps.ham.iff.lzw.pcx.pict.pixar.rle.tga.tiff.mp3.wma.', strtolower(substr($upload_foldername.'/'.basename($_FILES['uf']['name']), strrpos($upload_foldername.'/'.basename($_FILES['uf']['name']), '.') + 1, strlen($upload_foldername.'/'.basename($_FILES['uf']['name'])))))) {
            if(move_uploaded_file($_FILES['uf']['tmp_name'], $upload_foldername.'/'.basename($_FILES['uf']['name']))) {
             header('Location:'.basename($_SERVER['PHP_SELF']));
             exit;
            }
            else {
             $error_message = 'Your file failed to upload...';
            }
           }
           else {
            $error_message = 'You cannot upload that type of file. (<b>'.strtolower(substr($upload_foldername.'/'.basename($_FILES['uf']['name']), strrpos($upload_foldername.'/'.basename($_FILES['uf']['name']), '.') + 1, strlen($upload_foldername.'/'.basename($_FILES['uf']['name'])))).'</b>)';
           }
          }
          
          ?>
          <html>
           <head>
            <title>My Uploading Script</title>
            <style type="text/css">
             BODY { margin: 10px; padding: 0px; background: #FFFFFF; color: #000000; }
             BODY, TABLE, TR, TD { font-family: Verdana; font-size: 12px; color: #000000; }
             .fileinput { font-family: Verdana; font-size: 10px; padding: 4px; }
             .submitinput { font-family: Verdana; font-size: 10px; padding: 4px; }
            </style>
           </head>
           <body>
          
          <?php
          
          if($error_message != '') {
           echo('<div style="width:500px;border:1px solid #C0C0C0;background:#F0F0F0;padding:1px;margin:0px;">
           <table width="100%" cellspacing="1px" cellpadding="5px">
            <tr>
             <td valign="top" align="left" style="background:#FCFCFC;">'.$error_message.'</td>
            </tr>
           </table>
          </div><br />');
          }
          
          if(isset($_GET['vu']) && isset($_GET['un'])) {
           if(file_exists($upload_foldername.'/'.$_GET['un'])) {
            switch(strtolower(substr($upload_foldername.'/'.$_GET['un'], strrpos($upload_foldername.'/'.$_GET['un'], '.') + 1, strlen($upload_foldername.'/'.$_GET['un'])))) {
             default: $vuc = ''; break;
             case 'txt': $vuc = htmlentities(file_get_contents($upload_foldername.'/'.$_GET['un'])); break;
             case 'jpg': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'gif': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'png': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'jpeg': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'bmp': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'dcs': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'eps': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'ham': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'iff': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'lzw': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'pcx': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'pict': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'pixar': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'rle': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'tga': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'tiff': $vuc = '<img src="'.$upload_foldername.'/'.$_GET['un'].'" />'; break;
             case 'mp3': $vuc = '<embed src="'.$upload_foldername.'/'.$_GET['un'].'"></embed>'; break;
             case 'wma': $vuc = '<embed src="'.$upload_foldername.'/'.$_GET['un'].'"></embed>'; break;
             case 'mpeg': $vuc = '<embed src="'.$upload_foldername.'/'.$_GET['un'].'"></embed>'; break;
             case 'mpg': $vuc = '<embed src="'.$upload_foldername.'/'.$_GET['un'].'"></embed>'; break;
             case 'mpe': $vuc = '<embed src="'.$upload_foldername.'/'.$_GET['un'].'"></embed>'; break;
             case 'mov': $vuc = '<embed src="'.$upload_foldername.'/'.$_GET['un'].'"></embed>'; break;
             case 'avi': $vuc = '<embed src="'.$upload_foldername.'/'.$_GET['un'].'"></embed>'; break;
            }
            echo('<a href="javascript:history.go(-1);">Go Back</a><br /><br />'.$vuc.'
          
           </body>
          </html>');
            exit;
           }
          }
          
          ?>
          
          <form enctype="multipart/form-data" action="<?php echo(basename($_SERVER['PHP_SELF'])); ?>" method="POST">
           <div style="width:500px;border:1px solid #C0C0C0;background:#F0F0F0;padding:1px;margin:0px;">
            <table width="100%" cellspacing="1px" cellpadding="5px">
             <tr>
              <td width="50%" valign="middle" align="left" style="background:#FCFCFC;">Your File:</td>
              <td width="50%" valign="middle" align="left" style="background:#FCFCFC;"><input type="file" name="uf" id="uf" size="30" value="" class="fileinput" /></td>
             </tr>
             <tr>
              <td colspan="2" valign="middle" align="center" style="background:#FCFCFC;"><input type="submit" class="submitinput" value="Upload" /></td>
             </tr>
            </table>
           </div>
          </form>
          
          <div style="width:500px;border:1px solid #C0C0C0;background:#F0F0F0;padding:1px;margin:0px;">
           <table width="100%" cellspacing="1px" cellpadding="5px">
            <tr>
             <td width="50%" valign="top" align="left" style="background:#FCFCFC;">File Uploads:</td>
            </tr>
          <?php
          
          $upload_scan = scandir($upload_foldername);
          $upload_scan_count = count($upload_scan);
          for($a = 0;$a < $upload_scan_count;$a++) {
           if($upload_scan[$a] != '.' && $upload_scan[$a] != '..') {
            echo('  <tr>
             <td width="50%" valign="top" align="left" style="background:#FCFCFC;"><a href="'.basename($_SERVER['PHP_SELF']).'?vu=1&un='.urlencode(basename($upload_scan[$a])).'">'.htmlentities($upload_scan[$a]).'</a></td>
            </tr>');
            }
          }
          
          ?>
           </table>
          </div>
          
           </body>
          </html>
            Write a Reply...