[PHP] Debian 安裝 Composer

先安裝 php-cli ( PHP command line )

 sudo apt install php-cli

下載 Composer 安裝檔

 php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

驗證下載檔,https://composer.github.io/pubkeys.html 中取得 驗證碼,並帶入指令中,如果回覆Installer verified代表下載檔可以正常安裝

 php -r "if (hash_file('sha384', 'composer-setup.php') === '驗證碼') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

安裝 composer

php composer-setup.php

移除安裝檔

 php -r "unlink('composer-setup.php');"

將執行檔搬移到執行檔目錄

 sudo mv composer.phar /usr/local/bin/composer

安裝完成囉

#composer -V

Composer version 2.3.5 2022-04-13 16:43:00

PHP 載入圖檔

可用於載入內部非web資料夾圖檔,或外部http圖檔

$source_img = '/home/test/test.jpg'; 
//$source_img = 'https://test.jpg';
@$im = imagecreatefromjpeg($source_img);


if(!$im){ // 載入失敗顯示錯誤圖
	$w = 200;
	$h = 200;
	$fontsize = 5; // 1,2,3,4,5 max 5
	$errorstr = 'Image error';
	$im  = imagecreatetruecolor($w, $h); 
	$bgcolor = imagecolorallocate($im, 255, 255, 0);
	$textcolor  = imagecolorallocate($im, 0, 255, 255);

	imagefilledrectangle($im, 0, 0, $w, $h, $bgcolor); // 填滿背景
	$fw = imagefontwidth($fontsize);     // width of a character
	$fh = imagefontheight($fontsize);     // height of a character
	$l = strlen($errorstr);          // number of characters
	$tw = $l * $fw;              // text width
	$th = 1 * $fw;              // text height

	$xpos = ($w - $tw)/2;
	$ypos = ($h - $th)/2;

	imagestring($im, $fontsize, $xpos, $ypos, $errorstr , $textcolor);
}

header("Content-type: image/jpeg");
imagejpeg($im);
imagedestroy($im);
exit;

PHP 多檔案上傳

name 設為[]陣列 ,並在後方加上 multiple

<input type="file" name="userImage[]" id="userImage" multiple>

上傳處理

foreach( $_FILES[userImage][tmp_name] AS $k => $v){
	if(is_uploaded_file($_FILES['userImage']['tmp_name'][$k])) {
		$source_file = $_FILES['userImage']['tmp_name'][$k];
		$target_file = date( "Ymd.His").'_'.$_FILES['userImage']['name'][$k];
		if(move_uploaded_file($source_file,$target_file)) {
			echo $source_file.'上傳成功';
		}
	}
}

電話超連結

按下連結時會自動開啟撥號程式打電話

連結格式 tel:電話號碼

<a href="tel:<?=$mobile;?>"><?=$mobile;?></a>
<a href="tel:<?=$tel;?>"><?=$tel;?></a>

國際標準

+國碼-電話號碼,台灣號碼如果開頭為0要把0拿掉

$call_mobile_pos = strpos($mobile, '0');
if( $call_mobile_pos === 0) {
    $call_mobile = substr($mobile,1); 
} 
<a href="tel:+886-<?=$call_mobile;?>"><?=$mobile;?></a>

市話區碼開頭為0也要移除

if( strpos($tel, '0' ) === 0) {
     $call_tel = substr($tel,1);
}
<a href="tel:+886-<?=$call_tel;?>"><?=$tel;?></a>

Bootstrap 4.6 初始頁面

一切從此開始

<?
$title= 'Hello, world!';
?>
<!doctype html>
<html lang="zh-Hant-TW">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
	<!-- jquery ui css -->
	<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" >

    <title><?=$title?></title>
  </head>
  <body>
    <h1>Hello, world!</h1>

    <!-- Optional JavaScript; choose one of the two! -->

    <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
    <!-- no ajax <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
	<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script>
	<!-- UI -->
	<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js" integrity="sha256-VazP97ZCwtekAsvgPBSUwPFKdrwD3unUfSGVYrahUqU=" crossorigin="anonymous"></script>
    <!-- Option 2: Separate Popper and Bootstrap JS -->
    <!--
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js" integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.min.js" integrity="sha384-VHvPCCyXqtD5DqJeNxl2dtTyhF78xXNXdkwX1CZeRusQfRKp+tA7hAShOK/B/fQ2" crossorigin="anonymous"></script>
    -->
  </body>
</html>

[PHP]phplist 電子報 安裝教學

下載phplist:請至 https://www.phplist.org/ 官網下載
下載後解壓縮,只需要裡面的list資料夾

建立資料庫並設定連線帳號:

mysql> CREATE DATABASE 'phplistdb'; // 建立資料庫
mysql> CREATE USER 'phplist'@'localhost' IDENTIFIED BY 'password'; // 建立連線使用者
mysql> GRANT ALL PRIVILEGES ON phplist.* TO 'phplist'@'localhost'; // 設定使用者權限
mysql> FLUSH PRIVILEGES; // 更新權限

config/config.php 基本設定( 更多設定資料可參考config_extended.php )

 // 語言
 $language_module = 'english.inc';
 // 資料庫位址
 $database_host = 'localhost';
 // 資料庫名稱
 $database_name = 'phplistdb';
 // 資料庫連線使用者
 $database_user = 'phplist';
 // 資料庫密碼
 $database_password = 'password';
 // 如果有更換路徑請加入此設定
 $pageroot = '/lists';

資料庫初始化設定:

至管理介面 http://URL/lists/admin/
按下 Initialise Database 連結
照說明填寫設定欄位就可以使用

[PHP]CURL POST 資料

function CurlPost($PostURL,$PostData,$PStr=null){
     if($PStr){ //使用字串方式post
         foreach ($PostData as $k => $v) {
             $PostStr .= $k.'='.urlencode($v).'&';
             $PostCount++; // 計算共幾個post欄位
         }
         $PostStr = substr($PostStr,0,-1);//消去字尾多餘的 '&'
     }
     $ch = curl_init();
     curl_setopt($ch,CURLOPT_URL, $PostURL); // 設定所要傳送網址
     curl_setopt($ch,CURLOPT_HEADER, false); // 不顯示網頁
     curl_setopt($ch,CURLOPT_POST,1); // 開啟回傳
     curl_setopt($ch,CURLOPT_POSTFIELDS,$PostData); // 將post資料塞入
     curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); // 開啟將網頁內容回傳值
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //略過SSL連線驗證
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //略過SSL連線驗證
     $GetPost = curl_exec($ch); // 執行網頁
     curl_close($ch); // 關閉網頁
     return $GetPost;
 }

PHP & ajax

$.ajax({
url: '//www.gocar.idv.tw/tset/test.php',     
cache:false,      
async:false,     
type: "POST",     
dataType: 'json',     
data: '&key='+key ,     
success: function (data) {          
    $.each(data, function(index, val) {                   
    });
}, error:function(msg){         
      alert('錯誤!'+msg);     
    } 
});