SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: File upload

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    File upload

    Hi,

    I am using the following code to upload files limited to zip or rar, upon upload I have it set to rename the uploaded files to the id of the record placed in the database adding the file extension onto that.

    Problem is that it leaves the file extension off ie

    1
    2
    etc
    etc

    no file extension on the uploaded file.

    also when it inserts the data (filename) into the database the file extension is marked as ie

    1.x-zip-compressed
    2.x-zip-compressed

    I am wanting to have a page that allows people to download the files using links and need the file extensions to be ie

    1.rar
    2.zip
    etc
    etc

    depending on the file type uploaded.

    code:

    PHP Code:
    $file_dir  "files";
    $counter 0;

    $allowed_file_types = array(
    'application/x-zip-compressed' => 'x-zip-compressed');

    $file $_FILES['file'];
            
        while(
    $counter <= count($file))
    {
        if(
    $file['size'][$counter] > 0)
    {
        if(!
    array_key_exists($file['type'][$counter], $allowed_file_types))
    {
        echo 
    'File '.($counter+1).' is not a accepted file type!<br />';
    }else{
            @
    mysql_query("INSERT INTO filename (filename) VALUES ('0')");
            
    $new_id mysql_insert_id();
            
    $filetype $file['type'][$counter];
            
    $extention $allowed_file_types[$filetype];
            
    $filename $new_id.".".$extention;

            @
    mysql_query("UPDATE filename SET modelid='".$_GET['modelid']."', filename='".$filename."', userid='".$_SESSION['userid']."' WHERE fileid='".$new_id."'");

        
    $newfile $file_dir.'/'.$filename[$counter]; 
        
    move_uploaded_file($_FILES['file']['tmp_name'][$counter], $newfile);  


     }
    }
    $counter++;

    How to solve this?


    Thanks
    Last edited by _matrix_; Apr 30, 2006 at 01:22.

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What your trying to do is very simple. I think your code might be doing a little more than you ask for. It is a little late for me (4am almost). When I get the chance tommorow I should be able to lead you on your way.

  3. #3
    SitePoint Enthusiast spamonkey8's Avatar
    Join Date
    Feb 2006
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Side note: JavaScript can be used to filter by file extensions so that unacceptable file formats don't waste your bandwidth and your users' time. They can, of course, disable scripts, so keep the PHP filtering, too.

    Google something like "JavaScript form validation file uploads"

  4. #4
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Thanks for the thought of javascript, generally I pref to use php to filter out allowed file extensions.


    Cheers

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *bump*

    Solution or rewrite?


    Thanks

  6. #6
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,


    Any ideas?


    Thnaks


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
  •