發表文章

目前顯示的是有「php」標籤的文章

mysql 主鍵使用UUID, bigint, int?

最近開發建立資料表紀錄log時,以往都是使用主鍵都是設定int unsigned類型,這次在開發時,考量到專案log可能資料量大,或是該專案的長遠性,所以查詢是不是有更好的類型設定。 查到的資料與請教其他前輩有使用int unsigned, bigint unsigned, UUID, 複合式主鍵 這四種,整理如下: UUID: 資料量大的網站,使用UUID不會重複,也較無上限問題。 缺點:儲存內容多,UUID不直覺,無法排序(需要使用其他欄位輔助排序) mysql 語法:SELECT UUID(); 參考資料: https://hoohoo.top/blog/php-create-uuid-and-guid/ PHP前輩

[PHP] curl post

使用curl post傳送/取回資料 //虛擬網址 $url = 'https://www.test.com'; //要post過去的值 $post_arr = array('name' => 'joy', 'email' => 'joy@gmail.com'); $ch = curl_init(); //curl init curl_setopt($ch, CURLOPT_URL, $url); //設定連線網址 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded')); //設定header curl_setopt($ch, CURLOPT_POST, true); //啟用post curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_arr)); //傳入post參數 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //使用https網址, 而無法正常運作時, 要加這個 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //使用https網址, 而無法正常運作時, 要加這個 $result = curl_exec($ch); //執行, 送出post並將結果存回 curl_close($ch); //關閉連線 參考資料: https://blog.johnsonlu.org/php-curl/

[PHP] 檔案或目錄函式

file_exists( ):檢查檔案或目錄是否存在 is_file( ):檢查檔案是否存在 is_dir( ):檢查目錄是否存在 mkdir():建立目錄 unlink( ):刪除檔案 rmdir():刪除目錄 大致用法如下: $file = $_SERVER[DOCUMENT''].'/files/test.txt'; if (file_exists($file)) { echo "檔案存在"; } else { echo "無檔案"; } 參考資料: https://www.php.net/manual/en/function.file-exists.php https://www.php.net/manual/en/function.is-file.php https://www.php.net/manual/en/function.is-dir.php https://www.php.net/manual/en/function.mkdir.php https://www.php.net/manual/en/function.unlink.php https://www.php.net/manual/en/function.rmdir.php

[PHP] 頁面強制檔案下載語法

強制檔案下載 $file = $_SERVER['DOCUMENT'].'download/file.xlsx'; if (is_file($file)) { header("Content-type: application"); //視不同格式, 填入不同 header("Content-Disposition: attachment; filename=file.xlsx"); //指定下載檔名 @readfile($file); //將檔案寫入緩衝區 } else { echo "file not exist."; } 參考資料: https://codertw.com/%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80/206063/