Multiple File Upload in php

<?php
$db=mysql_connect(‘localhost’,’root’,”);
mysql_select_db(‘examples’,$db);

if(isset($_FILES[‘image’])){
$errors= array();
foreach($_FILES[‘image’][‘tmp_name’] as $key => $tmp_name ){
$file_name = $key.$_FILES[‘image’][‘name’][$key];
$file_size =$_FILES[‘image’][‘size’][$key];
$file_tmp =$_FILES[‘image’][‘tmp_name’][$key];
$file_type=$_FILES[‘image’][‘type’][$key];
if($file_size > 2097152){
$errors[]=’File size must be less than 2 MB’;
}
$query=”INSERT into upload_data (`FILE_NAME`,`FILE_SIZE`,`FILE_TYPE`) VALUES(‘$file_name’,’$file_size’,’$file_type’); “;
$desired_dir=”user_data”;
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir(“$desired_dir”, 0700);        // Create directory if it does not exist
}
if(is_dir(“$desired_dir/”.$file_name)==false){
move_uploaded_file($file_tmp,”$desired_dir/”.$file_name);
}else{                                    // rename the file if another one exist
$new_dir=”$desired_dir/”.$file_name.time();
rename($file_tmp,$new_dir) ;
}
mysql_query($query);
}else{
print_r($errors);
}
}
if(empty($error)){
echo “Success”;
}
}
?>

<form action=”” method=”POST” enctype=”multipart/form-data”>
<input type=”file” name=”image[]” multiple/>
<input type=”submit”/>
</form>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s