使用php将图像上传为mysql数据库中的blob

时间:2016-06-16 06:49:34

标签: php mysql

我在Mysql数据库中创建了一个表格,我在那里将图像作为blob存储为blob以及workshop_id,photo_no,filename,filesize,filetype。

要上传文件,我使用以下代码:

photo_upload.php:

<?php

require 'connect_db.inc.php';
if($_FILES['userfile']['size']>0 && isset($_POST['submit']))
{
    if($_FILES['userfile']['error']== UPLOAD_ERR_OK)
    {
    $work_id=mysql_real_escape_string($_POST['work_id']);
    $photo_no=$_POST['photo_no'];
    $photo_no=(int)$photo_no; // Changing photo no. to store many images for the same workshop.
    $photo_no = $photo_no+1; //  Incrementing the last photo no. to get  the current photo no.
    $filename = $_FILES["userfile"]["name"];
    $filetype_upload = $_FILES["userfile"]["type"];
    $filetype = pathinfo($filename, PATHINFO_EXTENSION);
    $filesize = $_FILES["userfile"]["size"];
    $tmpname = $_FILES["userfile"]["tmp_name"];
    //$validtypes = array('jpg','JPG','JPEG','PNG','jpeg','png');
    if($_FILES["userfile"]["type"]="image/gif" || $_FILES["userfile"]["type"]== "image/png" || $_FILES["userfile"]["type"]== "image/jpg" ||$_FILES['userfile']['type']== "image/jpeg" )
    {
        //$filecontent = addslashes(file_get_contents($_FILES['userfile']['tmp_name']));

        $filecontent = mysql_real_escape_string( file_get_contents($_FILES["userfile"]["tmp_name"]) );
        $filename= mysql_real_escape_string($filename);
        $query = "INSERT INTO photographs( Workshop_id,photo_no, filename, filetype, filesize, filecontent) VALUES('$work_id','$photo_no','$filename','$filetype_upload','$filesize','$filecontent') ";
         mysql_query($query) or die("Sorry couldn't upload. Query failed");
         echo'<script>alert("File '.$filename.' has been successfully uploaded");</script>';
         echo '<script>window.location.assign("photograph.php")</script>';

    }
    else
    {
        echo '<script>alert("Sorry the file was not of supported format.");</script>';
        echo '<script>window.location.assign("photograph.php");</script>';
    }
}
    else
    {
    echo '<script>alert(Error in uploading please try later!)</script>';
    echo '<script>window.location.assign("photographs.php")</script>';
    }

}
else
{
    echo '<script>alert("File not submitted properly or No file submitted. Please try again.");</script>';
    echo '<script>window.location.assign("photograph.php");</script>';

}


//if($_FILES['userfile']['type']=="image/gif" || $_FILES['userfile']['type']== "image/png" || $_FILES['userfile']['type']== "image/jpeg" || $_FILES['userfile']['type']== "image/JPEG" || $_FILES['userfile']['type']== "image/PNG" || $_FILES['userfile']['type']== "image/GIF")
?>

为了下载照片,我使用以下代码:

photo_fetch.php:

    <?php
error_reporting(0);
if(isset($_GET['id']) && is_numeric($_GET['id']) )
{
// if id is set then get the file with the id from database

//include 'library/config.php';
require 'connect_db.inc.php';

$id    = $_GET['id'];
$photo_no=$_GET['photo_no'];
$query = "SELECT * " ."FROM photographs WHERE Workshop_id = '$id' and photo_no = '$photo_no' LIMIT 0,30";

$row = mysql_query($query) or die('Error, query failed');

//list($name, $type, $size, $content) =mysql_fetch_array($result);
if(mysql_num_rows($row)!=NULL)
{
while($result = mysql_fetch_assoc($row))
{

$name = $result['filename'];
$type = $result['filetype'];

$size = $result['filesize'];
$content = $result['filecontent'];

//$content2 = stripslashes($content);
//echo $contest2."<br>";

header("Content-type: $type");
header("Content-size: $size");

//readfile($name);
echo $content;
}

}
else
{
   echo '<script>alert("Sorry no such record in the database");</script>';
   echo '<script>window.location.assign("attendance.php");</script>';
}


//$content2 = stripslashes($content);

//include 'library/closedb.php';

}
else
{
    echo '<script>alert("Not happening bro!");</script>';
    echo '<script>window.location.assign("attendance.php");</script>';
}

?>

最奇怪的部分是代码工作正常,直到昨天,今天它无法正常工作。为了记录,我也使用与使用类似代码的图像相同的方式上传pdf文件,并且其工作正常(上载和下载)。任何帮助都会非常有用。

0 个答案:

没有答案