發表文章

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

[Laravel]將路由的選擇性參數傳遞至控制器(Controller)

有時候可能會在路由中用到選擇性參數(Optional)像是 Route::get('/activity-record/upload/{ default? }','Admin\activityController@create'); 那麼在Controller中,像下面這樣接值是會報錯的 public function create( $default ) 改成這樣,設定一個初始值就沒問題啦 public function create( $default=null )

如何在PHP/Laravel5中設定CORS header

CORS是什麼? CORS( Cross-Origin Resource Sharing )簡單來說就是跨網站資源存取,出於安全性必須要在伺服端加入 header 這樣 javascript 的請求才不會被封鎖 PHP 寫法 <?php header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS'); header('Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept'); // code here Laravel 寫法 建立中間件 $ php artisan make:middleware Cors Cors.php <?php namespace App\Http\Middleware; use Closure; class Cors { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { return $next($request) ->header('Access-Control-Allow-Origin' , '*') ->header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT, DELETE') ->header('Access-Control-Allow-Headers', 'Content-Type...

Laravel 5 圖片、檔案上傳

比較進階有 Storage::disk() 的方法,可以參考 這裡 view 注意表單上要加 enctype="multipart/form-data 不然會無法傳送檔案 <form action={{ url('webAdmin/calendar/add') }} method="post" enctype="multipart/form-data"> {{ csrf_field() }} <label for="">選擇一個PDF</label><br> <input type="file" name="pdfFile" id="file"><br> <button type="submit">submit</button> controller public function store(Request $request) { try{ $destinationPath = public_path().'/pdf/'; $filename = $request->pdfFile->getclientoriginalname(); $filetype=$request->pdfFile->getMimeType(); if($filetype!='application/pdf'){ return "檔案格式錯誤"; } $unique_name = md5($filename. time()).'.pdf'; $request->file('pdfFile')->move($destinationPath,$uniqu...

PHP傳值給Javascript

PHP <?php $boo='Modern_PHP.pdf'?> Javascript <script> var foo={!! json_encode($foo) !!}; </script>

[PHP,Laravel5.4]Session簡單用法

存值 session(['key' => 'value']); 取值 $value = session('key'); 檢查存在 if (session()->has('username')); 參考:https://laravel.tw/docs/5.3/session

[PHP,Laravel5.4]入門學習筆記01-路由入門

在laravel目錄下routes>web.php 所有的導向設定都在這邊 而你的網頁檔案則放在resources\views 現在來看一下路由基本用法(web.php) 先知道我的主機位址在 http://127.0.0.1/laravel/public/ 首頁就可以這樣設定 Route::get('/',function(){     return view('index');      //抓取放在resources\views底下的index.php 或是index.blade.php }); Route::get('test1/{id}', function ($id) {     return 'User '.$id;    //試試看:你的主機位址/test1/666 }); Route::get('test2/{id?}', function ($id) {     return 'User '.$id;    //這邊在id後面多了?表示這是一個可以選填的變數 }); 通過上面的示範大概也知道除了丟檔案進去目錄外,還需要在web.php上設定路由才能顯示出網頁畫面。使用get的方式也與純php使用?id=xxx的方式不一樣 更多介紹請待下一篇...

[PHP,MySQL]圖片上傳和讀取 使用base64_encode & base64_decode

2017/08/10更新  classicBookShareUpLoad.php 刪除一些沆碼 2017/08/10更新 更新後第二段SQL與第三段SQL會對不上,請自行修改 此篇主要是做紀錄用然後稍作一些修改,主要參考自: http://www.webtech.tw/info.php?tid=24 http://forum.twbts.com/thread-3299-1-1.html http://stackoverflow.com/questions/16262098/displaying-a-base64-images-from-a-database-via-php 摘要: base64_encode主要是使上傳結果正確 $_FILES["file"]["type"]的紀錄是為了讓讀圖的時候能有正確的解碼方式(即資料庫除了存圖片的空間外還要另外設置一個欄位儲存file type) 資料庫設置: 圖片存放空間:mediumblob 圖片型態存放空間:varchar(50) 上傳頁面: < html > < body > < Form Action = "classicBookShareUpLoad.php" Method = "POST" Enctype = "multipart/form-data" > < Input Type = "File" Name = "upfile" >< br > < Input Type = "Submit" value = " 開始上傳 " > </ Form > </ body > </ html > classicBookShareUpLoad.php: <?php include ( "loginStaCheck.php" ); ?> <?php //開啟圖片檔 $file = fopen ( $_F...

[PHP,Javascript]線上文字編輯器CKEditor簡易使用範例

今天來說說ckeditor ,一個Open Sourse的線上文字編輯器,可以方便終端使用者編輯要發布的文章。 首先你會需要去 ckeditor.com 把檔案下載回來後加入到你的檔案中,下面示範了如何建立你的編輯器,然後取得文字資料並透過PHP把資料傳送給mySQL webNewsEditor.php: <!DOCTYPE html> <html>     <head>         <meta charset="utf-8">         <title>CKEditor</title>         <script src="./ckeditor/ckeditor.js"></script>     </head>     <body>         <form name = 'form' action = 'webNewsEditorResult.php' method='post'>             <input type="text" id="title" name="title" class="form-control" placeholder="文章標題" required>             <input type="text" id="schoolSelect" name="schoolSelect" class="form-control" placeholder="學校" required>             <textarea name="content" id="content" rows="10" cols="80">...

[PHP]基本網站登入頁面示例

圖片
套板樣式使用Bootstrap,不知道什麼是Bootstrap的可以自行google一下,然後這邊先直接假設已經在mySQL設定好一組使用者帳號。 資料庫內容大致上是這樣 其中account設定為主鍵 signin.php: <!DOCTYPE html> <html lang="en">   <head>     <meta charset="utf-8">     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1">     <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->     <title>管理系統</title>     <!-- Bootstrap -->     <link href="css/bootstrap.min.css" rel="stylesheet">     <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->     <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->     <!--[if lt IE 9]>       <script src="https://oss.maxcdn.com/...

[PHP]phpMyAdmin中文亂碼解決方式

1.資料庫資料表的"編碼與排序"均設定成"utf8_unicode_ci" 2.PHP語法加入  header("Content-Type:text/html; charset=utf-8"); 3.mysqli_query($conn,"SET NAMES 'utf8'"); //其中$conn資料庫是連線資訊

[PHP]MySQL連接方法

<?php //php顯示編碼 header("Content-Type:text/html; charset=utf-8"); //連接資料庫 $con = mysql_connect("localhost", "philip", "philip01"); mysql_query("set names utf8"); //連接狀態結果 if (!$con)   {   die('Could not connect: ' . mysql_error());   } $db_selected = mysql_select_db("teaching_english", $con); if (!$db_selected)   {   die ("Can\'t use test_db : " . mysql_error());   } ?>