Professional Documents
Culture Documents
PHP v MySQL
I. Thao tc vi tp tin
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.
1. M tp tin
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:
<?
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; ?>
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.
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
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); ?>
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
<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>
.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"]; } }