php - files
Manipulating files is a basic necessity
for serious programmers and PHP gives you a great deal of tools for creating,
uploading, and editing files.
his section of the PHP tutorial is completely
dedicated to how PHP can interact with files. After completing this section you
should have a solid understanding of all types of file manipulation in PHP!
php - how to create a file
The fopen function needs two important pieces of
information to operate correctly. First, we must supply it with the name of the
file that we want it to open. Secondly, we must tell the function what we plan
on doing with that file (i.e. read from the file, write information, etc).
Since we want to create a file, we must supply a file name and
tell PHP that we want to write to the file. Note: We have to tell PHP we are
writing to the file, otherwise it will not create a new file.
PHP Code:
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or
die("can't open file");
fclose($ourFileHandle);
1.
$ourFileName =
"testFile.txt";
Here we create the name of our file,
"testFile.txt" and store it into aPHP String variable $ourFileName.
2.
$ourFileHandle = fopen($ourFileName,
'w') or die("can't open
file");
This bit of code actually has two
parts. First we use the function fopenand give it two arguments:
our file name and we inform PHP that we want to write by passing the character
"w".
Second, the fopen function returns what is called
a file handle, which will allow us to manipulate the file. We save
the file handle into the$ourFileHandle variable. We will talk more
about file handles later on.
3.
fclose($ourFileHandle);
We close the file that was
opened. fclose takes the file handle that is to be closed. We
will talk more about this more in the file closing lesson.
Comments
Post a Comment
Thank you for your Comment