File uploading

PythonPython Forum LeaderThe Royal RAM
Im interested in learning about file uploading and then doing some simple stuff with the file.

Basically I want a form which uploads a file to a server but it gives it a unique filename of random characters.

I need it to insert the original file name, new randomized filename, filesize into a MySql database.

Any help or tutorials?
Thanks

The Royal Ram

Comments

  • ChroderChroder Senior Member The Royal RAM
    It's fairly easy. $_FILES is an array that holds any uploaded files. Each item in $_FILES has:
    • name - The name of the file
    • type - The mime type
    • size - The filesize in bytes
    • tmp_name - The path to the temp location it was uploaded to

    Basically all you have to do is move the file from the temp location to where you want it. PHP provides move_uploaded_file specifically for this (while also making sure it is a valid uploaded file [security]).

    Here's an example page I made up for you. Note that form has enctype="multipart/form-data". This is to tell PHP to process the upload. Otherwise, you'd need to manually parse $HTTP_RAW_POST_DATA -- eew!

    In this example, it uploads the files to ./uploads/. The filename is a pretty random 32 character (used by md5'ing a combo of the md5 check sum of the file itself, and a random integer from 1-9999).

    [php]<?php

    $page =& new uploadForm();


    /**
    * Container class for the upload page
    */
    class uploadForm
    {
    /**
    * Directory to save files to
    * @access private
    * @var string
    */
    var $save_dir;





    /**
    * Constructor; Decide on which action to take
    * @param string $save_dir The directory to save files to (with trailing slash)
    */
    function uploadForm($save_dir = './uploads/')
    {
    $this->save_dir = $save_dir;

    if(isset($_FILES) && is_uploaded_file($_FILES))
    $this->processUpload();
    else
    $this->showForm();
    }





    /**
    * Process any uploaded files
    * @access private
    */
    function processUpload()
    {
    $tmp_path = $_FILES;

    $old_name = $_FILES;
    $new_name = $this->genRandName($tmp_path, $old_name);
    $filesize = filesize($tmp_path);

    move_uploaded_file($tmp_path, $this->save_dir . $new_name);

    // Now insert $old_name, $new_name and $filesize into the DB

    echo "<h1>Thank You</h1>";
    }




    function genRandName($tmp_path, $filename)
    {
    $ext = '.' . substr(strrchr($filename, "."), 1);

    do
    {
    $new_name = md5( md5_file($tmp_path) . mt_rand(1, 9999) . time() ) . $ext;
    } while(file_exists($this->save_dir . $new_name));

    return $new_name;
    }





    /**
    * Show the upload form
    * @access private
    */
    function showForm()
    {
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">;
    <html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">;
    <head>
    <title>Upload</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />

    <style type="text/css">
    body {
    background-color: #ebe9ed;
    margin: 0;
    padding: 0;
    text-align: center;
    }

    body, span, div, table, thead, tfoot, tbody, tr, td {
    font: normal 9pt Verdana, Geneva, Arial, Helvetica, sans-serif;
    color: #000;
    }

    #mainwrap {
    background-color: #dadada;
    border-bottom: 1px solid #535353;
    border-right: 1px solid #898989;
    margin: 18px auto 18px auto;
    padding: 8px;
    width: 500px;
    text-align: left;
    }

    #content {
    background-color: #fff;
    border-left: 1px solid #6e6e6e;
    border-top: 1px solid #6e6e6e;
    padding: 15px;
    }
    </style>
    </head>
    <body>
    <div id="mainwrap">
    <div id="content">
    <form action="<?php echo $_SERVER; ?>" method="post" enctype="multipart/form-data">
    <h3>Choose a file to upload</h3>
    <div style="margin-left:15px;">
    <input type="file" name="upfile" size="40" style="font-size: 11px;" />
    <input type="submit" value="Upload" style="font-size: 11px; font-weight: bold;" />
    </div>
    </form>
    </div>
    </div>
    </body>
    </html>

    <?php
    }
    }

    ?>[/php]
  • PythonPython Forum Leader The Royal RAM
    wow... Thanks for that. You are a true webmaster :)

    The Royal Ram

Sign In or Register to comment.