PHP 上傳/下載儲存在資料庫中的imge類型資料(圖片)

來源:互聯網
上載者:User
這篇文章介紹的內容是關於PHP 上傳/下載儲存在資料庫中的imge類型資料(圖片),有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下

//上傳掃描件(圖片以img資料類型存入資料中)function mod_addPhoto(){    global $row_User;        global $mssql_connection;        if ($_FILES['myfile']['error'] > 0) {            $back = array(                        'message' => 'error',                        'date' => '檔案錯誤'                    );                    echo json_encode($back);                    exit();                } else {                    //判斷是否選擇了要上傳的表格                    if (empty($_POST['upfile'])) {                       $back = array(                                       'message' => 'error',                                       'date' => '上傳檔案不可為空'                                   );                                   echo json_encode($back);                                   exit();                               }                               //擷取表格的大小,限制上傳表格的大小5                               $file_size = $_FILES['myfile']['size'];                               if ($file_size > 100 * 1024 * 1024) {                                   $back = array(                                                   'message' => 'error',                                                   'date' => '檔案大小不能超過5M'                                               );                                               echo json_encode($back);                                               exit();                                           }                                           //擷取檔案參數                                           $imgfile = $_FILES['myfile'];                                           $name = $imgfile['name'];   //取得圖片名稱                                           $type = $imgfile['type']; //取得圖片類型                                           $size = $imgfile['size'];  //取得圖片長度                                           $tmpfile = $imgfile['tmp_name'];  //圖片上傳上來到臨時檔案的路徑                                            $file = fopen($tmpfile, 'rb');                                            $content = fread($file, $size);                                            $content = "0x" . bin2hex($content);                                            fclose($file);                                            //限制上傳表格類型                                            $file_type = $_FILES['myfile']['type'];                                            $file_type=substr($file_type, 6);                                            //擷取目前時間                                            date_default_timezone_set('PRC');                                            $now_time = date('Y-m-d H:i:s', time());                                            //儲存到資料庫                                            $OAV_ID = $_GET['OAV_ID'];                                            $OVA_ID = create_ID("OA_VehicleAttach");                                            global $mssql_connection;                                             if (!$mssql_connection) {                                                 $mssql_connection = open_mssqlconn();                                           }                                           $sfu_id = $row_User['SFU_ID'];                                           $sql1 = "INSERT INTO OA_VehicleAttach (OAV_ID, OVA_ID,OVA_Name, OVA_Date, OVA_Type, OVA_Size, OVA_Data, OVA_User)                                    VALUES ('$OAV_ID', '$OVA_ID', '$name','$now_time','$file_type',$file_size, $content,'$sfu_id' )";                                            $sql = iconv("utf-8", "gbk", $sql1);                                       //PHP使用UTF-8編碼。SQL Server 2008使用預設的GBK編碼  在執行查詢語句之前將查詢語句及所要插入的資料轉化為GBK編碼。                                            $cursor = sqlsrv_query($mssql_connection, $sql);                                            $aa = sqlsrv_rows_affected($cursor);                                            if ($aa) {                                                $back = array(                                                    'message' => 'success',                                                    'date' => '檔案上傳成功'                                                 );                                                 echo json_encode($back);                                                 exit();                                             } else {                                                $back = array(                                                   'message' => 'fail',                                                    'date' => '檔案上傳失敗'                                                 );                                                 echo json_encode($back);                                                 exit();                                             }                                         }                                     }//下載掃描件function  mod_down_scanning_copy(){    $id = $_GET['OVA_ID'];        //$id = getGP('OAV_ID', 'G');        $sql = "SELECT OVA_ID,OAV_ID,OVA_Name,OVA_Data,OVA_User,OVA_Size FROM OA_VehicleAttach where OVA_ID='" . $id . "'";        $cursor = mssqlquery($sql);        $row = sqlsrv_fetch_array($cursor, SQLSRV_FETCH_ASSOC);        if(isset($row['OVA_Name'])){            $filename=$row['OVA_Name'];             $filedata=$row['OVA_Data'];             $filesize=$row['OVA_Size'];         }        // $binaryStr = base64_decode($filedata);      //   file_put_contents($filename, $binaryStr);         $filePath = 'download/' . $filename;         $file = fopen($filePath, "w");//開啟檔案準備寫入         fwrite($file, $filedata);//寫入         fclose($file);//關閉     //    header("content-disposition:attachment;filename=".$filename);     //    header("content-length:".$filesize);     //    readfile($filedata);         if(file_exists($filePath)){             header("Content-type:application/octet-stream");             header("Content-Disposition:attachment;filename = ".$filename);             header("Accept-ranges:bytes");             header("Content-length:".$filesize);             readfile($filePath);         }}//SQL查詢function mssqlquery($Q){    global $mssql_connection;        if (!$mssql_connection) {            $mssql_connection = open_mssqlconn();        }         $Q = iconv("utf-8", "gbk", $Q);         $cursor = sqlsrv_query($mssql_connection, $Q);         if (!$cursor) {             echo "SQL查詢出錯.\n";             die(print_r(sqlsrv_errors(), true));         }         return $cursor;     }//擷取資料庫連接$mssql_connection = '';function open_mssqlconn(){    global $mssql_connection;    //    global $MSSQL_SERVER;    //    global $connectionInfo;        $MSSQL_SERVER = "DESKTOP-CA04H1Q\SQLEXPRESS";        $connectionInfo = array("Database" => "ToncentHuaJie", "UID" => "使用者名稱", "PWD" => "密碼");        if (!$mssql_connection) {            $mssql_link = sqlsrv_connect($MSSQL_SERVER, $connectionInfo);    //            sqlsrv_query("set NAMES GBK");            if (!$mssql_link) {                echo "Could not connect.\n";                die(print_r(sqlsrv_errors(), true));            }             return $mssql_link;         } else {             return $mssql_connection;         }     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.