Announcement

Collapse
No announcement yet.

PHP File upload barebone version

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP File upload barebone version

    File upload is regularly asked and people see it as a hard job with allot of code so i put here the minimal working code to upload something to a folder on a unix server.

    This code assumes that in the same folder this script is there is a folder called "files" and that it has correct permissions so you can upload data into it (chmod 777 or similar)

    Keep in mind that you allways need to check are uploading stuff you wanted meaning you need to check the file extension, content type, file size and that file is unique and and and.
    Basically only these mandatory checks (that i don't present here) makes the code long.

    PHP Code:
    <?php
    //If submit has been pressed (if it's set)
    if (isset($_POST['submit'])) {
      
    /* $_FILES is an array that contains information from the uploaded file. Different uploads are separated by the name variable that you set in the form (myfile in this case). In this example we use 
    tmp_name thats the actual uploaded file with random name. Name here is the actual given filename in your own machine. This action just moves the tmp_name file with new name into different location in the same server */
      
    move_uploaded_file($_FILES['myfile']['tmp_name'],"./files/".$_FILES['myfile']['name']);
    }

    /* Normal form. Only thing that might be new is the enctype which without the upload progress will not work. */

    echo "<form action='{$_SERVER['PHP_SELF']}' method='post' enctype='multipart/form-data'>";
    echo 
    "<input type='file' name='myfile' value='' />";
    echo 
    "<input type='submit' name='submit' value='Submit' />";
    ?>

  • #2
    There is additionally a lot of information and barebone/advanced examples on http://php.net/features.file-upload

    Comment

    Working...
    X