web service programming 張賢宗. 檔案目錄 2015/10/11introduction to php2
TRANSCRIPT
真實路徑 realpath()
<?phpchdir('/var/www/');echo realpath('./../../etc/passwd');?>
/etc/passwd <?php
echo realpath('/windows/system32');?>
C:\WINDOWS\System32
112/04/19 Introduction to PHP 3
目錄名稱 dirname()
<?php//before PHP 4.3.0dirname('c:/'); // returned '.‘//after PHP 4.3.0dirname('c:/x'); // returns 'c:\'dirname('c:/Temp/x'); // returns 'c:/Temp'dirname('/x'); // returns '\‘?>
112/04/19 Introduction to PHP 4
檔案名字 basename()
<?phpecho "1) ".basename("/etc/sudoers.d", ".d").PHP_EOL;echo "2) ".basename("/etc/passwd").PHP_EOL;echo "3) ".basename("/etc/").PHP_EOL;echo "4) ".basename(".").PHP_EOL;echo "5) ".basename("/");?>
1) sudoers 2) passwd 3) etc 4) . 5)
112/04/19 Introduction to PHP 5
檔案資訊 fileatime filectime filegroup fileinode filemtime fileowner fileperms filesize filetype
112/04/19 Introduction to PHP 7
檔案類型 is_dir is_file is_link is_readable is_writable is_executable is_uploaded_file
112/04/19 Introduction to PHP 8
一次讀進檔案內容 $a=file( $filename );
$a 是一個字串陣列 $b=file_get_contents( $filename )
$b 是一個字串 readfile()
把檔案內容直接輸出到 stdout
112/04/19 Introduction to PHP 9
讀入一個目錄內容<?php
$dir = '/tmp';$files1 = scandir($dir);$files2 = scandir($dir, 1);
print_r($files1);print_r($files2);?>
112/04/19 Introduction to PHP 11
ResultsArray
(
[0] => .
[1] => ..
[2] => bar.php
[3] => foo.txt
[4] => somedir
)
Array
(
[0] => somedir
[1] => foo.txt
[2] => bar.php
[3] => ..
[4] => .
)
112/04/19 Introduction to PHP 12
讀入一個目錄 2<?php
$dir = "/tmp";$dh = opendir($dir);while (false !== ($filename = readdir($dh))) { $files[] = $filename;}
sort($files);
print_r($files);
rsort($files);
print_r($files);
?>
112/04/19 Introduction to PHP 13
ResultsArray
(
[0] => .
[1] => ..
[2] => bar.php
[3] => foo.txt
[4] => somedir
)
Array
(
[0] => somedir
[1] => foo.txt
[2] => bar.php
[3] => ..
[4] => .
)
112/04/19 Introduction to PHP 14
URL as Filename
Filename can be a URL fopen(“http://www.widelab.org”); Options
Method, header, user_agent, cotent$op=array(“http”=>array( “method”=>”GET”,
“user_agent” =>”PHP-5”);$cc=stream_context_creat($op);file(“http://www.widelab.org”, 0,$cc);
112/04/19 Introduction to PHP 15
今天練習作業 請設計一個 php 程式,檔名為 hw3.php
,並使用 GET METHOD ,傳入一個參數 fn ,指定目錄名稱或是檔案名稱,如果 fn 所指定的路徑是一個目錄,請顯示該目錄下所有目錄與檔案,並且顯示該目錄的 檔案大小(目錄免)、建立時間(或是你想顯示的其他資訊)。如果 fn 所指定的路徑是一個檔案,請顯示該檔案的內容。
112/04/19 Introduction to PHP 17
For Example
http://www.widelab.org/~smallpig/hw3.php?fn=/data/php/zz/將會顯示 /data/php/zz/ 裡面的所有檔案列表
112/04/19 Introduction to PHP 18
注意事項 改作業時, Default 的路徑(就是使用者
沒有指定 fn 時)為 自己的 Home Directory ,另外如果使用者所指定的路徑 Prefix 不是 Home Directory 的話,請顯示錯誤訊息,不可以任意顯示其他目錄檔案。
你的目錄內至少要有圖、 Word 、文字檔與最少三層目錄。
112/04/19 Introduction to PHP 19
注意事項 如果路徑為目錄,必須能 Recursive 的繼續點
選。要能顯示中文檔名。 請參考
opendir() 、 is_dir() 、 is_file() 、 strncmp() 、 dir() 、 fopen() 、 fread() 、 fclose() 、 file_get_contents() 、 $_GET["fn"]
如果圖檔無法顯示的話,請在 php 最前面加上 header('Content-Type: image/jpeg');
如果文字檔無法顯示的話(應該不可能),請在 php 最前面加上 header("Content-type: text/plain");
112/04/19 Introduction to PHP 20
什麼是 headerTrying 163.25.114.31...
Connected to cguwwwcc.cgu.edu.tw.
Escape character is '^]'.
HTTP/1.1 200 OK
Content-Length: 49087
Content-Type: text/html
Content-Location: http://163.25.114.4/index.html
Last-Modified: Fri, 02 Apr 2010 08:14:11 GMT
Accept-Ranges: bytes
ETag: "f039e793cd2ca1:29f"
Server: Microsoft-IIS/6.0
Date: Tue, 06 Apr 2010 03:39:37 GMT
Connection: close
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><!-- InstanceBegin template="/Templates/95cgu.dwt" codeOutsideHTMLIsLocke
<head>
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<!-- InstanceBeginEditable name="doctitle" -->
<title> 長庚大學 </title>
<!-- InstanceEndEditable -->
112/04/19 Introduction to PHP 21
Header
void header ( string $string [, bool $replace = true [, int $http_response_code ]] )header('Content-type: application/pdf');
$replace header('WWW-Authenticate: Negotiate');
header('WWW-Authenticate: NTLM', false);
$http_response_code
112/04/19 Introduction to PHP 22
Special Header header("Location: http://www.example.com/"); header("Cache-Control: no-cache, must-
revalidate"); // HTTP/1.1header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
http://www.faqs.org/rfcs/rfc2616 HTTP/1.1 Header
112/04/19 Introduction to PHP 23
什麼是 content-type 讓 Browser 知道接收到的檔案類型為何 常見的 content-type
txt' => 'text/plain‘, 'htm' => 'text/html', 'html' => 'text/html', 'jpeg' => 'image/jpeg‘, 'jpg' => 'image/jpeg',
透過 string mime_content_type ( string $filename ) 取得
宣告方式 header("Content-type: text/plain"); header(" Content-type: “. mime_content_type
($filename));
112/04/19 Introduction to PHP 25
如果系統沒有 ime_content_typeif(!function_exists('mime_content_type')) {
function mime_content_type($filename) {
$mime_types = array(
'txt' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', 'php' => 'text/html', 'css' => 'text/css', 'js' => 'application/javascript', 'json' => 'application/json', 'xml' => 'application/xml', 'swf' => 'application/x-shockwave-flash', 'flv' => 'video/x-flv',
// images 'png' => 'image/png', 'jpe' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'bmp' => 'image/bmp', 'ico' => 'image/vnd.microsoft.icon', 'tiff' => 'image/tiff', 'tif' => 'image/tiff', 'svg' => 'image/svg+xml', 'svgz' => 'image/svg+xml',
// archives 'zip' => 'application/zip', 'rar' => 'application/x-rar-compressed', 'exe' => 'application/x-msdownload', 'msi' => 'application/x-msdownload', 'cab' => 'application/vnd.ms-cab-compressed',
'mp3' => 'audio/mpeg', 'qt' => 'video/quicktime', 'mov' => 'video/quicktime',
// adobe 'pdf' => 'application/pdf', 'psd' => 'image/vnd.adobe.photoshop', 'ai' => 'application/postscript', 'eps' => 'application/postscript', 'ps' => 'application/postscript',
// ms office 'doc' => 'application/msword', 'rtf' => 'application/rtf', 'xls' => 'application/vnd.ms-excel', 'ppt' => 'application/vnd.ms-powerpoint',
// open office 'odt' => 'application/vnd.oasis.opendocument.text', 'ods' => 'application/vnd.oasis.opendocument.spreadsheet', );
$ext = strtolower(array_pop(explode('.',$filename))); if (array_key_exists($ext, $mime_types)) { return $mime_types[$ext]; } elseif (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME); $mimetype = finfo_file($finfo, $filename); finfo_close($finfo); return $mimetype; } else { return 'application/octet-stream'; } }}?>
112/04/19 Introduction to PHP 26
如何顯示檔案 Home: ~/public_html File: ~/public_html/xx/a.file HomeURL: http://web.csie.cgu.edu.tw/~000/
直接轉換http://web.csie.cgu.edu.tw/~000/xx/a.file問題: php 無法顯示、部分檔案顯示有問題
間接顯示將檔名傳入,利用讀檔方式顯示文字檔的特殊符號無法顯示,例如 <, >
112/04/19 Introduction to PHP 27
Output a file<?php
// get contents of a file into a string$filename = "/usr/local/something.txt";
$Ctype=mime_content_type ($filename);
header(" Content-type: $Ctype”);
$handle = fopen($filename, "rb");$contents = fread($handle, filesize($filename));fclose($handle);
if($Ctype==??)
echo string htmlspecialchars ($contents);
else
echo $contents;?>
112/04/19 Introduction to PHP 28