Skip to main content

Posts

Showing posts with the label file handling

Basic PHP File Handling — Create, Open, Read, Write, Append, Close, and Delete

I don't do a great deal of file handling in my PHP code -- most of my customers don't have a need for it or there's no room for file creation in the already tight budget. On the rare occasion that I do need to manipulate files, I keep the following tip sheet. Create a File $my_file = 'file.txt' ; $handle = fopen ( $my_file , 'w' ) or die ( 'Cannot open file: ' . $my_file ) ; //implicitly creates file Open a File $my_file = 'file.txt' ; $handle = fopen ( $my_file , 'w' ) or die ( 'Cannot open file: ' . $my_file ) ; //open file for writing ('w','r','a')... Read a File $my_file = 'file.txt' ; $handle = fopen ( $my_file , 'r' ) ; $data = fread ( $handle , filesize ( $my_file ) ) ; Write to a File $my_file = 'file.txt' ; $handle = fopen ( $my_file , 'w' ) or die ( 'Cannot open file: ' . $my_file ) ; $data = 'Th