javascript – 将文件名插入数据库

前端之家收集整理的这篇文章主要介绍了javascript – 将文件名插入数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用照片上传脚本,可以在 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和文件输入的形式.我想插入文本区域数据和上传数据库的图像的名称.但是,使用上面给出的PHP脚本重命名上传映像.并且状态由post-status.PHP中由我写的自定义PHP脚本发布.我想要的是,我想将重命名文件名发送到post-status.PHP页面,以便我可以将其插入数据库.但是,由于这是一个外部脚本,我正在使用升级照片,我无法将其与我的脚本相结合.请帮我们您可以从上面给出的链接下载脚本.

解决方法

首先,您的HTML代码中的表单有一些问题,正如有些人已经提到的.其中一个问题是您需要使用multipart-form-data enctype才能上传文件.如果没有,那么$_FILES数组将为空,没有上传.
另一件事在表单标签中的runat =“server”属性.这是完全不必要的,只用于ASP.net.
第三个也是最关键的一个是$_SERVER [‘PHP_SELF’]是 vulnerable for XSS attacks

然后,你的问题.如果您已经签出了您正在使用的类的源代码,则会看到在成功上传文件调用了onComplete函数.以$Metas数组为第二个参数.该数组包含索引名下的新文件名.或者,您可以使用onSuccess方法,在文件上传时插入文件名.

猜你在找的JavaScript相关文章