Tải bản đầy đủ (.pdf) (14 trang)

Bài giảng PHP (Hypertext Preprocessing) - Chương 4: Làm việc với file

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (327.03 KB, 14 trang )

IV. Làm việc với file
IV.1. Sử dụng include()
IV.2. Sử dụng include_once() và include_path
cho các dự án lớn
IV.3. Kiểm tra file
IV.4. Đọc, ghi file
IV.5. Làm việc với thư mục


IV.1. Sử dụng include()






Lệnh include() cho phép bạn phối hợp giữa các file trong một
PHP project, ko giống như cú pháp #include của ngôn ngữ C,
lệnh này không chèn mã lệnh vào file mà thực thi file php giống
như cú pháp gọi hàm
include() sử dụng để chia sẻ các hàm dùng chung, các đoạn
mã chung trong một project có nhiều file
Nếu không tìm thấy file, include() thông báo warning nhưng
không dừng chương trình
PHP cung cấp 1 lệnh tương tự include() là require(), lệnh này
có sự khác biệt là sẽ dừng ngay chương trình khi không tìm
thấy file


IV.1. Sử dụng include() (2)





<!--File1.php->
echo "This is from file 1\n";
?>
<!--File2.php ->
echo "This is from file 2\n";
include("file1.php");
include("file3.php");
echo "This is from file 2\n";
?>


IV.1. Sử dụng include() (3)





Vì include() thực hiện lời gọi đến file php, do đó bạn có thể trả
về giá trị từ file PHP được include
<!--File11.php-->
return 4 + 4;
?>
<!--File12.php-->

echo "This is from file 12\n";
$retVal = include("file11.php");
echo "Return value from file 11: \$retVal=
$retVal\n";
echo "This is from file 12\n";
?>


IV.1. Sử dụng include() (4)






Bạn có thể đặt lệnh include bên trong 1 cấu trúc điều kiện hoặc
cấu trúc lặp, khi đó tùy theo điều kiện của cấu trúc mà include()
có được thực hiện hay không, 1 hay nhiều lần
<!--File22.php-->
if ($j==null) $j=10; else $j++;
return $i+$j;
?>
<!--File21.php-->
for ($i=0; $i<10; $i++) {
echo include("File22.php"), "\n";
}
?>



IV.2. Sử dụng include_once() và include_path
cho các dự án lớn







include_once() giống như include(), tuy nhiên có điểm khác biệt là
chỉ include 1 lần, lần sau nếu gặp lại file này thì ko include nữa
include_once() sử dụng cho các hàm thư viện dùng chung để
tránh trường hợp khi nhiều file cùng include đến 1 file, sau đó các
file này lại include lẫn nhau (so sánh với #ifdefine trong file header
của ngôn ngữ C)
include_once() phân biệt chữ hoa, chữ thường
include_once("a.php"); // this will include a.php
include_once("A.php"); // this will include a.php
again on Windows!
?>


IV.2. Sử dụng include_once() và include_path
cho các dự án lớn (2)



Trong quá trình làm việc, bạn hẳn có nhiều hàm được

dùng chung giữa nhiều file trong 1 project, giữa nhiều
project. Để sử dụng các hàm này, bạn có thể áp dụng
các cách sau:





include file theo đường dẫn tuyệt đối: Cách này dở vì khi cài
đặt trên máy khác sẽ không tìm thấy file được include
include file theo đường dẫn tương đối: Cách này tốt hơn,
nhưng mỗi khi đổi vị trí của file được include thì phải sửa lại tại
tất cả các file thực hiện lời gọi include
Cách tốt nhất là sử dụng include_path (thiết lập trong file
PHP.INI) đối với những file thư viện dùng chung được sử
dụng nhiều (giống như đối với ngôn ngữ C)


IV.2. Sử dụng include_once() và
include_path cho các dự án lớn (3)
Để thiết lập include_path, bạn có những cách sau:
- thay đổi include_path trong PHP.INI
- dùng lệnh set_include_path()
var_dump(get_include_path());
set_include_path('/inc'); // Works as of PHP 4.3.0
var_dump(get_include_path());
restore_include_path();
var_dump(get_include_path());
?>


- dùng lệnh ini_set()
var_dump(ini_get("include_path"));
ini_set("include_path", "/inc"); // Works in all PHP
versions
var_dump(ini_get("include_path"));
ini_restore("include_path");
var_dump(ini_get("include_path"));
?>


IV.3. Kiểm tra file

file_exist(), is_file(), is_dir(), is_readable(), is_writeable(), is_executable(), filesize(),
fileatime()
function outputFileTestInfo( $file ) {
if ( ! file_exists( $file ) ) {
print "$file does not exist
";
return;
}
print "$file is ".(is_file( $file )?"":"not ")."a file
\n";
print "$file is ".(is_dir( $file )?"":"not ")."a
directory
\n";
print "$file is ".(is_readable( $file )?"":"not
")."readable
\n";
print "$file is ".(is_writable( $file )?"":"not
")."writable
\n";
print "$file is ".( filesize($file))." bytes
\n";

print "$file was accessed on "
.date( "D d M Y g:i A", fileatime( $file ) )."
\n";
print "$file was modified on "
.date( "D d M Y g:i A", filemtime( $file ) )."
\n";
print "$file was changed on"
.date( "D d M Y g:i A", filectime( $file ) )."
\n";
}
outputFileTestInfo("c:\\windows\\system32\\cmd.exe");
?>


IV.4. Đọc, ghi file


fopen($filename, $mode);
fwrite($handle, $string);
fread($handle, $length);
fgets($handle);

sprintf($format);
fscanf($handle, $format);
fseek($handle, $offset);
fclose($handle);
file_get_contents($filename);


IV.4. Đọc, ghi file (2)
$var1 = 10;
$var2 = "This is a String";

$var3 = true;
$f = fopen("test.txt", "wt");
fwrite($f, "$var1 $var2 $var3\n");
fwrite($f, "$var1\n$var2\n$var3\n");
fclose($f);
echo "Read line by line......\n";
$f = fopen("test.txt", "rt");
echo "Read all file by fread......\n";
while (!feof($f)) {
$line = fgets($f);
$f = fopen("test.txt", "rb");
echo "$line";
$myfile = fread($f, filesize("test.txt"));
}
echo $myfile;
fclose($f);
fclose($f);
echo "Read all file......\n";
$myfile = file_get_contents("test.txt");
echo($myfile);
?>


IV.4. Đọc, ghi file (3)


$var1=10;
$var2=100;
$var3=100.3434;

$var4="Test string";
$f=fopen("test.txt", "wt");
fwrite($f, sprintf("%d %10.3f %10.3lf\n\r", $var1, $var2, $var3));
fwrite($f, sprintf("%s", $var4));
fclose($f);

$f=fopen("test.txt", "rt");
if (list($v1, $v2, $v3, $v4) = fscanf($f, "%d %f %lf\n\r%s")) {
var_dump($v1);
var_dump($v2);
var_dump($v3);
var_dump($v4);
}
$v4 = fgets($f);
var_dump($v4);
fclose($f);
?>


IV.4. Đọc, ghi file (4)


class AClass {
};
$ob1 =& new AClass();
$ob1->a = 10;
$ob1->b = 100.023;
$ob1->c = "Test String";
var_dump($ob1);


$f = fopen("test.txt", "wb");
fwrite($f, serialize($ob1));
fclose($f);
$f = fopen("test.txt", "rb");
$ob2 = unserialize(fgets($f));
fclose($f);
var_dump($ob2);
?>


IV.5. Làm việc với thư mục



mkdir(), rmdir(), opendir(), readdir(), closedir()
$dir=opendir("c:\\windows");
while ($file=readdir($dir)) {
echo "$file\n";
}
closedir($dir);
?>



×