将文件名插入数据库

时间:2016-11-09 11:48:29

标签: javascript php jquery html

我正在使用照片上传脚本,可在https://github.com/CreativeDream/php-uploader

找到

这是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>

这是将数据发送到post-status.php的Jquery:

//Get Image Value and Assign it to class 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+";";
    }
    var media = $(".mediafile").val(output);
});

// STATUS UPDATE
$(function() {
    $("#submit").click(function() {
        var textcontent = $(".newstatuscontent").val();
        if(media == ''){
            if(textcontent == ''){
                $('.cap_status').html("Status cannot be empty. Please write something.").addClass('cap_status_error').fadeIn(500).delay(3000).fadeOut(500);
            }
        }else{
            $.ajax({
                type: "POST",
                url: "post-status.php",
                data: {content:textcontent},
                cache: true,
                success: function(html){
                    $("#shownewstatus").after(html);
                    $(".newstatuscontent").val('');
                }  
            });
        }
        return false;
    });
});

这是重命名文件(图像)并将其上传到uploads文件夹的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;
?>

现在出现问题:

您可以看到表单中有textarea和文件输入。我想插入textarea数据和上传到数据库的图像的名称。但是使用上面给出的PHP脚本重命名和上传图像。状态由我在post-status.php编写的自定义php脚本发布。我想要的是我希望将重命名的文件名发送到post-status.php页面,以便我可以将其插入数据库。但由于这是我用于上传照片的外部脚本,我无法将其与我的脚本结合使用。请帮帮我们。您可以从上面给出的链接下载脚本。

2 个答案:

答案 0 :(得分:7)

首先,HTML代码中的表单存在一些问题,正如某些人已经提到的那样。其中一个问题是您需要使用runat="server" enctype才能上传文件。如果没有,那么$_SERVER['PHP_SELF']数组将为空并且没有上传任何内容 另一件事是表单标记中的onComplete属性。这完全没必要,只在ASP.net中使用 第三个也是最具社交性的是,$metasvulnerable for XSS attacks

然后,对你的问题。如果您查看了您正在使用的课程的源代码,您会看到在成功上传文件后调用name函数。使用onSuccess数组作为其第二个参数。此数组包含索引{{1}}下的新文件名 或者,您可以使用{{1}}方法在文件上传时插入文件名。

答案 1 :(得分:4)

也许您应该在class UrgentController < ApplicationController def index @materials = Material.where((Time.zone.now.to_date - arrive_date).to_i <= 5) end end 代码中使用“enctype = multipart / form-data”属性

<form>