發佈時間: 2011-01-09 週日
- 10月 18 週六 201410:18
解決 PHP/mySQL 資料庫讀取中文顯示亂碼或問號
解決 PHP/mySQL 資料庫讀取中文顯示亂碼或問號
發佈時間: 2011-01-09 週日
發佈時間: 2011-01-09 週日
- 8月 24 週日 201413:44
PHP模板引擎原理

開發一個web項目,通常分為兩部分,一部分是GUI,即界面、美工,使用HTML,CSS,JS編寫,另一部分則是業務邏輯,即程序、功能,使用PHP編寫。而模板引擎則是聯繫這兩部分的「橋樑」,可理解成一個PHP類,裡面定義了許多方法,實現了將PHP的原始輸出加載上界面樣式後再輸出。
- 2月 19 週三 201408:32
php fopen download file 下載檔案
<?php
$content = "";
$fp = fopen("http://file-to-download.com/a-file.zip", "rb");
if (!$fp)
die("Error opening file.");
while (!feof($fp))
$content .= fread($fp, 2048);
fclose($fp);
$fp=fopen("local-file-name.zip", "w");
fwrite($fp, $content);
fclose($fp);
?>
$content = "";
$fp = fopen("http://file-to-download.com/a-file.zip", "rb");
if (!$fp)
die("Error opening file.");
while (!feof($fp))
$content .= fread($fp, 2048);
fclose($fp);
$fp=fopen("local-file-name.zip", "w");
fwrite($fp, $content);
fclose($fp);
?>
- 2月 17 週一 201420:41
在 php 使用 at 命令 增加排程
首先要講下 at 命令 是要用 /bin/bash shell 來執行的,
你可以在php裡查一下 apache 使用的是哪個 shell,
<?
$output = shell_exec('echo $SHELL');
echo $output;
?>
centos 預設 apache 的 shell 是 /bin/nologin
在linux 裡改
usermod -s /bin/bash apache
這樣就可以apache 就可以使用 at 了
由於at指令不同其他,不能用shell_exec執行,所以用以下方法
<?php
$r = popen('/usr/bin/at noon',"w");
fwrite($r,"ls -l");
pclose($r);
?>
搞定
你可以在php裡查一下 apache 使用的是哪個 shell,
<?
$output = shell_exec('echo $SHELL');
echo $output;
?>
centos 預設 apache 的 shell 是 /bin/nologin
在linux 裡改
usermod -s /bin/bash apache
這樣就可以apache 就可以使用 at 了
由於at指令不同其他,不能用shell_exec執行,所以用以下方法
<?php
$r = popen('/usr/bin/at noon',"w");
fwrite($r,"ls -l");
pclose($r);
?>
搞定
This is because
images and tmp_file_upload are only writable by root user. For upload to work we need to make the owner of those folders same as httpd process owner OR make them globally writable (bad practice).ps aux | grep httpd. The first column will be the owner typically it will be nobodyChange the owner of images and tmp_file_upload to be become nobody or whatever the owner you found in step 1.
$sudo chown nobody /var/www/html/mysite/images/
$sudo chown nobody /var/www/html/mysite/tmp_file_upload/
Chmod images and tmp_file_upload now to be writable by the owner, if needed [Seems you already have this in place]. Mentioned in @Dmitry Teplyakov answer.
$ sudo chmod -R 0755 /var/www/html/mysite/images/
$ sudo chmod -R 0755 /var/www/html/mysite/tmp_file_upload/
For more details why this behavior happend, check the manual http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir , note that it also talking about open_basedir directive.
- 2月 16 週日 201408:31
php 上傳多個檔案
上傳多個檔案
可以對 input 功能變數使用不同的 name 來上傳多個檔案。
PHP 支援同時上傳多個檔案並將它們的訊息自動以陣列的形式組織。要完成這項功能,需要在 HTML 表單中對檔案上傳功能變數使用和多選框與復選框相同的陣列式送出語法。
注: 對多檔案上傳的支援是在 PHP 3.0.10 版本增加的。
例子 38-4. 上傳多個檔案
<form action="file-upload.php" method="post" enctype="multipart/form-data"> |
當以上表單被送出後,陣列 $_FILES['userfile'],$_FILES['userfile']['name'] 和 $_FILES['userfile']['size'] 將被起始化(在 PHP 4.1.0 以前版本是 $HTTP_POST_FILES)。若果 register_globals 的設定為 on,則和檔案上傳關聯的全局變量也將被起始化。所有這些送出的訊息都將被儲存到以數字為索引的陣列中。
例如,假設名為 /home/test/review.html 和 /home/test/xwp.out 的檔案被送出,則 $_FILES['userfile']['name'][0] 的值將是 review.html,而 $_FILES['userfile']['name'][1] 的值將是 xwp.out。類似的,$_FILES['userfile']['size'][0] 將包括檔案 review.html 的大小,依此類推。
此外也同時設定了 $_FILES['userfile']['name'][0],$_FILES['userfile']['tmp_name'][0],$_FILES['userfile']['size'][0] 以及 $_FILES['userfile']['type'][0]。
- 2月 09 週日 201419:00
php 遞歸 問題
一開始這樣寫,但出問題
function trim_dom_node($string){
$string=str_replace(" <","<",$string,$count);
echo "com ";
if($count >= 1)
{
trim_dom_node($string);
}
else {return str_replace("\n<","<",$string);}
}
function trim_dom_node($string){
$string=str_replace(" <","<",$string,$count);
echo "com ";
if($count >= 1)
{
trim_dom_node($string);
}
else {return str_replace("\n<","<",$string);}
}
- 2月 04 週二 201415:20
php 回覆 xml 格式 資料
<?
header("Content-Type:text/xml");
echo "<
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<g2sResponse xmlns="http://www.gamingstandards.com/wsdl/g2s/v1.0"><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<g2s:g2sMessage xmlns:g2s="http://www.gamingstandards.com/g2s/schemas/v1.0.3">
<g2s:g2sAck g2s:dateTimeSent="'.time_xml().'" g2s:egmId="RBG_1234"
g2s:hostId="2"/>
</g2s:g2sMessage></g2sResponse>
</soap:Body>
</soap:Envelope>
header("Content-Type:text/xml");
echo "<
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<g2sResponse xmlns="http://www.gamingstandards.com/wsdl/g2s/v1.0"><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<g2s:g2sMessage xmlns:g2s="http://www.gamingstandards.com/g2s/schemas/v1.0.3">
<g2s:g2sAck g2s:dateTimeSent="'.time_xml().'" g2s:egmId="RBG_1234"
g2s:hostId="2"/>
</g2s:g2sMessage></g2sResponse>
</soap:Body>
</soap:Envelope>
- 2月 03 週一 201415:20
PHP 物件導向 設計 Build seven good object-oriented habits in PHP
In the early days of PHP programming, PHP code was limited to being procedural in nature. Procedural codeis characterized by the use of procedures for the building blocks of the application. Procedures offer a certain level of reuse by allowing procedures to be called by other procedures.
However, without object-oriented language constructs, a programmer can still introduce OO characteristics into PHP code. It's a tad more difficult and can make the code more difficult to read because it's mixing paradigms (procedural language with pseudo-OO design). OO constructs in PHP code — such as the ability to define and use classes, the ability to build relationships between classes that use inheritance, and the ability to define interfaces — make it much easier to build code that adheres to good OO practices.
While purely procedural designs without much modularity run just fine, the advantages of OO design show up in the maintenance. Because a typical application will spend the bulk of its lifetime in maintenance, code maintenance is a large expense over the lifetime of an application. It can also be easily forgotten during development. If you're in a race to get your application developed and deployed, long-term maintainability can take a back seat to getting something to work.
However, without object-oriented language constructs, a programmer can still introduce OO characteristics into PHP code. It's a tad more difficult and can make the code more difficult to read because it's mixing paradigms (procedural language with pseudo-OO design). OO constructs in PHP code — such as the ability to define and use classes, the ability to build relationships between classes that use inheritance, and the ability to define interfaces — make it much easier to build code that adheres to good OO practices.
While purely procedural designs without much modularity run just fine, the advantages of OO design show up in the maintenance. Because a typical application will spend the bulk of its lifetime in maintenance, code maintenance is a large expense over the lifetime of an application. It can also be easily forgotten during development. If you're in a race to get your application developed and deployed, long-term maintainability can take a back seat to getting something to work.
- 2月 02 週日 201418:58
php function 參數 數目 可變
<?php function foo() {
$numargs = func_num_args(); if ($numargs > 2) {
echo "First: ". func_get_arg(0). "\n";
echo "Second: ". func_get_arg(1). "\n";
echo "Third: ". func_get_arg(2). "\n";
} return $numargs;
}
$n = foo (10, 15, 20); //傳入 3 個參數 echo $n; ?>