Streams and Network Programming - File System Functions
The PHP5 Zend Certification contains questions about file system functions, they do this so much that their free on-line sample test first question is:
How can precisely one byte be read from a file, pointed by $fp?
A) fseek($fp, 1)
B) fgets($fp, 1)
C) fgetss($fp, 1)
D) fgetc($fp)
E) All of the above
…. The answer? … If you read my last blogpost you’d go with fgets because “Gets line from file pointer” but that’s incorrect the correct answer is fgetc($fp);
Anyway, enough proving that you need to know all of this and lets get into it:
PHP File Functions (some)
PHP Functions
fopen($filename, $mode);
fopen("counter.txt", "a+");
The code above will open counter.txt file so we can do what we want. Read its data and even write data to the end of the file. The $mode where covered in the last blog post.
filesize($filename);
This function gets the size of the givenfile in bytes, or FALSE.
filesize() may return unexpected results for files which are larger than 2GB. For files between 2GB and 4GB in size this can usually be overcome by using sprintf(”%u”, filesize($file)).
fgets($filepointer);
$fp is the variable holding the contents of the file, must be provided by fopen() or fstockopen().
The file stops reading when the value of length-1 is reached, a new line or at End Of File (EOF), if no length is given it goes on until EOL.
ftruncate($fp, $length);
This function truncates a file to a given length, it takes the variable holding the contents of the file (filepointer). It must be writeable (correct mode). The size variable tells us how much data we want, if the size is larger its filled with null bytes, if the size is smaller the extra data is lost.
fwrite($fp, $contents, $length);
fwrite($fp, "I will be saved to file", "23");
The function above saves files with the contents you want. The file perimeter needs to know the file pointer resource that was created using fopen(); . The second perimeter is what the contents of the file will be, the third perimeter is optional but is the length of the string we want to be saved. It will stop saving after the bytes specified in the length. If you use multiple fwrite on a filepointer, they will append each other.
feof($fp);
while(!feof($fp)){
echo fgets($file). "<br />";
}
feof Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE. The code above will output each line until the EOF is reached, at which point the loop will end.
fread($fp, $length);
The fread function reads the contents of the loaded file from the file pointer. Reading stops as soon as the length is met, it reaches EOF, file is unreachable or 8192 bytes have been read.
This will obviously return the contents of the file, or FALSE if nothing s read.
fseek($fp, $offset, $whence );
fseek($fp, "10", SEEK_SET)
fseek uses the file pointer and gets contents from the file. The second perimeter is the length and depending on the third perimeter is how it will be gathered.
* SEEK_SET - Set position equal to offset bytes.
* SEEK_CUR - Set position to current location plus offset .
* SEEK_END - Set position to end-of-file plus offset .
readfile($file);
This does exactly what it says on the tin, it reads the contents of a file and prints it to the script. readfile();.
file_get_contents($file,$incpath,$context,$offset,$maxlen);
file_get_contents("counter.txt");
Once again it does what it says, it gets the entire contents of a file and returns it to a string. Personaly I enter NULL in the second and third perimeter as ive had no use of these yet. The forth perimeter lets you ahve an offset what byte do you want to start reading the file and maxlen is the maximum length of the data you want to read.
file_put_contents($file, $contents, FILE_APPEND);
file_put_contents("counter.txt", "30 visitors today", FILE_APPEND);
Write a string to a file using this function, the first perimeter is the filename, the second is the data we want to write to the file. The third perimiter is known as flags, leave this empty or use FILE_APPEND if you want the new data, to be after the data that used to be in their. Their are some more.
chdir($directory);
echo getcwd() . "\n"; //prints /home/is-hacked/
echo file_exists('php.gif') //Prints FALSE
chdir('public_html'); //changes directory
echo getcwd() . "\n"; //prints /home/is-hacked/public_html/
file_exists('php.gif') //Prints TRUE
As you can see chdir changes the directory that is being used in the php script. If (most likely) safemode is on then chdir checks if you own the folder your moving to. You can then use other functions after the directory change.
getcwd();
echo getcwd(); //Display directory
This function returns the current working directory that the script is being run in.
mkdir($pathname, $mode, $recursive);
mkdir("newdir", 0666, true); //Make directory
The function allows you to create a new directory in the directory that you are currently in. The first perimeter is the directory name, the seccond is the mode you can read more about modes in chmod
The function returns true or false, if it works or not.
rmdir($pathname);
rmdir("newdir"); //removes directory
This function DELETES the directory and files inside, it returns TRUE on deletion or false on failure
unlink($filename);
unlink("newfile.txt"); //removes file
You can delete files also using the unlink function, just enter the files name and have fun.
chmod($file, $mode);
chmod("/test/file.txt", 0666); //chmod file
The chmod function changes the files permissions, this can be done so you can make a file writeable, write to the file, then chmod it back to readable only. This is useful for security and such, When safe mode is enabled, PHP checks whether the files or directories you are about to operate on have the same UID (owner) as the script that is being executed. In addition, you cannot set the SUID, SGID and sticky bits.
is_dir();
var_dump(is_dir('a_file.txt')); //Returns FALSE (is file)
var_dump(is_dir('public_html'));//Returns TRUE is active directory
This tells us weather a file name is a directory, as expected it returns true if the directory exists and false if it does not.
is_file($filename);
is_file("counter.txt"); //checks if file exists
Tells whether the filename is a regular file, returns TRUE if it is a file and FALSE if it is not.
is_readable($filename); and is_writable($filename);
is_readable("counter.txt"); //checks if path exists and is readable
is_writable("counter.txt"); //checks if path file exists and is writeable
It does what it says if the file is readable it returns true or false. If the file is writable it returns true of false.
is_uploaded_file($filename);
is_uploaded_file("counter.txt"); //checks if path is an uploaded file
Ths returns TRUE if the file named was uploaded by a HTTP Post, this is useful for security. It is most likely used as such:
is_uploaded_file($_FILES['userfile']['tmp_name']);