0

I don't want an upload form to close while the file is being uploaded. I created a jQuery script to trigger the PHP file. It inserts the link in the database but doesn't upload the file. The upload works well without jQuery.

Upload Form

<form action="../action/subs/new-device.php/" method="post" id="updatedevice" enctype="multipart/form-data" >

    Upload File(s): <input id="content" class="input_short" type="file" name="content[]" multiple="multiple" />

    <button class="btn btn-default backprod" id='insert1'>Save</button>
</form>

jquery script

$('#updatedevice').submit(function(){
 return false;
});

$('#insert1').click(function(){
 $.post( 
  $('#updatedevice').attr('action'),
  $('#updatedevice :input').serializeArray(),
 );
});

new-device.php

<?php
include '../db/connect.php';

$valid_formats = array("jpg", "png", "gif", "zip", "docx","zip","pdf",   "txt", "xlsx");
$max_file_size = 3040*500; 
$path = "/home5/rep1/public_html/tmp/upload/$filestf/"; // Upload  directory
if (!is_dir($path)) {
    mkdir($path);
}

$count = 0;

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){

  foreach ($_FILES['content']['name'] as $f => $name) {     
    if ($_FILES['content']['error'][$f] == 4) {
        continue; 
    }          
    if ($_FILES['content']['error'][$f] == 0) {               
      if ($_FILES['content']['size'][$f] > $max_file_size) {
        $message[] = "$name is too large!.";
        continue; 
      }
      elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION),    $valid_formats) ){
        $message[] = "$name is not a valid format";
        continue;
      }
      else{ 
        if(move_uploaded_file($_FILES["content"]["tmp_name"][$f],     $path.$name))
        $count++; 
      }
    }

    $_SESSION['u_files'] = $count;
    $_SESSION['tmp_dir'] = $path;
  }
}

$deviceid=mysqli_insert_id($con);
$newpath = "/home5/rep1/public_html/uploads/devices/$orderid/";
rename($tmppath,$newpath);
$sql="INSERT INTO oz2ts_devices_files (device_id, file_path)
    VALUES
    ('$deviceid', '$newpath')";
$result=(!mysqli_query($con,$sql));
mysqli_close($con);
?>
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
July James
  • 29
  • 3
  • 3
    Possible duplicate of [How can I upload files asynchronously?](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously) – Jay Blanchard Nov 20 '15 at 18:10
  • 1
    Possible duplicate of [How can I create a file upload using jQuery and php?](http://stackoverflow.com/questions/6779177/how-can-i-create-a-file-upload-using-jquery-and-php) – Reza-S4 Nov 20 '15 at 18:21

1 Answers1

0

You can use some jquery file upload plugins like blueimp or valums file uploader

and use the same upload functionality on PHP.

Hope this help you.

Community
  • 1
  • 1
keviveks
  • 320
  • 1
  • 3
  • 17