file handling Flashcards
fopen(“testfile.txt”, ‘w’)
Opens a file called testfile, the second parameter ‘w’ tells the function to open the file for writing. The function creates the file if it doesn’t already exist. Be careful when playing around with these functions: if the file already exists, the w mode parameter causes the open call to delete the old contents (even if you don’t write anything new)
fread
Reads from file
fread($fh, 3); // Requests the first 3 characters of file assigned to $fh
The read function is commonly used with binary data. But if you use it on text data that spans more than one line, remember to count newline characters.
fwrite
Writes to file
fclose
Closes file
copy(‘testfile.txt’, ‘testfile2.txt’)
Copies testfile.txt to testfile2.txt
moving a file
To move a file, rename it with the rename function:
rename(‘testfile2.txt’, ‘testfile2.new’);
deleting a file
To delete a file, use the unlink function:
unlink(‘testfile2.new’);
file_exists
Returns TRUE if the file exists or FALSE if it doesn’t:
file_exists(‘testfile2.new’);
fseek
Moves the file pointer