How do I open a file to write content to?



How do I open a file to write content to?..

Answer / jude jeevanraj.p

Working with files and folders in PHP is fairly easy though
it can take a little while to get used to.

You need to create a filehandle, which is a pointer to a
file, that you wish to write too.

Use the fopen function to open a file, and pass the name of
the file as the first parameter and what you want to do
with it second - in this case 'w' for write.

Then simple use fwrite to write to your file, and close it
with fclose.

Here's a simple example that will create a file in the
directory called example.txt and write to it with just a
few lines of PHP code.

<?php

$myfile = fopen("example.txt","w");
fwrite($myfile,"That was easy!");
fclose($myfile);
?>

Is This Answer Correct ?    5 Yes 0 No

Post New Answer

More PHP Interview Questions

What are the different types of errors in PHP?

5 Answers  


How to stop the execution of php script?

0 Answers  


What is string in php?

0 Answers  


What are the different tables present in mysql, which type of table is generated when we are creating a table in the following syntax: create table employee(eno int(2),ename varchar(10)) ?

7 Answers   HCL,


recurring account in PayPal payment gateway?

1 Answers   A1 Technology, HBL, HBL Power Systems, IBM,


Why use php artisan serve?

0 Answers  


What is the difference between runtime exception and compile time exception?

0 Answers  


Write a query to find the 2nd highest salary of an employee from the employee table?

0 Answers  


How does csrf attack work?

0 Answers  


When to use single quotes, double quotes, and backticks?

0 Answers  


How come the code works, but does not for two-dimensional array of mine?

0 Answers  


Where are php configuration settings stored?

0 Answers  


Categories