0
我不想在文件上传时关闭上传表单。我创建了一个jQuery脚本来触发PHP文件。它将链接插入数据库中,但不会上传文件。没有jQuery,上传效果很好。使用php和jquery上传
上传表单
<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脚本
$('#updatedevice').submit(function(){
return false;
});
$('#insert1').click(function(){
$.post(
$('#updatedevice').attr('action'),
$('#updatedevice :input').serializeArray(),
);
});
新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);
?>
可能重复[如何异步上传文件?](http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously) –
可能重复[如何创建一个文件上传使用jQuery和PHP?](http://stackoverflow.com/questions/6779177/how-can-i-create-a-file-upload-using-jquery-and-php) –