0

I am new for developing.I have opted for php to learn coding.So I might make mistake as I learn by myself, kindly clarify my doubts. I have problem in uploading files using php to a folder.What I really do is, I upload a file and the file is saved in a folder and the name of the file alone inserted in the database. While uploading the file I do copy the file to another folder which will be used for the editing purpose so that the original file will not be disturbed.Here the problem I get is, the file is uploaded successfully as well as the name too inserted in database. But it take much time to get upload even the size of the file is small.It works good while I test using my local but when I come in real time this issue(slow uploading) I face. What the person incharge in uploading do is, uploading a file and opening a new browser and upload another file. When the new browser is opened the files get uploaded but in the previous browser it is still in process. The code I have written to copy the file to another folder is not executed as the new browser is opened to upload another set of files. I am using xamp cp v3.2.1.To minimize the execution time I have set the default Maximum execution time to 30. But unable to upload file fastly.

Below is my php coding:

 <?php 





 // connect to the database
 include('connect-db.php');

 if (isset($_POST['submit']))
 { 
 // get form data, making sure it is valid
 $udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
 $file_array=($_FILES['file_array']['name']);



 // check to make sure both fields are entered
 if ($udate == '' || $file_array=='')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
 renderForm($udate, $file_array, $error);
 }
 else
 {
     $udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
if(isset($_FILES['file_array']))
{
    $name_arrray=$_FILES['file_array']['name'];
    $tmp_name_arrray=$_FILES['file_array']['tmp_name'];
    for($i=0;$i <count($tmp_name_arrray); $i++)
    {
        if(move_uploaded_file($tmp_name_arrray[$i],"test_uploads/".str_replace(' ','',$name_arrray[$i])))

        {

                       // save the data to the database
$j=str_replace(' ','',$name_arrray[$i]);
echo $j;
 $udate = mysql_real_escape_string(htmlspecialchars($_POST['udate']));
  $provider = mysql_real_escape_string(htmlspecialchars($_POST['provider']));
  $existfile=mysql_query("select ubatch_file from batches");
  while($existing = mysql_fetch_array( $existfile)) {
      if($j==$existing['ubatch_file'])
    echo'  <script>
function myFunction() {
    alert("file already exists");
}
</script>';

      }

 mysql_query("INSERT IGNORE batches SET udate='$udate', ubatch_file='$j',provider='$provider',privilege='$_SESSION[PRIVILEGE]'")
 or die(mysql_error()); 
        echo $name_arrray[$i]."uploaded completed"."<br>";
        $src = 'test_uploads';
$dst = 'copy_test_uploads';
$files = glob("test_uploads/*.*");
      foreach($files as $file){
      $file_to_go = str_replace($src,$dst,$file);
      copy($file, $file_to_go);

       /* echo "<script type=\"text/javascript\">
                        alert(\"CSV File has been successfully Uploaded.\");
                        window.location = \"uploadbatches1.php\"
                    </script>";*/
      }
        } else
        {
            echo "move_uploaded_file function failed for".$name_array[$i]."<br>";
        }

    }
}

 // once saved, redirect back to the view page
 header("Location:uploadbatches1.php"); 
 }
 }



?>
Lina Gom
  • 97
  • 1
  • 7
  • 1
    How big is the uploaded file ? – Abhik Chakraborty Apr 29 '15 at 06:19
  • Why does you php script begin with a `}`? – Cristik Apr 29 '15 at 06:23
  • Sorry its an end of a php code. – Lina Gom Apr 29 '15 at 06:24
  • max_execution_time=30 max_input_time=60 post_max_size=128M upload_max_filesize=32M max_file_uploads=20 – Lina Gom Apr 29 '15 at 06:27
  • and you forget to start session at the top of your page!! – Saty Apr 29 '15 at 06:27
  • @saty: what does `session_start()` has to do with the upload? – Cristik Apr 29 '15 at 06:30
  • @AbhikChakraborty Each file will be below 32MB – Lina Gom Apr 29 '15 at 06:33
  • my problem is too slow uploading.Do I have to make any changes in XAMPP to make it faster? – Lina Gom Apr 29 '15 at 06:39
  • well thats pretty big and you can't expect that to be faster. You may do chuck upload and there several tutorial on it, here is one http://stackoverflow.com/questions/9011138/handling-pluploads-chunked-uploads-on-the-server-side – Abhik Chakraborty Apr 29 '15 at 06:39
  • But some times even some KB files too take the same time to upload. Until another browser is opened the uploading file is still in progress as I have mentioned above. – Lina Gom Apr 29 '15 at 06:42
  • @Cristik i know session_start() have nothing to do with upload but OP used session on his page it is just an awareness for further error. – Saty Apr 29 '15 at 06:45
  • I have found reason for this issue.The code for uploading files and copying in another folder works fine.I have deleted all the files in both the folders and tried to upload.It uploads faster.Now I have found the reason but how could I compress the files in the folder using php.As I have no option than to compress.Help me on this issue – Lina Gom Apr 30 '15 at 09:42

1 Answers1

0

It takes much time because, each and everytime all the files are copied to the newfolder. This exceeds the execution time.Only copying the uploaded files makes uploading and copying files fast.

Lina Gom
  • 97
  • 1
  • 7