php环境: Debian 7.8 + Apache 2.2.22 + Mysql 5.5.44 + PHP 5.4.44
1、php文件语法检查
shen@debian:/var/www$ php -l parse-error.php
PHP Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in parse-error.php on line 11
Errors parsing parse-error.php
shen@debian:/var/www$ php -l parse-error.php
PHP Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in parse-error.php on line 11
Errors parsing parse-error.php
shen@debian:/var/www$ cat parse-error.php -n
1
2
3
4
5<?php
6echo "My first PHP script!
7?>
8
9
10
shen@debian:/var/www$ php -l index.php
No syntax errors detected in index.php
shen@debian:/var/www$ cat -n index.php
1
2
3
4
5<?php
6echo "My first PHP script!";
7?>
8
9
10
2、move_uploaded_file移动文件未生效
原因: move_uploaded_file函数的目标目录upload必须对帐号www-data可写
因为apache2服务的启动帐号是www-data,非root
shen@debian:/var/www/shm_fast$ ls -l upload
总用量 24
-rw-r--r-- 1 www-data www-data 23913 9月 1 11:48 1.png
查看apache2进程信息
shen@debian:/var/www/shm_fast$ ps -edf | grep apache2
www-data 2121 27140 0 09:45 ? 00:00:00 /usr/sbin/apache2 -k start
shen 4853 24406 0 11:50 pts/2 00:00:00 grep apache2
root 27140 1 0 8月31 ? 00:00:04 /usr/sbin/apache2 -k start
www-data 29266 27140 0 8月31 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 29483 27140 0 8月31 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 29484 27140 0 8月31 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 29486 27140 0 8月31 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 29487 27140 0 8月31 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 29488 27140 0 8月31 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 29489 27140 0 8月31 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 29491 27140 0 8月31 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 29494 27140 0 8月31 ? 00:00:00 /usr/sbin/apache2 -k start
3、echo打印布尔值FALSE显示为空
shen@debian:/var/www/shm_fast$ php
echo "FALSE=[".FALSE."]\n";
FALSE=[]
FALSE显示为1
shen@debian:/var/www/shm_fast$ php
echo "TRUE=[".TRUE."]\n";
TRUE=[1]
4、move_uploaded_file移动文件为中文文件名,目标文件名显示为乱码
期望: /tmp/2015-03-22 21:03:03的屏幕截图.png
实际: /tmp/2015-03-22 21:03:03????Ļ??ͼ.png
解决:
设置php.ini中的default_charset = "UTF-8"
view /etc/php5/apache2/php.ini
681 ; PHP's default character set is set to empty.
682 ; http://php.net/default-charset
683 default_charset = "UTF-8"
sudo /etc/init.d/apache2 restart
修改upload_file.php:
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "
";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB
";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], "/tmp/" . _FILES["file"]["name"]);
}
echo "Moved to: " . "/tmp/" . $_FILES["file"]["name"];
?>
==>
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "
";
}
else
{
$filename_gbk = $_FILES["file"]["name"];
$filename_utf8 = iconv("GBK", "UTF-8", $filename_gbk);
echo "Upload: " . $filename_utf8 . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB
";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
move_uploaded_file($_FILES["file"]["tmp_name"], "/tmp/" . $filename_utf8);
echo "Moved to: " . "/tmp/" . $filename_utf8;
}
?>
再次查看/tmp目录,能到中文文件名的图片
5、php网页中看不到错误信息
比如
shen@debian:/var/www/shm_fast$ cat trigger-error.php
$test=2;
if ($test>1)
{
trigger_error("Value must be 1 or below");
}
?>
在php命令行执行能显示错误:
shen@debian:/var/www/shm_fast$ php -f trigger-error.php
PHP Notice: Value must be 1 or below in /run/shm/fast_www/trigger-error.php on line 5
浏览器打开php网页的错误在apache的错误日志中:
shen@debian:/var/www/shm_fast$ sudo tail /var/log/apache2/error.log
[Tue Sep 01 13:51:45 2015] [error] [client ::1] Negotiation: discovered file(s) matching request: /var/www/shm_fast/cookie (None could be negotiated).
[Tue Sep 01 13:56:02 2015] [error] [client ::1] PHP Warning: Cookie values cannot contain any of the following ',; \\t\\r\\n\\013\\014' in /run/shm/fast_www/cookie.php on line 3
[Tue Sep 01 14:01:26 2015] [error] [client ::1] Negotiation: discovered file(s) matching request: /var/www/shm_fast/set-cookie (None could be negotiated).
[Tue Sep 01 14:02:42 2015] [error] [client 192.168.1.163] PHP Notice: Undefined index: user in /run/shm/fast_www/get-cookie.php on line 4
[Tue Sep 01 14:55:44 2015] [error] [client ::1] PHP Warning: fopen(welcome.txt): failed to open stream: No such file or directory in /run/shm/fast_www/error.php on line 2
[Tue Sep 01 14:56:12 2015] [error] [client ::1] PHP Warning: fopen(welcome.txt): failed to open stream: No such file or directory in /run/shm/fast_www/error.php on line 2
[Tue Sep 01 15:03:15 2015] [error] [client ::1] script '/var/www/shm_fast/trigger-handler.php' not found or unable to stat
[Tue Sep 01 15:03:24 2015] [error] [client ::1] PHP Notice: Value must be 1 or below in /run/shm/fast_www/trigger-error.php on line 5
[Tue Sep 01 15:04:06 2015] [error] [client ::1] PHP Notice: Value must be 1 or below in /run/shm/fast_www/trigger-error.php on line 5
[Tue Sep 01 15:04:08 2015] [error] [client ::1] PHP Notice: Value must be 1 or below in /run/shm/fast_www/trigger-error.php on line 5
顺便显示一下apache的访问日志:
shen@debian:/var/www/shm_fast$ sudo tail /var/log/apache2/access.log
::1 - - [01/Sep/2015:14:49:43 +0800] "GET /shm_fast/session.php HTTP/1.1" 200 355 "-" "curl/7.26.0"
::1 - - [01/Sep/2015:14:49:43 +0800] "GET /shm_fast/session.php HTTP/1.1" 200 355 "-" "curl/7.26.0"
::1 - - [01/Sep/2015:14:55:44 +0800] "GET /shm_fast/error.php HTTP/1.1" 200 306 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"
::1 - - [01/Sep/2015:14:56:12 +0800] "GET /shm_fast/error.php HTTP/1.1" 200 205 "-" "curl/7.26.0"
::1 - - [01/Sep/2015:14:57:41 +0800] "GET /shm_fast/die.php HTTP/1.1" 200 320 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.2.0"
::1 - - [01/Sep/2015:15:01:55 +0800] "GET /shm_fast/error-handler.php HTTP/1.1" 200 348 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.2.0"
::1 - - [01/Sep/2015:15:03:15 +0800] "GET /shm_fast/trigger-handler.php HTTP/1.1" 404 512 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.2.0"
::1 - - [01/Sep/2015:15:03:24 +0800] "GET /shm_fast/trigger-error.php HTTP/1.1" 200 306 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.2.0"
::1 - - [01/Sep/2015:15:04:06 +0800] "GET /shm_fast/trigger-error.php HTTP/1.1" 200 205 "-" "curl/7.26.0"
::1 - - [01/Sep/2015:15:04:08 +0800] "GET /shm_fast/trigger-error.php HTTP/1.1" 200 205 "-" "curl/7.26.0"
6、error_log输出php日志
shen@debian:/var/www/shm_fast$ cat error-log.php
//error handler function
function customError($errno, $errstr)
{
echo "Error: [$errno] $errstr
";
echo "Webmaster has been notified";
error_log("Error: [$errno] $errstr");
}
//set error handler
set_error_handler("customError",E_USER_WARNING);
//trigger error
$test=2;
if ($test>1)
{
trigger_error("Value must be 1 or below",E_USER_WARNING);
}
?>
**Error: **[512] Value must be 1 or below
Webmaster has been notified
查看apache的错误日志:
shen@debian:/var/www/shm_fast$ sudo tail -1 /var/log/apache2/error.log
[Tue Sep 01 18:38:21 2015] [error] [client ::1] Error: [512] Value must be 1 or below
脚本方式执行error-log.php不会在apache错误日志中输出错误,而是输出到stdout:
shen@debian:/var/www/shm_fast$ php -f error-log.php
Error: [512] Value must be 1 or below
Webmaster has been notifiedError: [512] Value must be 1 or below