PHP部分--圖片上傳服務器、圖片路徑存入數據庫,並讀取


html頁面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文檔</title>
</head>

<body>
<form action="shangchuan.php" method="post" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" value="上傳" />
</form>
</body>
</html>

php處理頁面

<?php
if($_FILES["file"]["error"])
{
    echo $_FILES["file"]["error"];
}
else
{
    //控制上傳文件的類型,大小
    if(($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="image/png") && $_FILES["file"]["size"]<1024000)
    {
        //找到文件存放的位置
        $filename = "./file/".date("YmdHis").$_FILES["file"]["name"];
        
        //轉換編碼格式
        $filename = iconv("UTF-8","gb2312",$filename);
        
        //判斷文件是否存在
        if(file_exists($filename))
        {
            echo "該文件已存在!";
        }
        else
        {
            //保存文件
            move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
        }
    }
    else
    {
        echo "文件類型不正確!";
    }
}

 把圖片路徑存入數據庫(在上傳成功下面調用數據庫存入)

<?php
include("./gongju/DBDA.class.php");
if($_FILES["file"]["error"])
{
    echo $_FILES["file"]["error"];
}
else
{
    //控制上傳文件的類型,大小
    if(($_FILES["file"]["type"]=="image/jpeg" || $_FILES["file"]["type"]=="image/png") && $_FILES["file"]["size"]<1024000)
    {
        //找到文件存放的位置
        $filename = "./file/".date("YmdHis").$_FILES["file"]["name"];
        
        //轉換編碼格式
        $filename = iconv("UTF-8","gb2312",$filename);
        
        //判斷文件是否存在
        if(file_exists($filename))
        {
            echo "該文件已存在!";
        }
        else
        {
            //保存文件
            move_uploaded_file($_FILES["file"]["tmp_name"],$filename);
            
            $db=new DBDA(); $sql="insert into image values('','{$filename}')"; if($db->Query($sql,0)) { echo "數據插入數據庫"; } else { echo "數據未插入數據庫"; };  
        }
    }
    else
    {
        echo "文件類型不正確!";
    }
}

從數據庫中調用圖片路徑,並讀取圖片

<?php
include("./gongju/DBDA.class.php");
$db=new DBDA();
$sql="select img from image where ids=1";
$lujing=$db->StrQuery($sql);
echo $lujing;
echo "<img src='{$lujing}'/>";
?>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM