Wednesday, June 4, 2014

PHP File Upload - Multiple, Thumbnail Resize

The PHP File Upload is the Easiest Way to Upload a File to Webserver.

Following Code Snippet is the Simple PHP File Upload in 3 Line of Code.

Single File Upload
HTML Code
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>File Upload</title>
    </head>
    <body>
    <form enctype="multipart/form-data" name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        Send this file: <input name="userfile" type="file" />
        <input type="submit" value="Send File" />
    </form>
    </body>
</html>

PHP Code

<?php
//Check and Move Uploaded File
if(isset($_FILES['userfile']) && !empty($_FILES['userfile'])) {
    $tmp_name = $_FILES["userfile"]["tmp_name"];
    $destination = $_FILES["userfile"]["name"];
    move_uploaded_file($tmp_name, $destination);
}
?>

That's It.

Copy Both in a Same File. Final File Looks Like index.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>File Upload</title>
    </head>
    <body>
    <form enctype="multipart/form-data" name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        Send this file: <input name="userfile" type="file" />
        <input type="submit" value="Send File" />
    </form>
    </body>
</html>
<?php
//Check and Move Uploaded File
if(isset($_FILES['userfile']) && !empty($_FILES['userfile'])) {
    $tmp_name = $_FILES["userfile"]["tmp_name"];
    $destination = $_FILES["userfile"]["name"];
    move_uploaded_file($tmp_name, $destination);
}
?>


Multiple File Upload using pure PHP Codes
Multiple File upload using other Coding frameworks are really tough. But PHP Lets you do that in a Very Easy way.

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Multiple File Upload</title>
    </head>
    <body>
    <form enctype="multipart/form-data" name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        Send this file1: <input name="userfile[]" type="file" /> <br />
        Send this file2: <input name="userfile[]" type="file" /> <br />
        Send this file3: <input name="userfile[]" type="file" /> <br />
        <input type="submit" value="Send File" />
    </form>
    </body>
</html>
<?php
//Check and Move Uploaded File
if(isset($_FILES['userfile']) && !empty($_FILES['userfile'])) {
    foreach($_FILES['userfile']['error'] as $keyy => $er) {
        
        if($er == UPLOAD_ERR_OK) { //OK TO Upload
            $tmp_name = $_FILES["userfile"]["tmp_name"][$keyy];
            $destination = $_FILES["userfile"]["name"][$keyy];
            move_uploaded_file($tmp_name, $destination);
        }
    }
}
?>


Multiple File Upload - Browse Images / Video / Audio only



<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Multiple File Upload</title>
    </head>
    <body>
    <form enctype="multipart/form-data" name="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        All Images Only: <input name="userfile[]" accept='image/*' type="file" /> <br />
        All Images,Audio and Video: <input name="userfile[]" accept='image/*|audio/*|video/*' type="file" /><br />
        .newext file, any Image: <input name="userfile[]" accept=".newext,image/*" type="file" /><br />
        only jpeg: <input name="userfile[]" accept="image/jpeg" type="file" /><br />
        <input type="submit" value="Send File" />
    </form>
    </body>
</html>
<?php
//Check and Move Uploaded File
if(isset($_FILES['userfile']) && !empty($_FILES['userfile'])) {
    foreach($_FILES['userfile']['error'] as $keyy => $er) {
        
        if($er == UPLOAD_ERR_OK) { //OK TO Upload
            $tmp_name = $_FILES["userfile"]["tmp_name"][$keyy];
            $destination = $_FILES["userfile"]["name"][$keyy];
            move_uploaded_file($tmp_name, $destination);
        }
    }
}
?>


Accepted File Formats
image/*
video/*
audio/*
.anycustomextension



5 comments:

  1. Thank You .

    Its Useful

    ReplyDelete
  2. That is simple and Cool. thanks boss

    ReplyDelete
  3. Usually I do not read post on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Great work admin.Keep update more blog.
    PHP Training in Chennai

    ReplyDelete
  4. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete
  5. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    Android Training in Chennai
    Ios Training in Chennai

    ReplyDelete