5
votes

I am trying to insert multiple images into mysql database using uploadify. My database is having one of it's column to be type longblob. Every time I select multiple files,only my first image gets inserted successfully.I have set multi = true.I am unable to get more than one image inserted and also whenever I click on the upload link,the onComplete event does not get executed.There is no error displayed. For some reason my script is not able to insert more than one image and as a result my upload complete event is not getting called.

Most of examples I have come across,do not insert the actual image but just the path of file name into database.Can anyone please help me to get my script to upload multiple images into the db?

this is my index.php script:

<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";
}
}

?>

2
use print_r($_FILES) to print your output , design the loop patern - Leon Armstrong
do NOT use addslashes(). it's about as useful for sql injection prevent as a piece of wet toilet paper is for drying out new jersey. - Marc B
did you tried jquery file upload plugin? it uses fileApi and fallback to iframe transport also it have hooks to use canvas enabled browsers to resize images clientside - zb'
Why are you storing images in the database? You are better off storing them on the file system and keeping a reference to the file path in the database. - toneplex

2 Answers

0
votes
0
votes

try with code below

  <?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";
}
}