解決 PHP/mySQL 資料庫讀取中文顯示亂碼或問號

發佈時間: 2011-01-09 週日

Felix 發表在 痞客邦 留言(0) 人氣()


 開發一個web項目,通常分為兩部分,一部分是GUI,即界面、美工,使用HTML,CSS,JS編寫,另一部分則是業務邏輯,即程序、功能,使用PHP編寫。而模板引擎則是聯繫這兩部分的「橋樑」,可理解成一個PHP類,裡面定義了許多方法,實現了將PHP的原始輸出加載上界面樣式後再輸出。

Felix 發表在 痞客邦 留言(0) 人氣()

<?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);
?>

Felix 發表在 痞客邦 留言(0) 人氣()

首先要講下 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);
?>
搞定

Felix 發表在 痞客邦 留言(0) 人氣()

 

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).

  • Check apache process owner:  ps aux | grep httpd. The first column will be the owner typically it will be nobody


  • Change 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.



  • Felix 發表在 痞客邦 留言(0) 人氣()


    上傳多個檔案
    可以對 input 功能變數使用不同的 name 來上傳多個檔案。
    PHP 支援同時上傳多個檔案並將它們的訊息自動以陣列的形式組織。要完成這項功能,需要在 HTML 表單中對檔案上傳功能變數使用和多選框與復選框相同的陣列式送出語法。


    注: 對多檔案上傳的支援是在 PHP 3.0.10 版本增加的。




     





    例子 38-4. 上傳多個檔案









    <form action="file-upload.php" method="post" enctype="multipart/form-data">
    Send these files:<br />
    <input name="userfile[]" type="file" /><br />
    <input name="userfile[]" type="file" /><br />
    <input type="submit" value="Send files" />
    </form>







    當以上表單被送出後,陣列 $_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]。   

    Felix 發表在 痞客邦 留言(0) 人氣()

    一開始這樣寫,但出問題
    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);}
    }

    Felix 發表在 痞客邦 留言(0) 人氣()

    <?
    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">&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
    &lt;g2s:g2sMessage xmlns:g2s="http://www.gamingstandards.com/g2s/schemas/v1.0.3"&gt;
    &lt;g2s:g2sAck g2s:dateTimeSent="'.time_xml().'" g2s:egmId="RBG_1234"
    g2s:hostId="2"/&gt;
    &lt;/g2s:g2sMessage&gt;</g2sResponse>
    </soap:Body>
    </soap:Envelope>

    Felix 發表在 痞客邦 留言(0) 人氣()

    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.

    Felix 發表在 痞客邦 留言(0) 人氣()















    <?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; ?>














    Felix 發表在 痞客邦 留言(0) 人氣()

    PHP 5.4 支持 單引號 轉換 htmlspecialchars htmlentities 
     

    var_dump(get_html_translation_table(HTML_ENTITIES, ENT_QUOTES | ENT_XML1));

    The result will look like this:
    array(5) {
    ["""]=>
    string(6) "&quot;"
    ["&"]=>
    string(5) "&amp;"
    ["'"]=>
    string(6) "&apos;"
    ["<"]=>
    string(4) "&lt;"
    [">"]=>
    string(4) "&gt;"
    }

     
    echo htmlspecialchars("&<>\"'", ENT_QUOTES | ENT_XML1);

    echo html_entity_decode("&amp;&lt;&gt;&quot;&apos;", ENT_QUOTES | ENT_XML1);
     
     
    http://nikic.github.io/2012/01/28/htmlspecialchars-improvements-in-PHP-5-4.html

    Felix 發表在 痞客邦 留言(0) 人氣()

    HTTP redirects usually have the response status 301 or 302
    and provide the redirection URL in the “Location” header. I’ve written
    three complementary PHP functions that you can use to find out where an
    URL redirects to (based on a helpful thread at WebmasterWorld). You don’t even need CURL for this – fsockopen() will do just fine.

    Felix 發表在 痞客邦 留言(0) 人氣()

    1 2
    Blog Stats
    ⚠️

    成人內容提醒

    本部落格內容僅限年滿十八歲者瀏覽。
    若您未滿十八歲,請立即離開。

    已滿十八歲者,亦請勿將內容提供給未成年人士。