2016-11-09 103 views
10

我使用的是照片上传脚本,它可以在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> 

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

//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; 
    }); 
}); 

这是重命名文件(图像)并将其上传到上传文件夹的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页面,以便我可以将其插入到数据库中。但是因为这是一个外部脚本,我用它来渲染照片,所以我无法将它与脚本结合起来。请帮助我们。你可以从上面给出的链接下载脚本。

+0

我认为你的代码没有完成,在修复真正的问题之前,你可能会考虑几个问题来解决,首先你需要添加multipart/form-data,如下面的回答,也是这一行“if(media =='') “这不是jQuery ...在你的代码上工作并使其成功上传文件,然后考虑解决名称问题。 –

+0

到目前为止你输出了什么?在知道发布的内容后,使用file和textarea的问题就来了。你尝试调试它们吗? – Rahi

+0

你所做的一切都是错误的。您应该通过输入上传和发送数据来完成1个请求。使用“jquery form”插件,它将您的表单与Ajax包装进行包装,无需额外编码,只需重新开发serverside部分即可。 – num8er

回答

7

首先,使用“ENCTYPE =的multipart/form-data的”属性也有一些问题,在您的HTML代码的形式,正如一些人已经提到。其中一个问题是您需要使用multipart-form-data enctype才能上传文件。如果没有,那么$_FILES阵列将是空的,没有上传。
另一件事runat="server"属性在窗体标签。这完全没有必要,只能在ASP.net中使用。
第三个,也是最具政治性的一个,就是$_SERVER['PHP_SELF']vulnerable for XSS attacks

然后,对你的问题。如果您检出了所用课程的源代码,则会看到在成功上载文件后调用onComplete函数。以$metas数组作为其第二个参数。该数组包含索引name下的新文件名。
或者,您可以使用onSuccess方法在文件上传时插入文件名。

4

也许你应该在<form>标签

<form method="post" enctype= multipart/form-data action="<?php echo $_SERVER['PHP_SELF']; ?>" id="newstatus" runat="server"> 
+0

正确地阅读问题... –