0

I have troubles trying to save the file name of an uploaded file with Plupload. Here is the Plupload section:

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;

But I have no idea how to insert the file name into my database:

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

I am getting the error 'An error occured while inserting your post. Please try again later'

4

1 回答 1

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其放入数据库中。

这个对我有用。希望能帮助到你

于 2013-02-23T23:46:29.807 回答