Php File Operations
Thanks to file system functions in PHP, we will be able to create new files, change or delete existing files. We will also touch upon many important functions that will be required for file uploading and downloading under this heading. Thanks to these functions, you can create and delete millions of files with a single PHP file, creating the feeling of having done a great job and satisfying your ego, or you can create useful applications such as nice file management, archive management, file manager. Now we will start by creating a file and learn how to open, read, write, close and delete it. Writing and deleting permissions to files Before we proceed with operations such as creating or deleting files, I would like to talk about the permissions and permissions we need to do all these.
In PHP, we cannot interfere with a directory or a file without any hesitation. First, we need to have some privileges or write permission to the files. If we are working locally on our own computer, permissions are granted by default. But a real site host has restrictions for security. We need to set the modification mode called CHMOD to the file or directory we will work on. Without going into too much detail about CHMOD, I just want to tell you what we will use. We can give write permission to a file or directory via FTP or the host's management panel. To do this from FTP, all we need to do is right-click on the required file or directory and write "777" in the permission values.
We give all write, read and delete permissions to the file or directory where we set the CHMOD value to 777. And at this point we focus on the security issue. As you know, when you give all these permissions, you have to be cautious and act paranoid. We need to be careful about what we do and what we do when coding the system. Creating a file with PHP We will introduce a function called touch() to create, modify and run a non-existent file. This function, which means touch in Turkish, is used to make the first touch on files in PHP or to change the last modification date if a file exists. It is simple to use, works with one parameter. We enter the file name to be created in the parameter.
touch('hello.txt');
When the above code runs, hello in the directory where the source PHP file is located. It creates an empty file named .txt. Opening and closing files with PHP Before reading, changing and deleting the contents of the files, we need to open them in PHP. This opening process should not be understood as reading. It means opening the file in order to provide initial access and connection before starting to perform the necessary operations. We will do this access with the fopen() function. This function works with two parameters. In the first parameter, we enter the file we will access, and in the second parameter, we enter the mode that will indicate why we are accessing the file.There are 8 modes that will indicate why we will access the files. Let's write these modes with their explanations in the table:
r Opens the file for reading.
(read)r+ Opens the file for both reading and writing.
(read)w Opens the file for writing. Deletes existing content
(write)w+ Opens the file for both writing and reading. It deletes the existing content and writes it from scratch.
(write)a Opens the file for writing. It does not delete existing content but adds it to the end.
(append)a+ Opens the file for both writing and reading. It does not delete the existing content but appends it to the end.
(append)x Creates the file and opens it for writing. Returns FALSE if the file already exists.
x+ It creates the file and opens it for writing and reading. If the file already exists, it returns FALSE.
Now, with the above file modes, we can treat the file we want. After performing the necessary operations with the files, we should not forget to close them. If we do not close it, we may encounter problems accessing the next file. We will use the fclose() function to close the files. This function works with a parameter and closes the file we opened. We write the variable of the file we opened with fopen into its parameter.
$file = fopen('hello.txt', < span class="string">'r');fclose($file);
In the example above, we opened the hello.txt file to read it, provided initial access, and then closed it immediately. Do not think that we reach the content just by providing access. After establishing the connection, we will perform the necessary operations using other reading and writing functions. Reading contents of files with PHP BWe will use the r mode in fopen() to perform this operation, and the fread() function to access the content. In the first parameter of this function, we will write the variable of the file we previously opened with fopen. In the second parameter, we will specify how many bytes we will read from the file.
$file = fopen('hello.txt', 'r');
$content = fread($file, filesize('hello.txt'));
echo $content;fclose($file);
Now, if there is something in our hello.txt file, it will be written on the screen exactly as it is. If it is empty, nothing will be written on the screen. Since we wanted to read the entire content of the file, we specified the total size of the file in the second parameter with the filesize() function to get the entire content. Writing to files with PHP We will use w mode and fwrite() function to write files. However, when using w mode, the content of the file we access is completely deleted and replaced with the value we will write. If you want to write without losing the previously existing content, then you will need to use a mode. This function works with two parameters. We write the variable that we accessed to the file with fopen() in the first parameter, and the content to be written in the second parameter.
$file = fopen('hello.txt', 'w');
fwrite($file, 'Hello World');
fclose($file);
When we ran the example above, we wrote Hello World in the content of our file named hello.txt. Let's give another example:
for($number = 1; $number < 4; $number++) {
$file_name = 'File' . $number . '.txt';
touch($file_name);
$file = fopen($file_name, 'w');
fwrite($file, 'The contents of this ' . $number . '. file!');
fclose($file);
}
In this example, we created 3 txt files (File1.txt, File2.txt, File3.txt) and added content to each of them as "The content of this 1st file". Deleting files with PHPWith the unlink() function, which we will see as the opposite of the file creation function. You can destroy any file you want. Its usage is as simple as the creation function and has a single parameter. unlink('hello.txt'); Deletes the hello.txt file that we worked on in other examples. Checking the existence of files in PHP, being aware of the existence of files, changing them if they exist, or creating them, etc. We will use the file_exists function to perform operations. If there is a file named the file whose parameter we will enter, it will return TRUE, otherwise it will return FALSE. Let's show it with an example:
for($number = 1; $number < 4; $number++) {
$file_name = 'File' . $number . '.txt'; touch($file_name);
$file = fopen($file_name, 'w');
fwrite($file, 'The contents of this ' . $number . '. file!');
fclose($file);
}
Since we deleted the hello.txt file in the previous topic, it will say "We have no file" on the screen.