+ Reply to Thread
Results 1 to 4 of 4

Thread: Simple file uploader

  1. #1
    Jacki's Avatar
    Jacki is offline Learning Programmer
    Join Date
    Sep 2009
    Location
    Switzerland
    Posts
    80
    Rep Power
    9

    Wink Simple file uploader

    Hello, today I'm going to explain you how you can make a simple file uploader.

    First you have to know that the maximum upload size is definited in the php.ini (upload_max_filesize) of your host, so if you want to upload bigger files you have to change the upload_max_filesize value.

    Ok, we're ready to start to write our file uploader:
    The most important thing is the form with wich we will send our files to the php code:
    HTML Code:
    <form method=\"post\" action=\"upload.php\" enctype=\"multipart/form-data\">
    	File to upload: <input type=\"file\" name=\"file\"> <input type=\"submit\" name=\"submit\" value=\"Upload\"> 
    </form>
    Now we have to define the maximum size of the file to be uploaded:
    Code:
    define("MAX_FILE_SIZE"10485760); // Maximum file size is 10 mb, 10485760 is in bytes 
    The php files variable that we need are:
    Code:
    $file_name    $_FILES['file']['name']; // File name
    $file_size    $_FILES['file']['size']; // File size
    $file_temp    $_FILES['file']['tmp_name']; // Our temporaney file
    $file_dest    "files/".basename($file_name); // File destination 
    but these will be also useful:
    Code:
    $file_type    $_FILES['file']['type']; // The file type, if we want to upload a determinated type of file
    $file_error $_FILES['file']['error']; // The files errors 
    Now we need the main function with wich we will upload the file (move_uploaded_file):
    Code:
        if ($file_size <= MAX_FILE_SIZE) {
            
    // Uploading file
            
    if(move_uploaded_file($file_temp$file_dest))
                echo 
    'File uploaded: '.$file_dest;
            else
                echo 
    'Error: '.$file_error;
        } else {
            echo 
    "This file is too big!";
        } 
    This is the full code:
    Code:
    <?php
    define
    ("MAX_FILE_SIZE"10485760); // Maximum file size is 10 mb, 10485760 is in bytes

    if(isset($_POST['submit'])) {
        
    // Files variables
        
    $file_name    $_FILES['file']['name']; // File name
        
    $file_size    $_FILES['file']['size']; // File size
        
    $file_type    $_FILES['file']['type']; // The file type, if we want to upload a determinated type of file
        
    $file_temp    $_FILES['file']['tmp_name']; // Our temporaney file
        
    $file_error $_FILES['file']['error']; // The files errors
        
    $file_dest    "files/".basename($file_name); // File destination
        
        
    if ($file_size <= MAX_FILE_SIZE) {
            
    // Uploading file
            
    if(move_uploaded_file($file_temp$file_dest))
                echo 
    'File uploaded: '.$file_dest;
            else
                echo 
    'Error: '.$file_error;
        } else {
            echo 
    "This file is too big!";
        }

    } else {
        
    // Uploading form
        
    echo "<form method=\"post\" action=\"upload.php\" enctype=\"multipart/form-data\">
                File to upload: <input type=\"file\" name=\"file\"> <input type=\"submit\" name=\"submit\" value=\"Upload\"> 
             </form>"
    ;
    }
    ?>
    I hope as always it will be useful.. Have fun!

    Bye!



  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jacki's Avatar
    Jacki is offline Learning Programmer
    Join Date
    Sep 2009
    Location
    Switzerland
    Posts
    80
    Rep Power
    9

    Re: Simple file uploader

    Do you have any opinion or suggestion?



  4. #3
    Jordan Guest

    Re: Simple file uploader

    Well done! +rep.
    I recommend not using isset to test $_POST values though. I wrote a blog post explaining why here: http://forum.codecall.net/blogs/jord...ing-isset.html

  5. #4
    Join Date
    Jun 2010
    Posts
    3
    Rep Power
    0

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Permission denied in Maxs image uploader
    By onething in forum PHP Development
    Replies: 7
    Last Post: 08-10-2011, 07:00 AM
  2. how to make a website uploader in vb 6.0?
    By darkneo10 in forum Visual Basic Programming
    Replies: 1
    Last Post: 01-28-2010, 05:46 AM
  3. [PHP] Imageshack uploader
    By luruke in forum Classes and Code Snippets
    Replies: 5
    Last Post: 02-23-2009, 03:13 AM
  4. Replies: 1
    Last Post: 01-09-2009, 05:51 AM

Tags for this Thread

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