5

我正在尝试使用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";
}
}

?>

4

2 回答 2

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";
}
}
于 2014-09-25T04:41:06.423 回答
0

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

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

于 2013-01-03T17:02:19.567 回答