ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
D
H
L
4/25/2015
CH??NG03
Gi?ng vi¨ºn: H? Di¨ºn L?i
1Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015 2Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
1. Gi?i thi?u
3
? D¨´ng ?? g?i n?i dung c?a t?p tin ??a v¨¤o trang
web.
? Khi ?o?n m? trong file_name b? l?i s? xu?t hi?n
c?nh b¨¢o
? ?? l¨¤m gi?m l?p l?i c?a code.
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
2. M?t s? h¨¤m
4
? H¨¤m include ("pathfile");
? H¨¤m include_once("pathfile");
? H¨¤m require("pathfile");
? H¨¤m require_once("pathfile");
? Ch¨² ?:
H¨¤m include_once(), require_once() d¨´ng ?? g?i n?i
dung c?a t?p tin ??a v¨¤o trang web ch? th?c hi?n duy
nh?t m?t l?n.
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015 5Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
1. Ch? ?? m? t?p tin
6
? M? file ? ch? ?? read only, write only hay c? read
v¨¤ write.
? M? file ?? t?n t?i, ghi ?¨¨ hay ghi th¨ºm(??u, cu?i).
? Khi mu?n ghi file h? th?ng th¨¬ ch¨²ng ta c?n ch?
??nh ch? ?? ghi file l¨¤ nh? ph?n ho?c text.
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015 7
? Ph?n lo?i ch? ?? m? t?p tin
Ch? ?? M? t?
r Ch? ??c file, b?t ??u ??c ??u file
r+ ??c v¨¤ ghi file: B?t ??u t? ??u file
w
Ch? ghi file. M? v¨¤ x¨®a to¨¤n b? n?i dung c?a
file ?? c¨® ho?c t?o ra m?t file m?i n?u file ?¨®
kh?ng t?n t?i, sau ?¨® ghi n?i dung v¨¤o file.
w+
??c v¨¤ ghi. M? v¨¤ x¨®a to¨¤n b? n?i dung c?a
file ?? c¨® ho?c t?o ra m?t file m?i n?u file ?¨®
kh?ng t?n t?i, sau ?¨® ghi n?i dung v¨¤o file
1. Ch? ?? m? t?p tin¡­
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
2. M?t s? h¨¤m x? t?p tin
8
? H¨¤m fopen(<t¨ºn_t?p_tin>,<ch? ?? m?>): h¨¤m m? t?p tin.
? H¨¤m feof($f): ki?m tra k?t th¨²c t?p tin
? H¨¤m fgets($f): ??c t?ng d¨°ng trong t?p tin
? H¨¤m fgetc($f): ??c t?ng k? t? trong t?p tin
? H¨¤m readfile($pathfile): ??c to¨¤n b? nd t?p tin
? H¨¤m fclose($f): ?¨®ng t?p tin
? H¨¤m file_exists($pathfile): ki?m tra xem c¨® t?n t¨¤i t?p tin
kh?ng?
? H¨¤m filesize($pathfile): tr? v? k¨ªch th??c t?p tin
? H¨¤m unlink($pathfile): x¨®a t?p tin
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
3. Ghi n?i dung l¨ºn t?p tin
9
? H¨¤m fwrite($f, $nd): ghi n?i dung v¨¤o t?p
? ??nh d?ng t?p tin
? Tr??c khi ghi chu?i v¨¤o file, ch¨²ng ta c?n ph?i
??nh d?ng l?i chu?i ?¨® theo nhu c?u xu?t d? li?u
tr? l?i khi ??c file.
? M?t s? ??nh d?ng ???c quy ??nh s?n nh? sau:
+ t : nh?y tab
+ n : xu?ng d¨°ng
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
4. V¨ª d?
10
<?php
if(file_exists("hinh/test.txt")){
$f=fopen("hinh/test.txt","r+");
while(!feof($f))
{
$row=fgets($f);
echo $row."<br>";
}
}
else
{
echo "T?p tin kh?ng t?n t?i";
}
?>
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015 11Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
1. M?t s? h¨¤m l¨¤m vi?c v?i th? m?c
12
? H¨¤m mkdir(): t?o th? m?c
? H¨¤m is_dir(): ki?m tra t?n t?i th? m?c
? H¨¤m opendir(): m? th? m?c
? H¨¤m closedir(): ?¨®ng th? m?c
? H¨¤m readdir(): ??c n?i dung th? m?c
? H¨¤m rmdir(): X¨®a th? m?c
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
2. V¨ª d?
13
<?php
if(is_dir("hinh"))
{
$dir=opendir("hinh");
while($f=readdir($dir))
{
if(file_exists("hinh/".$f))
{
echo "<img src=/slideshow/chng-3-to-web-ng/47397595/&
}
}
}
?>
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015 14Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
1. Gi?i thi?u
15
? Trong h?u h?t c¨¢c ?ng d?ng web, th?ng th??ng
ng??i d¨´ng c¨® th? upload file l¨ºn server.
? ??i v?i ?ng d?ng web x?y d?ng b?ng ng?n ng?
PHP ch¨²ng ta c?ng c¨® th? x?y d?ng cho ng??i
d¨´ng upload file l¨ºn server.
? Khi upload l¨ºn server, n?u t?p tin ?? t?n t?i th¨¬ t?p
tin m?i s? ghi ?¨¨ l¨ºn.
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
2. ??c ?i?m c?a form uploda
16
<form action ="xuly_upload.php" method ="POST"
enctype ="multipart/form-data">
¡­
</form>
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
3. T?p tin xuly_upload.php
17
? L?y th?ng tin t? form upload
? $tenfile= $_FILES["tendk"]["name"];
? $kieufile=$_FILES["tendk"]["type"];
? $sizefile= $_FILES[" tendk "]["size"];
? $tmp_name=$_FILES[¡°tendk¡±]["tmp_name"];
? $errorfile=$_FILES[¡°tendk¡±]["error"];
? H¨¤m upload file
move_uploaded_file($tmp_name , "pathdir/".$tenfile);
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015 18Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
1. Khai b¨¢o Cookies
19
? D¨´ng h¨¤m setcookie() ?? khai b¨¢o m?t bi?n cookies.
Ch¨² ?: H¨¤m n¨¤y ??t tr¨ºn th? <html>
? C¨² ph¨¢p: setcookie(name, value, expire, path);
Trong ?¨®:
+ Name: t¨ºn bi?n cookie
+ value : gi¨¢ tr?
+ expire: th?i gian(??n v? t¨ªnh l¨¤ gi?y)
+ path
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
V¨ª d?
20
V¨ª d?:
<?php
setcookie("name", $value, time()+3600,'/');
?>
<html>
¡­¡­¡­¡­.
¡­¡­¡­¡­.
¡­¡­¡­¡­
</html>
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
2. L?y gi¨¢ tr? bi?n Cookies
21
? Khi ng??i d¨´ng mu?n s? d?ng bi?n cookie ?? ??ng
k? ch¨²ng ta d¨´ng bi?n $_COOKIE ?? ??c gi¨¢ tr? bi?n
cookies.
? C¨² ph¨¢p:
$bien = $_COOKIE[¡°t¨ºn bi?n cookie¡±];
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
3. H?y bi?n Cookies
22
? ?? h?y cookies ta d¨´ng h¨¤m setcookie() v?i gi¨¢ tr?
b?ng "" v¨¤ th?i gian =- th?i gian gi?i h?n.
? C¨² ph¨¢p: setcookie(name, "" , time() ¨C th?i gian gi?i
h?n);
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
4. Kh?i ??ng Sessions
23
? ?? l?u tr? th?ng tin ng??i d¨´ng v¨¤o session, ch¨²ng ta
ph?i kh?i ??ng session.
? Ch¨² ?: H¨¤m kh?i ??ng session ph?i ??t ph¨ªa tr¨ºn th?
<html>
? C¨² ph¨¢p:
session_start();
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
5. Kh?i t?o Sessions
24
? ?? kh?i t?o ta d¨´ng bi?n $_SESSION nh?n v¨¤ l?u tr?
gi¨¢ tr? c?a bi?n session.
? C¨² ph¨¢p:
$_SESSION["ten_bien_session"] ="gi¨¢ tri";
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
6. L?y gi¨¢ tr? t? Sessions
25
? ?? l?y gi¨¢ tr? c?a bi?n session ta c?ng d¨´ng bi?n
$_SESSION ?? ??c gi¨¢ tr? bi?n session.
? C¨² ph¨¢p:
$gia_tri=$_SESSION["ten_bien_session"];
Gi?ng vi¨ºn: H? Di¨ºn L?i
D
H
L
4/25/2015
7. H?y bi?n Sessions
26
? H?y to¨¤n b? c¨¢c bi?n session
?? h?y t?t c? c¨¢c bi?n ta d¨´ng h¨¤m session_destroy();
? H?y m?t bi?n session
?? h?y 1 bi?n ta d¨´ng h¨¤m unset($_SESSION["t¨ºn
bi?n session"]);
Gi?ng vi¨ºn: H? Di¨ºn L?i

More Related Content

Ch??ng 3: T?o web ??ng

  • 1. D H L 4/25/2015 CH??NG03 Gi?ng vi¨ºn: H? Di¨ºn L?i 1Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 3. D H L 4/25/2015 1. Gi?i thi?u 3 ? D¨´ng ?? g?i n?i dung c?a t?p tin ??a v¨¤o trang web. ? Khi ?o?n m? trong file_name b? l?i s? xu?t hi?n c?nh b¨¢o ? ?? l¨¤m gi?m l?p l?i c?a code. Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 4. D H L 4/25/2015 2. M?t s? h¨¤m 4 ? H¨¤m include ("pathfile"); ? H¨¤m include_once("pathfile"); ? H¨¤m require("pathfile"); ? H¨¤m require_once("pathfile"); ? Ch¨² ?: H¨¤m include_once(), require_once() d¨´ng ?? g?i n?i dung c?a t?p tin ??a v¨¤o trang web ch? th?c hi?n duy nh?t m?t l?n. Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 6. D H L 4/25/2015 1. Ch? ?? m? t?p tin 6 ? M? file ? ch? ?? read only, write only hay c? read v¨¤ write. ? M? file ?? t?n t?i, ghi ?¨¨ hay ghi th¨ºm(??u, cu?i). ? Khi mu?n ghi file h? th?ng th¨¬ ch¨²ng ta c?n ch? ??nh ch? ?? ghi file l¨¤ nh? ph?n ho?c text. Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 7. D H L 4/25/2015 7 ? Ph?n lo?i ch? ?? m? t?p tin Ch? ?? M? t? r Ch? ??c file, b?t ??u ??c ??u file r+ ??c v¨¤ ghi file: B?t ??u t? ??u file w Ch? ghi file. M? v¨¤ x¨®a to¨¤n b? n?i dung c?a file ?? c¨® ho?c t?o ra m?t file m?i n?u file ?¨® kh?ng t?n t?i, sau ?¨® ghi n?i dung v¨¤o file. w+ ??c v¨¤ ghi. M? v¨¤ x¨®a to¨¤n b? n?i dung c?a file ?? c¨® ho?c t?o ra m?t file m?i n?u file ?¨® kh?ng t?n t?i, sau ?¨® ghi n?i dung v¨¤o file 1. Ch? ?? m? t?p tin¡­ Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 8. D H L 4/25/2015 2. M?t s? h¨¤m x? t?p tin 8 ? H¨¤m fopen(<t¨ºn_t?p_tin>,<ch? ?? m?>): h¨¤m m? t?p tin. ? H¨¤m feof($f): ki?m tra k?t th¨²c t?p tin ? H¨¤m fgets($f): ??c t?ng d¨°ng trong t?p tin ? H¨¤m fgetc($f): ??c t?ng k? t? trong t?p tin ? H¨¤m readfile($pathfile): ??c to¨¤n b? nd t?p tin ? H¨¤m fclose($f): ?¨®ng t?p tin ? H¨¤m file_exists($pathfile): ki?m tra xem c¨® t?n t¨¤i t?p tin kh?ng? ? H¨¤m filesize($pathfile): tr? v? k¨ªch th??c t?p tin ? H¨¤m unlink($pathfile): x¨®a t?p tin Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 9. D H L 4/25/2015 3. Ghi n?i dung l¨ºn t?p tin 9 ? H¨¤m fwrite($f, $nd): ghi n?i dung v¨¤o t?p ? ??nh d?ng t?p tin ? Tr??c khi ghi chu?i v¨¤o file, ch¨²ng ta c?n ph?i ??nh d?ng l?i chu?i ?¨® theo nhu c?u xu?t d? li?u tr? l?i khi ??c file. ? M?t s? ??nh d?ng ???c quy ??nh s?n nh? sau: + t : nh?y tab + n : xu?ng d¨°ng Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 12. D H L 4/25/2015 1. M?t s? h¨¤m l¨¤m vi?c v?i th? m?c 12 ? H¨¤m mkdir(): t?o th? m?c ? H¨¤m is_dir(): ki?m tra t?n t?i th? m?c ? H¨¤m opendir(): m? th? m?c ? H¨¤m closedir(): ?¨®ng th? m?c ? H¨¤m readdir(): ??c n?i dung th? m?c ? H¨¤m rmdir(): X¨®a th? m?c Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 15. D H L 4/25/2015 1. Gi?i thi?u 15 ? Trong h?u h?t c¨¢c ?ng d?ng web, th?ng th??ng ng??i d¨´ng c¨® th? upload file l¨ºn server. ? ??i v?i ?ng d?ng web x?y d?ng b?ng ng?n ng? PHP ch¨²ng ta c?ng c¨® th? x?y d?ng cho ng??i d¨´ng upload file l¨ºn server. ? Khi upload l¨ºn server, n?u t?p tin ?? t?n t?i th¨¬ t?p tin m?i s? ghi ?¨¨ l¨ºn. Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 16. D H L 4/25/2015 2. ??c ?i?m c?a form uploda 16 <form action ="xuly_upload.php" method ="POST" enctype ="multipart/form-data"> ¡­ </form> Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 17. D H L 4/25/2015 3. T?p tin xuly_upload.php 17 ? L?y th?ng tin t? form upload ? $tenfile= $_FILES["tendk"]["name"]; ? $kieufile=$_FILES["tendk"]["type"]; ? $sizefile= $_FILES[" tendk "]["size"]; ? $tmp_name=$_FILES[¡°tendk¡±]["tmp_name"]; ? $errorfile=$_FILES[¡°tendk¡±]["error"]; ? H¨¤m upload file move_uploaded_file($tmp_name , "pathdir/".$tenfile); Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 19. D H L 4/25/2015 1. Khai b¨¢o Cookies 19 ? D¨´ng h¨¤m setcookie() ?? khai b¨¢o m?t bi?n cookies. Ch¨² ?: H¨¤m n¨¤y ??t tr¨ºn th? <html> ? C¨² ph¨¢p: setcookie(name, value, expire, path); Trong ?¨®: + Name: t¨ºn bi?n cookie + value : gi¨¢ tr? + expire: th?i gian(??n v? t¨ªnh l¨¤ gi?y) + path Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 20. D H L 4/25/2015 V¨ª d? 20 V¨ª d?: <?php setcookie("name", $value, time()+3600,'/'); ?> <html> ¡­¡­¡­¡­. ¡­¡­¡­¡­. ¡­¡­¡­¡­ </html> Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 21. D H L 4/25/2015 2. L?y gi¨¢ tr? bi?n Cookies 21 ? Khi ng??i d¨´ng mu?n s? d?ng bi?n cookie ?? ??ng k? ch¨²ng ta d¨´ng bi?n $_COOKIE ?? ??c gi¨¢ tr? bi?n cookies. ? C¨² ph¨¢p: $bien = $_COOKIE[¡°t¨ºn bi?n cookie¡±]; Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 22. D H L 4/25/2015 3. H?y bi?n Cookies 22 ? ?? h?y cookies ta d¨´ng h¨¤m setcookie() v?i gi¨¢ tr? b?ng "" v¨¤ th?i gian =- th?i gian gi?i h?n. ? C¨² ph¨¢p: setcookie(name, "" , time() ¨C th?i gian gi?i h?n); Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 23. D H L 4/25/2015 4. Kh?i ??ng Sessions 23 ? ?? l?u tr? th?ng tin ng??i d¨´ng v¨¤o session, ch¨²ng ta ph?i kh?i ??ng session. ? Ch¨² ?: H¨¤m kh?i ??ng session ph?i ??t ph¨ªa tr¨ºn th? <html> ? C¨² ph¨¢p: session_start(); Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 24. D H L 4/25/2015 5. Kh?i t?o Sessions 24 ? ?? kh?i t?o ta d¨´ng bi?n $_SESSION nh?n v¨¤ l?u tr? gi¨¢ tr? c?a bi?n session. ? C¨² ph¨¢p: $_SESSION["ten_bien_session"] ="gi¨¢ tri"; Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 25. D H L 4/25/2015 6. L?y gi¨¢ tr? t? Sessions 25 ? ?? l?y gi¨¢ tr? c?a bi?n session ta c?ng d¨´ng bi?n $_SESSION ?? ??c gi¨¢ tr? bi?n session. ? C¨² ph¨¢p: $gia_tri=$_SESSION["ten_bien_session"]; Gi?ng vi¨ºn: H? Di¨ºn L?i
  • 26. D H L 4/25/2015 7. H?y bi?n Sessions 26 ? H?y to¨¤n b? c¨¢c bi?n session ?? h?y t?t c? c¨¢c bi?n ta d¨´ng h¨¤m session_destroy(); ? H?y m?t bi?n session ?? h?y 1 bi?n ta d¨´ng h¨¤m unset($_SESSION["t¨ºn bi?n session"]); Gi?ng vi¨ºn: H? Di¨ºn L?i