How To Create a File With PHP And Write Content To It
One of my tasks for a project was to create an XML file with simple XML fields to be able to index content in to a Solr Search Core.
So today I will teach you how to create an file with php and write content to it.
Lets get started.
Step 1
Write a function / method that will create the file and write content to it.
function
my_file_writer(
$filename
){
// Code goes here
}
Step 2
In this step I want to grab content from my mysql database tables.
function
my_file_writer(
$filename
){
// connect to db.
echo
"connecting...
";
$con
= mysql_connect(
"localhost"
,
"username"
,
"password"
);
if
(!
$con
)
{
die
(
'Could not connect: '
. mysql_error());
}
mysql_select_db(
"database_name"
,
$con
)
or
die
(
'Could not connect: '
. mysql_error());
echo
"Connected
";
// select latest articles.
echo
"Selecting data...
";
$result
= mysql_query(
"SELECT id, title, content FROM table"
)
or
die
(
'Could not select data: '
. mysql_error());
echo
"Selected
";
}
Step 3
Here we are going to add a while loop to grab the mysql data we selected and put it into the right format for writing it to our file.
function
my_file_writer(
$filename
){
// connect to db.
echo
"connecting...
";
$con
= mysql_connect(
"localhost"
,
"username"
,
"password"
);
if
(!
$con
)
{
die
(
'Could not connect: '
. mysql_error());
}
mysql_select_db(
"database_name"
,
$con
)
or
die
(
'Could not connect: '
. mysql_error());
echo
"Connected
";
// select latest articles.
echo
"Selecting data...
";
$result
= mysql_query(
"SELECT id, title, content FROM table"
)
or
die
(
'Could not select data: '
. mysql_error());
echo
"Selected"
;
$stringData
=
""
;
// we put the first part of the $stringData variable outside the loop
while
(
$row
= mysql_fetch_array(
$result
))
{
// set variables
$page_id
=
$row
[
'id'
];
$page_url
=
$row
[
'url'
];
$pagetitle
=
$row
[
'title'
];
$bodytext
=
$row
[
'content'
];
$bodytext
=
strip_tags
(
$bodytext
);
// Here we strip all html tags from the body text
$stringData
.= "
$page_id
http:
//your-url-here.com/pagename
$pagetitle
$bodytext
\n\n";
}
// end of while loop
$stringData
.=
""
;
// we put the final part of the $stringData variable at the end outside of the while loop
}
?>
Step 4
In this step we start creating and writing to the file.
$fh
=
fopen
(
$filename
,
'w'
);
// we create the file, notice the 'w'. This is to be able to write to the file once.
fwrite(
$fh
,
$stringData
);
// here we write the data to the file.
echo
"Writing file...
";
echo
"Closing file...
";
fclose(
$fh
);
//here we close our file
echo
'Finished!'
;
Step 5
all thats left to do is to close the mysql connection.
mysql_close(
$con
);
// close the connection
Step 6
Now you can create your file and run the script.
my_file_writer(
"Your_File_Name.xml"
);
Conclusion
As you can see it’s really easy to create a file and add any content you want to it. You should be able to see a file with the name you gave it after you ran your script. Any suggestion is welcome.