jQuery uploadify将图像存储在mysql中

时间:2012-11-17 01:20:41

标签: php jquery mysql uploadify

我正在尝试使用uploadify将多个图像插入到mysql数据库中。 我的数据库有一个类型为longblob的列。 每次我选择多个文件时,只有我的第一个图像被成功插入。我设置了multi = true。我无法插入多个图像,每当我点击上传链接时,onComplete事件都不会被执行。没有显示错误。 由于某些原因,我的脚本无法插入多个图像,因此我的上传完成事件未被调用。

我遇到的大多数示例都没有插入实际图像,只是将文件名的路径插入到数据库中。任何人都可以帮助我让我的脚本将多个图像上传到数据库中吗?

这是我的index.php脚本:

<script type="text/javascript">

 $(document).ready(function() {

//alert('I am ready to use uploadify!');
$("#picture").uploadify({
    'uploader': 'uploadifyit/uploadify.swf',
    'script': 'sqlupload.php',
    'cancelImg': 'uploadifyit/cancel.png',
    'folder': 'uploads',
    'auto': false, // use for auto upload
    'multi': true,
    'queueSizeLimit': 4,
    'sizeLimit': 10000000,
    'onQueueFull': function(event, queueSizeLimit) {
        alert("Please don't put anymore files in me! You can upload " + queueSizeLimit + " files at once");
        return false;
    },
    'onAllComplete'  : function(){ 
    alert("Thank you. All files have been uploaded successfully."); 
    },
    'onComplete': function(event, ID, fileObj, response, data) {
        console.log(response); // If you're using firebug
        alert(response); // Will alert the php error
    },
    'onUploadError' : function(file, errorCode, errorMsg, errorString) {
        alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
    }
});
});
</script>

sqlupload.php

        <?php
         mysql_connect("localhost", "root", "")or die(mysql_error());
          mysql_select_db("project") or die(mysql_error()); 

         if (!empty($_FILES)) {

         $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
         $fileParts = pathinfo($_FILES['Filedata']['name']);

     if (in_array(strtolower($fileParts['extension']),$fileTypes)) {

    $pic_name = $_FILES['Filedata']['name']; 
        $pic_size = $_FILES['Filedata']['size']; 
        $pic_type = $_FILES['Filedata']['type'];
    $imgData =addslashes(file_get_contents($_FILES['Filedata']['tmp_name']));    

// our sql query
      $sql = "INSERT INTO photos (name,type,size,image)   VALUES('$pic_name','$pic_type','$pic_size','$imgData')";
 // insert the image
mysql_query($sql) or die("Error in Query: " . mysql_error());
}
else {
echo "Invalid file type";
}
}

&GT;

2 个答案:

答案 0 :(得分:0)

也许这个插件可以帮到你:

http://des.delestesoft.com:8080/?go=2

答案 1 :(得分:0)

尝试下面的代码

  <?php
         mysql_connect("localhost", "root", "")or die(mysql_error());
          mysql_select_db("project") or die(mysql_error()); 

         if (!empty($_FILES)) {

         $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
         $fileParts = pathinfo($_FILES['Filedata']['name']);
         //Upload file
         $tempFile = $_FILES['Filedata']['tmp_name'];
         $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_GET['folder'] . '/';
         $targetFile =  str_replace('//','/',$targetPath) . $_FILES['Filedata']['name'];
         move_uploaded_file($tempFile,$targetFile);

     if (in_array(strtolower($fileParts['extension']),$fileTypes)) {

    $pic_name = $_FILES['Filedata']['name']; 
        $pic_size = $_FILES['Filedata']['size']; 
        $pic_type = $_FILES['Filedata']['type'];
    $imgData =addslashes(file_get_contents($_FILES['Filedata']['tmp_name']));    

// our sql query
      $sql = "INSERT INTO photos (name)   VALUES('$pic_name')";
 // insert the image
mysql_query($sql) or die("Error in Query: " . mysql_error());
}
else {
echo "Invalid file type";
}
}