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



7 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
  6. It’s always good to learn tips like you share for blog posting. I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too. Thanks and keep post such a informative blogs.
    AngularJS Training Institute in Chennai
    AngularJS Cetification Training in Chennai
    AWS Training in Chennai
    AWS Devops Training in Chennai
    DevOps Certification in Chennai
    DevOPs Certification Training Institute in Chennai
    Best AngularJS Training in Chennai
    AngularJS Training in Chennai

    ReplyDelete
  7. It’s always good to learn tips like you share for blog posting. I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me.Linking is very useful thing you have really helped lots of people who visit this blog and provided them this useful information on opinionated astrologer. Thanks a lot for this, Well done. I will let you know if its work for me too. Thanks and keep post such a informative blogs.
    AngularJS Training Institute in Chennai
    AngularJS Cetification Training in Chennai
    AWS Training in Chennai
    AWS Devops Training in Chennai
    DevOps Certification in Chennai
    DevOPs Certification Training Institute in Chennai
    Best AngularJS Training in Chennai
    AngularJS Training in Chennai

    ReplyDelete