Skip to content

Instantly share code, notes, and snippets.

@ronakjain2012
Created July 1, 2016 07:50
Show Gist options
  • Save ronakjain2012/765c0eb71502eb3b0605067e283b64f1 to your computer and use it in GitHub Desktop.
Save ronakjain2012/765c0eb71502eb3b0605067e283b64f1 to your computer and use it in GitHub Desktop.
Upload Multiple files using php
<form method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple />
<input type="submit" value="Upload" class="btn btn-success"/>
</form>
<?php
$valid_formats = array("c", "cpp", "php", "java", "bat", "js" ,"sql" ,"html");
$max_file_size = 1024*5000; //100 kb
$path = "myfiles/"; // Upload directory
$count = 0;
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
// Loop $_FILES to exeicute all files
foreach ($_FILES['files']['name'] as $f => $name) {
if ($_FILES['files']['error'][$f] == 4) {
continue; // Skip file if any error found
}
if ($_FILES['files']['error'][$f] == 0) {
if ($_FILES['files']['size'][$f] > $max_file_size) {
$message[] = "$name is too large!.";
continue; // Skip large files
}
elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats) ){
$message[] = "$name is not a valid format";
continue; // Skip invalid file formats
}
else{ // No error found! Move uploaded files
if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
$fullPath= $path.$name;
$ext = pathinfo($name, PATHINFO_EXTENSION);
$query = "insert into file_management (file_owner,file_upload_time,file_name,file_path,file_type) values('$user',now(),'$name','$fullPath','$ext')";
$conn->query($query);
if($conn->errno)
echo $conn->errno;
if($conn->error)
echo $conn->error;
$count++; // Number of successfully uploaded file
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment