IV. YACIMIENTOS MINERALES
IV.2. Localidades de Rocas Dimensionables
File Handling
Keywords: file, directory, filename, extension, handle, stream, read,
write, permission, file wrapper, protocol, path, append, buffer, socket,
open, close, timeout.
Subjects:
9.1.
File Handle
9.2.
File Manipulation
9.3.
Directory
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
110
9.1.
File Handle:
9.1.1. Create file handle:
- The fopen() function is used to open files in PHP.
- The first parameter of this function contains the name of the file to be opened and the second
parameter specifies in which mode the file should be opened:
$fileHandle = fopen('counter.txt', 'w+');
9.1.2. File open mode:
Mode
Description
r
Open for reading only; place the file pointer at the beginning of the file.
r+
Open for reading and writing; place the file pointer at the beginning of the file.
w
Open for writing only; place the file pointer at the beginning of the file and
truncate the file to zero length. If the file does not exist, attempt to create it.
w+
Open for reading and writing; place the file pointer at the beginning of the file
and truncate the file to zero length. If the file does not exist, attempt to create
it.
a
Open for writing only; place the file pointer at the end of the file. If the file
does not exist, attempt to create it.
a+
Open for reading and writing; place the file pointer at the end of the file. If the
file does not exist, attempt to create it.
x
Create and open for writing only; place the file pointer at the beginning of the
file. If the file already exists, the fopen() call will fail by returning FALSE and
generating an error of level E_WARNING. If the file does not exist, attempt
to create it.
x+
Create and open for reading and writing; place the file pointer at the beginning
of the file. If the file already exists, the fopen() call will fail by returning
FALSE and generating an error of level E_WARNING. If the file does not
exist, attempt to create it.
9.1.3. Close file handle:
- In PHP it is not system critical to close all your files after using them because the server will
close all files after the PHP code finishes execution. However the programmer is still free
to make mistakes (i.e. editing a file that you accidentally forgot to close). You should close
all files after you have finished with them because it's a good programming practice
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
111
fclose($fileHandle);
- After a file has been closed down with fclose it is impossible to read, write or append to that
file unless it is once more opened up with the fopen function.
9.2.
File Manipulation:
Function
Handle Description
file_exists()
Checks whether a file or directory exists.
Ex:
if (file_exists('uploads/photo/a.jpg')) {
echo "The file exists"; }
is_file()
Tells whether the filename is a regular file.
Ex:
var_dump(is_file('uploads/photo/a.jpg'));
// Displays "bool(true)" if file exists and is a regular file.
is_readable()
Tells wheter the filename is readable.
Ex:
if (is_readable($filename)) { echo 'The file is readable'; }
is_writable()
Tells whether the filename is writable.
fread()
Yes
Reads data from file, with specified length.
Ex:
$contents = fread($handle, 2048);
fwrite()
Yes
Writes data to file. Ex:
$writeByte = fwrite($handle, $ somecontent);
rewind()
Yes
Rewind the position of a file pointer.
Ex:
$rewindStatus = rewind($handle);
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
112
Ex:
$contents = file_get_contents('config/text.txt');
chmod()
Changes file mode.
Ex:
chmod("/somedir/somefile", 0666);
copy()
Copies file.
Ex:
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n"; }
rename()
Renames a file or directory.
Ex:
rename("file.txt", "upload/user.txt");
filesize()
Gets file size.
Ex:
$size = filesize('upload/user.txt');
unlink()
Deletes a file.
Ex:
if(unlink('upload/user.txt'')) echo 'Delete successfully!';
filemtime()
Gets file modification time. Return the Unix Timestamp.
Ex:
$timestamp = filemtime('myfile.jpg');
pathinfo()
Returns information about a file path.
Ex:
$path_parts = pathinfo('/www/htdocs/index.html'); echo $path_parts['dirname'], "\n"; echo $path_parts['basename'], "\n"; echo $path_parts['extension'], "\n"; echo $path_parts['filename'], "\n"; // Displays /www/htdocs index.html htmlTHÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
113
index
9.3.
Directory:
9.3.1. Open directory handle:
$dh = opendir('uploads/photos/');
9.3.2. Useful directory functions:
Function
Handle
Description
readdir()
Yes
Reads entry from directory handle
is_dir()
Tells whether the filename is a directory
mkdir()
Makes directory.
Ex:
mkdir("/path/to/my/dir", 0700, true);
rmdir()
Removes directory. Note: directory must be empty
dirname()
Returns directory name component of path.
Ex:
$path = "/etc/passwd";
$file = dirname($path); // $file is set to "/etc"
9.3.3. Close directory handle:
closedir($dh);
9.3.4. Example:
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n"; }
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING
114
closedir($handle); }
9.4.
File & Directory Constants:
9.4.1.__FILE__
: get the current executed file. Example:echo dirname(__FILE__);
9.4.2.
DIRECTORY_SEPARATOR
: get the directory of the current system. '\' inWindows and '/' in Linux.
Chapter Exercise:
Write the website allows user register, login to system. After logging, users can upload photos,
view their uploaded photos, deleting their photos. Each user has a photo directory with his
name.
a.
When register, there is no 2 users with the same name.
b.
Password using md5() function to store
c.
Photos must be jpg,gif,png and size is not larger than 300KB
d.
User information stored in file with format:
"
rasmus,198792ks90s978jhsludssdf098s9adj,[email protected],AVATAR_URL
bob, lakljliuaklnkajsdoif928379skjad,[email protected],AVATAR_URL
…
"
THÀNH VIÊN HIỆP HỘI AN TOÀN THÔNG TIN VIỆT NAM- VNISA WWW.ATHENA.EDU.VN . TEL: 1900 54 54 56 -090 7879 477
VO DUY TUAN PHP Beginner & Intermediate
ATHENA INFORMATION TRAINING