You are on page 1of 21

Thao Tc Vi Tp Tin Trong PHP

PHP v MySQL

I. Thao tc vi tp tin

Thao tc c/ghi ni dung cc tp tin(file) c th s dng cho mt vi cng vic nh:


c v chnh sa ni dung ca cc tp tin cu hnh(config file) chnh sa cc gi tr nh chui kt ni, tn c s d liu, ti khon ng nhp Ti(upload) cc tp tin ln server.

Ngoi ra vic thao tc vi tp tin cn cho php ng dng c th lm vic vi cc tp tin XML thay cho c s d liu.

Trnh t lm vic vi tp tin theo cc bc sau:


Bc 1: M/to tp tin cn lm vic + Hm fopen() Bc 2: Thc hin c/ghi ni dung + Hm fgets(), fgetc(), fread(), fputs(), fwrite() Bc 3: ng tp tin + Hm fclose()

1. M tp tin

m mt file ta s dng hm fopen() theo c php: fopen(filename, mode);


Tham s filename mode M t Ch ra tn file, hoc URL mun m. Ch ra ch (kiu truy xut) m file (r, r+, w, w+, r, r+, a, a+, x, x+)

Hm fopen() tr v FALSE v li nu tht bi.C th thm k t @ trc tn hm nu khng mun hin th li

Cc ch m file
M file c, con tr c s bt u ti v tr u tin ca file. M file c/ghi, con tr c s bt u ti v tr u tin ca file. M file ghi. M v xa ni dung c ca file hoc to file mi nu file cha tn ti M file c/ghi. M v xa ni dung c ca file hoc to file mi nu file cha tn ti M file ghi. M v ghi thm ni dung vo cui ca file hoc to file mi nu file cha tn ti M file c/ghi. M v ghi thm ni dung vo cui ca file M file ghi. To mt file mi. Tr v FALSE v li nu file tn ti c/ghi. To mt file mi. Tr v FALSE v li nu file tn ti

mode M t r r+ w w+ a a+ x x+

V d:
<?

$file=fopen("welcome.txt","r") or exit("Unable to open file!"); $file=fopen(images/test.gif","r") or exit("Unable to open file!");

$file=fopen(http://www.example.com/test.htm ","r") or exit("Unable to openfile!");


?>

3.ng tp tin

ng file: sau khi thao tc xong vi cc tp tin th ta phi ng tp tin li gii phng lung. ng 1 file ang m ta s dng hm fclose() theo c php. fclose(tn file) V d
<?php fclose($file); ?>

2.Thao tc vi tp tin

c ni dung ca tp tin: c ni dung ca tp tin ta c th c theo tng dng hoc c tng k t. Vic c file c thc thi thng qua s h tr ca hm feof() hm ny c vai tr kim tra kt thc file. Gip ta duyt qua ton b ni dung ca tp tin m khng cn bit chnh xc di ca d liu.

Hm feof() tr v true nu n cui file. V d: <?php if(feof($file)) echo end of file; ?>

A. c ni dung file theo tng dng

c mt dng trong ni dung ca file ta s dng hm fgets(): V d: <?php $file = fopen("welcome.txt", "r") or exit("Unable to open file!"); //c qua tng dng cho ti khi kt thc. while(!feof($file)){ echo fgets($file). "<br />"; } fclose($file); ?>

B. c tng k t

C th s dng hm fgetc() c ln lt tng k t trong file: V d: <?php $file=fopen("welcome.txt","r") or exit("Unable to open file!"); while (!feof($file)) { echo fgetc($file); } fclose($file); ?> c v ghi ra tng k t ca file cho ti khi kt thc.

II. Ghi ni dung vo file

ghi ni dung vo file ang m ta s dng hm fwrite(). fwrite(file,string,length);


Tham s file string length M t Ch ra file ang c m ghi. Ch ra chui d liu s c ghi vo file Ch ra s byte ti a c ghi vo file

Hm fwrite() tr v FALSE nu tht bi, ngc li tr v s byte c ghi vo file C th dng hm ny ghi d liu nh phn, hoc d liu k t

II. Ghi ni dung vo file

V d: hm fwrite().
<?php $filename = welcome.txt"; $file = fopen($filename, w); $str= Hello\n"; fwrite($file, $str); $str= world\n"; echo fwrite($file, $str); fclose($file); ?>

Hm fwrite() cho php ta ghi bt c dng d liu no vo file. Trong trng hp s dng fwrite() vi tham s w v file tn ti th ni dung ca file s b xo trng v ghi d liu mi vo file. Trong trng hp cn ghi thm ni dung vo file ta c th s dng hm fwrite() vi tham s m file cho php ni thm.

V d Ghi thm ni dung vo file c <?php $filename = welcome.txt"; $file = fopen($filename, a) or die("can't open file"); $str= "New data 1\n"; fwrite($file, $str); $str= "New Data 2\n"; fwrite($file, $str); fclose($file); ?>

III. Upload File

Chc nng upload file cho php ta c th ti cc file ln th mc ca server. thc thi thao tc upload PHP h tr i tng ni ti $_FILE; Ta c bng cc la chn vi $_FILE: $_FILES["filefield_name"]["name"] - ly hoc thit lp tn file upload $_FILES["filefield_name"]["type"] - ch nh kiu file upload $_FILES["filefield_name"]["size"] dung lng ca file upload(tnh bng byte) $_FILES["filefield_name"]["error"] - li tr v trong qu trnh upload file. $_FILES["filefield_name"]["tmp_name"] : i tng lu tm thi trn server.

a. To Form upload

To mt form upload file v t thuc tnh cho form nh sau:


method l POST enctype l multipart/form-data

<form action="upload_file.php" method="post enctype="multipart/form-data"> Please choose a file: <input type="file" name=imagefile" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form>

Hin th cc thng tin v tin trnh upload


<?php if ($_FILES["file"]["error"] > 0) { echo "Error: " . $_FILES["imagefile"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["imagefile"]["name"] . "<br />"; echo "Type: " . $_FILES["imagefile"]["type"] . "<br />"; echo "Size: " . ($_FILES["imagefile"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES["imagefile"]["tmp_name"]; } ?>

B.Hiu chnh cc iu kin khi upload file


if ((($_FILES[imagefile"]["type"] == "image/gif") || ($_FILES[imagefile"]["type"] == "image/jpeg") || ($_FILES[imagefile"]["type"] == "image/pjpeg")) && ($_FILES[imagefile"]["size"] < 20000)) { if ($_FILES[imagefile"]["error"] > 0) { echo "Error: " . $_FILES["file"]["error"] . "<br />"; }

.GIF: image/gif .JPG: image/jpeg .PNG: image/png .WAV: audio/wav .MP3: audio/mpeg3 .DOC: application/msword .PDF: application/pdf

else { echo "Upload: " . $_FILES[imagefile"]["name"] . "<br />"; echo "Type: " . $_FILES[imagefile"]["type"] . "<br />"; echo "Size: " . ($_FILES[imagefile"]["size"] / 1024) . " Kb<br />"; echo "Stored in: " . $_FILES[imagefile"]["tmp_name"]; } } else { echo "Invalid file"; } - Trong on code trn ta khng ch kiu file upload v dung lng ti a c th upload.

C. Lu file vo th mc

Lc ny file ang c lu trong i tng tm thi, ta phi lu ra file trn th mc ca server. if (file_exists("upload/" . $_FILES[imagefile"]["name"])) { echo $_FILES[imagefile"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES[imagefile"]["tmp_name"], "upload/" . $_FILES[imagefile"]["name"]); echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; } }

You might also like