Created
June 23, 2018 23:31
-
-
Save jerry-maheswara-github/8e4c6b3f19c5dc7b5bf268a7f18ec1ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* ------------------------------------------------------- | |
* Author: Jerry Maheswara | |
* Description: Upload Multiple File with Database | |
* Silahkan dikembangkan lebih lanjut....!! | |
* Feel free to improve your code.... | |
* jangan sungkan-sungakan.... ini hanya sebagai dasar saja... | |
*/ | |
// KONEKSI KE DATABASE | |
/* | |
-- buat database: `upload_multi_file` | |
CREATE TABLE IF NOT EXISTS `upload_data` ( | |
`file_size` varchar(255) NOT NULL, | |
`file_name` varchar(255) NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
*/ | |
$con = mysql_connect("localhost","root",""); | |
mysql_select_db("upload_multi_file", $con); | |
error_reporting(E_ALL); | |
$upload_dir= 'uploads'; // ini folder tempat file yg diupload | |
$num_uploads = 4; | |
$max_file_size = 5120000; | |
$upload_max = $max_file_size * 1024; | |
$msg = 'Pilih File untuk di upload'; | |
$messages = array(); | |
if(isset($_FILES['userfile']['tmp_name'])) | |
{ | |
for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++) | |
{ | |
if(!is_uploaded_file($_FILES['userfile']['tmp_name'][$i])) | |
{ | |
$messages[] = 'Upload Gagal'; | |
} | |
elseif($_FILES['userfile']['size'][$i] > $upload_max) | |
{ | |
$messages[] = "Ukuran file melebihi batas"; | |
} | |
elseif($_FILES['userfile']['size'][$i] > $max_file_size) | |
{ | |
$messages[] = "Ukuran file melebihi batas"; | |
} | |
else | |
{ | |
if(@copy($_FILES['userfile']['tmp_name'][$i],$upload_dir.'/'.$_FILES['userfile']['name'][$i])) | |
{ | |
$messages[] = $_FILES['userfile']['name'][$i].' uploaded'; | |
$f_size = $_FILES['userfile']['size'][$i]; | |
$f_name = $_FILES['userfile']['name'][$i]; | |
$query = "INSERT INTO `upload_multi_file`.`upload_data` (`file_size`, `file_name`) VALUES ('".$f_size."','".$f_name."')"; | |
mysql_query($query); | |
} | |
else | |
{ | |
$messages[] = 'Upload '.$_FILES['userfile']['name'][$i].' Gagal'; | |
} | |
} | |
} | |
} | |
?> | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
<head> | |
<title>Multiple File Upload</title> | |
</head> | |
<body> | |
<h3><?php echo $msg; ?></h3> | |
<p> | |
<?php | |
if(sizeof($messages) != 0) | |
{ | |
foreach($messages as $err) | |
{ | |
echo $err.'<br />'; | |
} | |
} | |
?> | |
</p> | |
<form enctype="multipart/form-data" method="post"> | |
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" /> | |
<?php | |
$num = 0; | |
while($num < $num_uploads) | |
{ | |
echo '<div><input name="userfile[]" type="file" /></div>'; | |
$num++; | |
} | |
?> | |
<input type="submit" value="Upload" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment