简体   繁体   English

使用php和jquery上传

[英]Upload using php and jquery

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. 我创建了一个jQuery脚本来触发PHP文件。 It inserts the link in the database but doesn't upload the file. 它将链接插入数据库中,但不上传文件。 The upload works well without jQuery. 没有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 jQuery脚本

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

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

new-device.php 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);
?>

You can use some jquery file upload plugins like blueimp or valums file uploader 您可以使用一些jquery文件上传插件,例如blueimpvalums文件上传器

and use the same upload functionality on PHP. 并在PHP上使用相同的上传功能。

Hope this help you. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM