[PHP] Very simple file uploader

Discussion in 'Web Programming' started by World Domination, Nov 21, 2010.

[PHP] Very simple file uploader
  1. Unread #1 - Nov 21, 2010 at 8:47 AM
  2. World Domination
    Joined:
    Apr 9, 2007
    Posts:
    1,563
    Referrals:
    3
    Sythe Gold:
    5

    World Domination Guru
    Banned

    [PHP] Very simple file uploader

    This is a very 'bare-bones' file uploader. It will take the file you specify, and upload it to your server.

    You will need to create 2 new pages. One called 'uplaoder.php' (this is the page with the HTML formatting and whatnot), and one called 'upload.php' (this page will hold the PHP coding that does the uploading).

    HTML:
    Code:
    <form action="upload.php" method="post" name="add-form" enctype="multipart/form-data">
    <label for="file">
    <div align="center"></div>
    </label>
    <div align="center"></div>
    <div align="center">
      <p class="style1">Max upload file size: 100MB<br />
        <br />
        <span class="style2">Choose a File:</span><br />
          <input type="file" name="file" id="file" />
        <br />
        <input type="hidden" name="action" value="add" /> 
          <!-- Hidden input type -->
          <input type="submit" name="submit" value="Upload" />
        </p>
      </div>
    </form>
    PHP:
    PHP:
    <?php 
    if(isset($_POST['submit'])) {
        
        if(
    $_POST['action'] == 'add') {
            
    // Folder where the file will be uploaded.
            
    $upload_path './uploads/' $_FILES['file']['name'];
            
    // Checks if the file already exists in the folder.
            
    if(!file_exists($upload_path)) {
                
    // Check the size of the file. 
                            
    if($_FILES['file']['size'] < 100000000000 && $_FILES['file']['size'] > 0) {
                    
    // If there are any errors.
                    
    if($_FILES['file']['error']) {
                        echo 
    "Error: " $_FILES['file']['error'];
                    } else {
                        
    // Success!
                        
    echo "File successfully uploaded.<br /><br />";
                        echo 
    "The filename is: <strong>" $_FILES['file']['name'] . "</strong><br />";
                        echo 
    "The filesize is: <strong>" $_FILES['file']['size'] . "</strong><br />";
                        echo 
    "The filetype is: <strong>" $_FILES['file']['type'] . "</strong><br />";
                                    
                        
    // Move uploaded file to upload folder.
                        
    move_uploaded_file($_FILES['file']['tmp_name'], $upload_path);
                    }
                } else {
                    echo 
    $_FILES['file']['size'] < 'Incorrect filename.' 'File size is too big';
                } 
            } else {
                echo 
    "File already exists in your folder. ";
            }
        }
    }
    ?>
    If you wish to rename the files, be certain that the 'form action=' int he HTML page is set to the correct file name for the page holding the PHP coding.

    *Note: this will not upload a file larger than 100MB in most cases. Many web hosts limit the file size you can upload in the php.ini file and many times you do not have access to this file. Sometimes the file size will be even smaller that 100MB, check with your host before trying to upload and large files so you don't waste your time trying to upload a 100MB file when your hosts only allows 50MB sized files to be uploaded.
     
  3. Unread #2 - Nov 23, 2010 at 11:53 AM
  4. iLike
    Joined:
    Nov 23, 2010
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0

    iLike Newcomer

    [PHP] Very simple file uploader

    Can't say this is the best way of uploading files, this will allow anyone to upload any PHP script. PHP is a server-sided language, so people can actually gain full control (after some exploitation & fiddling around) over your server.
    I have to go now but I'll post an example which filters out PHP files tomorrow or something.
     
  5. Unread #3 - Nov 23, 2010 at 1:11 PM
  6. World Domination
    Joined:
    Apr 9, 2007
    Posts:
    1,563
    Referrals:
    3
    Sythe Gold:
    5

    World Domination Guru
    Banned

    [PHP] Very simple file uploader

    Yes, I do know this, that's why I said it was a "very basic and bare bones" and don't actually recommend it to use to upload your files unless it's private, password protected, etc. I thought I put something like that in my initial post, but I guess I forgot to.
    This script is pretty much for learning purposes only.

    You could also limit the file types you can upload with a simple IF Statement, after calling an $allowed_types variable; something like:

    PHP:
    $allowed_types = array('image/png');

    if (
    in_array($_FILES['file_up']['type'], $allowed_types)) {
    That would limit the uploader to only upload .png files.
     
  7. Unread #4 - Nov 29, 2010 at 6:57 AM
  8. amazing horse
    Joined:
    Oct 12, 2010
    Posts:
    71
    Referrals:
    0
    Sythe Gold:
    0

    amazing horse Member
    Banned

    [PHP] Very simple file uploader

    thanks man
     
  9. Unread #5 - Dec 18, 2010 at 12:12 PM
  10. patdaman45
    Joined:
    Dec 18, 2010
    Posts:
    4
    Referrals:
    0
    Sythe Gold:
    0

    patdaman45 Newcomer

    [PHP] Very simple file uploader

    Thanks, this should come be very useful.
     
  11. Unread #6 - May 24, 2011 at 1:58 PM
  12. Massive MK
    Joined:
    May 23, 2011
    Posts:
    130
    Referrals:
    0
    Sythe Gold:
    0

    Massive MK Active Member

    [PHP] Very simple file uploader

    it's a good example if you ask me but only for protected directory's :p
     
< Require PHP Coding. | Premium Online Store Cscart Templates | Cscart Themes | Cscart Skins >

Users viewing this thread
1 guest


 
 
Adblock breaks this site