javascript – 将文件名插入数据库

前端之家收集整理的这篇文章主要介绍了javascript – 将文件名插入数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用照片上传脚本,可以在 https://github.com/CreativeDream/php-uploader找到

这是HTML:

  1. <form method="post" action="<?PHP echo $_SERVER['PHP_SELF']; ?>" id="newstatus" runat="server">
  2. <textarea name="status" class="textarea newstatuscontent" placeholder="What are you thinking?"></textarea>
  3. <div class="media"><input type="file" name="files[]" id="filer_input2" multiple="multiple"></div>
  4. <input type="submit" name="post" value="Post" class="post-btn" id="submit" />
  5. </form>

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

  1. //Get Image Value and Assign it to class mediafile
  2. $('#filer_input2').change(function(){
  3. var files = $(this)[0].files;
  4. var output = "";
  5. for(var i = 0; i < files.length; i++){
  6. console.log(files[i].name);
  7. output += files[i].name+";";
  8. }
  9. var media = $(".mediafile").val(output);
  10. });
  11.  
  12. // STATUS UPDATE
  13. $(function() {
  14. $("#submit").click(function() {
  15. var textcontent = $(".newstatuscontent").val();
  16. if(media == ''){
  17. if(textcontent == ''){
  18. $('.cap_status').html("Status cannot be empty. Please write something.").addClass('cap_status_error').fadeIn(500).delay(3000).fadeOut(500);
  19. }
  20. }else{
  21. $.ajax({
  22. type: "POST",url: "post-status.PHP",data: {content:textcontent},cache: true,success: function(html){
  23. $("#shownewstatus").after(html);
  24. $(".newstatuscontent").val('');
  25. }
  26. });
  27. }
  28. return false;
  29. });
  30. });

这是重新命名文件(图像)并将其上传到uploads文件夹的PHP文件.

  1. <?PHP
  2. include('class.uploader.PHP');
  3.  
  4. $uploader = new Uploader();
  5. $data = $uploader->upload($_FILES['files'],array(
  6. 'limit' => 10,//Maximum Limit of files. {null,Number}
  7. 'maxSize' => 10,//Maximum Size of files {null,Number(in MB's)}
  8. 'extensions' => null,//Whitelist for file extension. {null,Array(ex: array('jpg','png'))}
  9. 'required' => false,//Minimum one file is required for upload {Boolean}
  10. 'uploadDir' => '../uploads/',//Upload directory {String}
  11. 'title' => array('{{random}}{{.extension}}',32),//New file name {null,String,Array} *please read documentation in README.md
  12. 'removeFiles' => true,//Enable file exclusion {Boolean(extra for jQuery.filer),String($_POST field name containing json data with file names)}
  13. 'replace' => false,//Replace the file if it already exists {Boolean}
  14. 'perms' => null,//Uploaded file permisions {null,Number}
  15. 'onCheck' => null,//A callback function name to be called by checking a file for errors (must return an array) | ($file) | Callback
  16. 'onError' => null,//A callback function name to be called if an error occured (must return an array) | ($errors,$file) | Callback
  17. 'onSuccess' => null,//A callback function name to be called if all files were successfully uploaded | ($files,$Metas) | Callback
  18. 'onUpload' => null,//A callback function name to be called if all files were successfully uploaded (must return an array) | ($file) | Callback
  19. 'onComplete' => null,//A callback function name to be called when upload is complete | ($file) | Callback
  20. 'onRemove' => null //A callback function name to be called by removing files (must return an array) | ($removed_files) | Callback
  21. ));
  22.  
  23. if($data['isComplete']){
  24. $files = $data['data'];
  25.  
  26. echo json_encode($files['Metas'][0]['name']);
  27. }
  28.  
  29. if($data['hasErrors']){
  30. $errors = $data['errors'];
  31. echo json_encode($errors);
  32. }
  33.  
  34. exit;
  35. ?>

现在的问题是:

您可以看到有一个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相关文章