简体   繁体   中英

can't upload image to mysql database

This is the first time i am trying to upload image to a database.i have the following piece of code to upload images to mysql database.But no images are being uploaded to the database.No error or warning has been given.Can't figure out what might be the problem.Any help will be appreciated.

php:

if(isset($_FILES['files'])){
    $name=$_FILES['files']['name'];
    if(!empty($name)){


        if(!file_exists($name)){
            $temp=$_FILES['files']['tmp_name'];

            //move_uploaded_file($temp,getcwd().'/uploads/'.$name);
            try{
                 $servername='localhost';
                 $username='root';
                 $password='';
                 $dbname='login';
                 $fp = fopen($temp, 'r');
                 $conn=new PDO('mysql:host=localhost;dbname=login',$username,$password);
                 $stmt = $conn->prepare("INSERT INTO images (image_name, image) VALUES (?, ?)");

                 $stmt->bindParam(1, $_FILES['file']['name']);
                 $stmt->bindParam(2, $fp, PDO::PARAM_LOB);
                 if($stmt->execute()){
                     echo 'its uploaded to database';
                 }
            }catch(PDOException $e){
                    echo $e->getMessage();
            }

        }else{

            echo 'file already exists';
        }
    }
    }else{
        echo 'select a file';
    }

HTML and js:

<html>
    <head>
        <meta charset="utf-8">
        <title>A Simple Page with CKEditor</title>
        <!-- Make sure the path to CKEditor is correct. -->

        <style>
            #mydiv{
                position: relative;
                overflow: hidden;
                width:80px;
                height:30px;
                background:crimson;
                color:white;
                text-align:center;
                padding:auto;
                border-radius:4px ;
                border:1px solid black;
                font-size:22px;
            }
            #files{
                   position: absolute;
                   top: 0;
                   right: 0;
                   margin: 0;
                   padding: 0;
                   font-size: 20px;
                   cursor: pointer;
                   opacity: 0;
                   filter: alpha(opacity=0);
            }
        </style>
    </head>
    <body>
    <form action='file.php' method='POST' enctype='multipart/form-data'>
      <div id='mydiv'>upload
        <input type="file" id="files" name="files" multiple />
      </div>
      <input type='submit' value='submit' name='submit'>
    </form>
<output id="list"></output>

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
        return function(e) {
          // Render thumbnail.
          console.log(theFile.type);
          var div = document.createElement('div');
          div.setAttribute('style','width:300px;height:300px;border:1px solid black;position:relative;');
          div.innerHTML = ['<img style="width:300px;height:300px;" class="thumb" src="',e.target.result,
                            '" title="',escape(theFile.name), '"/>'].join('');
          document.getElementById('list').insertBefore(div, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
    </body>
</html>

Checking at your PHP code, it is most probably the $stmt->execute() execution failed.

From PHP documentation ( http://php.net/manual/en/pdo.lobs.php ), you need to open your image in 'rb' mode:

$fp = fopen($_FILES['file']['tmp_name'], 'rb');

in order to insert it to the database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM