BoxyGILLETT
BoxyGILLETT

Reputation: 25

Image uploader faster

Here is a multi image selector uploader the problem I am having is once selected images and click submit it takes ages to to the request is there any way of making it process faster??

Do not worry about database table and columns are there for testing reasons not made my full database yet just been testing 1st.

Thanks in advance.

   <?
session_start();
include('connect.php');
?>

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

<?

if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
    $file_name = $key.$_FILES['files']['name'][$key];
    $file_size =$_FILES['files']['size'][$key];
    $file_tmp =$_FILES['files']['tmp_name'][$key];
    $file_type=$_FILES['files']['type'][$key];  
    if($file_size > 2097152){
        $errors[]='File size must be less than 2 MB';
    }
$desired_dir="Uploads/";    
    $query="INSERT into Pictures (`id`,`Image1`,`Image2`,`Image3`) VALUES('','$desired_dir$file_name','$file_size','$file_type'); ";
    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";
}
}
?>

Upvotes: 0

Views: 1588

Answers (2)

mdikici
mdikici

Reputation: 440

As far as I know, there is no multi threading upload of files to server. And the upload speed is limited with server's download speed and user's upload speed (which is much more slower then server's download speed). Unfortunately, it is not possible to make uploads faster by changing the code.

Upvotes: 0

Paul Dessert
Paul Dessert

Reputation: 6389

Based on the comments above, the upload seems normal. You have a slower internet connection and will experience a slowdown when uploading files.

I also strongly recommend you look into MySQLi or PDO. Your current script is wide open to attack

Upvotes: 1

Related Questions