Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用AJAX将文件名插入数据库_Javascript_Php_Jquery_Html_Ajax - Fatal编程技术网

Javascript 使用AJAX将文件名插入数据库

Javascript 使用AJAX将文件名插入数据库,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我正在使用照片上传脚本,可以在 我想将文件名作为逗号分隔的值插入数据库。但由于此上载程序使用拖放div上载文件,因此它不保存中的值 HTML: <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="newstatus" runat="server"> <textarea name="status" class="textarea newstatuscontent" pla

我正在使用照片上传脚本,可以在

我想将文件名作为逗号分隔的值插入数据库。但由于此上载程序使用拖放div上载文件,因此它不保存
中的值

HTML:

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" id="newstatus" runat="server">
      <textarea name="status" class="textarea newstatuscontent" placeholder="What are you thinking?"></textarea>
      <div class="media"><input type="file" name="files[]" id="filer_input2" multiple="multiple"></div>
      <input type="submit" name="post" value="Post" class="post-btn" id="submit" />
</form>
PHP图像上载文件:

<?php
    include('class.uploader.php');

    $uploader = new Uploader();
    $data = $uploader->upload($_FILES['files'], array(
        'limit' => 10, //Maximum Limit of files. {null, Number}
        'maxSize' => 10, //Maximum Size of files {null, Number(in MB's)}
        'extensions' => null, //Whitelist for file extension. {null, Array(ex: array('jpg', 'png'))}
        'required' => false, //Minimum one file is required for upload {Boolean}
        'uploadDir' => '../uploads/', //Upload directory {String}
        'title' =>  array('{{random}}{{.extension}}', 32), //New file name {null, String, Array} *please read documentation in README.md
        'removeFiles' => true, //Enable file exclusion {Boolean(extra for jQuery.filer), String($_POST field name containing json data with file names)}
        'replace' => false, //Replace the file if it already exists  {Boolean}
        'perms' => null, //Uploaded file permisions {null, Number}
        'onCheck' => null, //A callback function name to be called by checking a file for errors (must return an array) | ($file) | Callback
        'onError' => null, //A callback function name to be called if an error occured (must return an array) | ($errors, $file) | Callback
        'onSuccess' => null, //A callback function name to be called if all files were successfully uploaded | ($files, $metas) | Callback
        'onUpload' => null, //A callback function name to be called if all files were successfully uploaded (must return an array) | ($file) | Callback
        'onComplete' => null, //A callback function name to be called when upload is complete | ($file) | Callback
        'onRemove' => null //A callback function name to be called by removing files (must return an array) | ($removed_files) | Callback
    ));

    if($data['isComplete']){
        $files = $data['data'];

        echo json_encode($files['metas'][0]['name']);
    }

    if($data['hasErrors']){
        $errors = $data['errors'];
        echo json_encode($errors);
    }

    exit;
?>
如何获取和解析文件名
下面是一个工作示例,我向您展示了如何获取文件名

我做的第一件事是听文件输入是否改变。当它这样做时,它将抓取与输入相关联的文件并循环遍历所有文件。我将名称连接到一个由分号分隔的字符串中,并将
.mediafile
的值设置为连接的文件名

$('#filer_input2').change(function(){
    var files = $(this)[0].files;
var output = "";
for (var i = 0; i < files.length; i++) {
    console.log(files[i].name);
    output += files[i].name+";";
}

$(".mediafile").val(output);
$('#文件管理器输入2')。更改(函数(){
var files=$(this)[0]。文件;
var输出=”;
对于(var i=0;i
}))


Fiddle:作为逗号分隔的值…gooosh,为什么?像这样的abc.jpg,bcd.jpg,123.jpg。。。所以我以后可以提取出来使用..这些应该存储在另一个表中..文件id,文件名,mime类型,存储路径,创建日期,等等,非常正确。允许我快速更新我的代码。@ShubhamJha答案和jsfiddle已经更新,可以处理多个文件。是的。。我在一个PHP文件中重命名该文件,并将其上载到uploads文件夹。。等一下。。我正在用那个脚本更新我的答案。。。因此,你能使文件值重命名为与它被上传的内容,否则它将不匹配的数据库值…更新。。。正在使用
标题重命名该文件。。请看一看。。我需要插入重命名文件的名称,以便与上载值和数据库插入值相匹配。。
$('#filer_input2').change(function(){
    var files = $(this)[0].files;
var output = "";
for (var i = 0; i < files.length; i++) {
    console.log(files[i].name);
    output += files[i].name+";";
}

$(".mediafile").val(output);