plupload filename到数据库问题

时间:2011-09-17 19:08:19

标签: plupload sql-insert

我在尝试使用Plupload保存上传文件的文件名时遇到了麻烦。这是Plupload部分:

echo<<<_END
  <div id="container">
    <div id="filelist">No runtime found.</div>
    <br />
    <a id="pickfiles" href="javascript:;">[Select file]</a> 
    <a id="uploadfiles" href="javascript:;">[Upload it]</a>
  </div>

  <script type="text/javascript"> 
  // Custom example logic
    function $(id) {
      return document.getElementById(id);
    }

  var uploader = new plupload.Uploader({
    runtimes : 'gears,html5,flash,silverlight,browserplus',browse_button : 'pickfiles',container: 'container',max_file_size : '1mb',
    url : 'upload.php',
    resize : {width : 320, height : 240, quality : 90},
    flash_swf_url : 'plupload/js/plupload.flash.swf',
    silverlight_xap_url : 'plupload/js/plupload.silverlight.xap',
    filters : [{title : "Image files", extensions : "jpg,jpeg,gif,png"}]
  });

  uploader.bind('Init', function(up, params) {
    $('filelist').innerHTML = "<div>Current runtime: " + params.runtime + "</div>"; 
  });

  uploader.bind('FilesAdded', function(up, files) {
    for (var i in files) {
      $('filelist').innerHTML += '<div id="' + files[i].id + '">' + files[i].name     + ' (' + plupload.formatSize(files[i].size) + ') <b></b></div>';
    }});

  uploader.bind('UploadProgress', function(up, file) {
    $(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%    </span>";
  });

  $('uploadfiles').onclick = function() {
    uploader.start();
    return false;
  };

  uploader.init();
</script>
_END;

但我不知道如何将文件名插入我的数据库:

$post_img = file??

$topicid = mysql_insert_id();
$sql = "INSERT INTO
    posts(post_content,
    post_date,
    post_topic,
    post_by,
    post_img
  )
  VALUES
  (
    '" . mysql_real_escape_string($_POST['post_content']) . "',
    NOW(),
    " . $topicid . ",
    " . $_SESSION['userid'] . ",
    " . $post_img . " )";
  $result = mysql_query($sql);

  if(!$result) {
    //something went wrong, display the error
    echo 'An error occured while inserting your post. Please try again later.<br /><br />' . mysql_error();
    $sql = "ROLLBACK;";
    $result = mysql_query($sql);
  }

我收到错误'插入帖子时出错。请稍后再试一次'

2 个答案:

答案 0 :(得分:2)

我将mysql查询放入upload.php文件中。 我不确定该文件中的哪个位置,但我想它在“检查文件是否已上传”部分,位于upload.php文件的末尾。

if (!$chunks || $chunk == $chunks - 1) {

// Strip the temp .part suffix off 
rename("{$filePath}.part", $filePath);

    //your mysql query here**************************
}

我认为它可能在这里的原因是因为它检查上传文件中是否还有任何块。如果它不是任何左边,你就在if声明中。 upload.php在每个上传的文件上运行foreach,因此您只需将$fileName放入数据库即可。

它对我有用。希望它有所帮助

答案 1 :(得分:0)

很多格拉西亚斯迈克。它帮助了我

  

if(!$ chunks || $ chunk == $ chunks - 1){

效果更好
  

如果($块== 1){