Opening a file in PHP

Opening a file in PHP

In PHP, you can open a file using the fopen() function, which takes two parameters: the first parameter is the file name or path, and the second parameter specifies the mode in which the file should be opened.

The mode parameter can be a combination of the following:

  • “r” for read-only access
  • “w” for write-only access (and creates the file if it doesn’t exist)
  • “a” for write-only access (appends to the file if it exists)
  • “x” for write-only access (and returns an error if the file already exists)
  • “b” for binary mode (useful when dealing with non-text files)
  • “+” for read and write access

Here’s an example of opening a file in PHP:

$file = fopen(“example.txt”, “r”); // do something with the file… fclose($file);

In this example, the fopen() function opens the file “example.txt” in read-only mode and returns a file handle, which is stored in the $file variable. Once you’re done working with the file, you should close it using the fclose() function to free up resources.

Apply for PHP Certification!

https://www.vskills.in/certification/certified-php-developer

Back to Tutorials

Get industry recognized certification – Contact us

Menu