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);
?>