Friday, January 16, 2015

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);
}
?>

Thats 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



No comments:

Post a Comment

Note: Only a member of this blog may post a comment.