Tutorial PHP file open/read and create/write

in #utopian-io7 years ago

Tutorial PHP file open/read and create/write

PHP file open/read and create/write

Here we will understand all types of file manipulation in PHP, like create, open, and close a file. After establishing those basics, we will then cover other important file tasks, such as: read, write, and uploading files with PHP.

When we are manipulating files we must be very careful because we can do a lot of damage if we do something wrong. Common errors include editing the wrong file, filling a hard-drive with garbage data, and accidentally deleting a file's contents.

PHP File Opening fopen()

In PHP, a file is created using a command that is also used to open files. It may seem a little confusing, but we'll try to explain it further.
In PHP the fopen function is used to open files. However, it can also create a file if it does not find the file specified in the function call. So if you use fopen on a file that does not exist, it will create it, given that you open the file for writing or appending.

resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )

If filename is of the form "scheme://...", it is assumed to be a URL and PHP will search for a protocol handler (also known as a wrapper) for that scheme. If no wrappers for that protocol are registered, PHP will emit a notice to help you track potential problems in your script and then continue as though filename specifies a regular file.

If PHP has decided that filename specifies a local file, then it will tra to open a stream on that file. The file must be accessible to PHP, so you need to ensure that the file access permissions allow this access. If you have enabled safe mode or open_basedir further restrictions may apply.

If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.

The mode parameter specifies the type of access you require to the stream. It may be any of the following:

Mode Description

1.png

fopen() binds a named resource, specified by filename, to a stream.
Returns a file pointer resource on success, or FALSE on error.

< ?php
$file=fopen("welcome.txt","r");
? >
If the fopen() function is unable to open the specified file, it returns 0 (false).

PHP File Creating

< ?php
$file=fopen("welcome.txt","r") or exit("Unable to open file!");? >

PHP File Closing

PHP closes the file automatically once it reaches the end of the script, but it's a good habit to closing the file with fclose().

The fclose() requires the name of the file (or a variable that holds the filename) we want to close.

< ?php
$file = fopen("test.txt","r");

//some code to be executed

fclose($file);
? >

PHP File Read fread()

The fread() reads from an open file. The function will stop at the end of the file or when it reaches the specified length, whichever comes first.This function returns the read string, or FALSE on failure.

< ?php
$file = fopen("test.txt","r");
fread($file,"10");
fclose($file);
? >

PHP Writing File fwrite()

The fwrite() function is used to write to a file.
There are two differences: first, you must fopen() the file in write mode ('w' for write), and second, instead of using the fread() function to read from the file handle, use the fwrite() function to write to it.

< ?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Jugal Patel\n";
fwrite($myfile, $txt);
$txt = "Jugal Patel\n";
fwrite($myfile, $txt);
fclose($myfile);
? >

PHP Check end of file

The feof() function checks if the "end-of-file" (EOF) has been reached.The feof() function is useful for looping through data of unknown length.
You cannot read from files opened in w, a, and x mode!

< ?php
$file = “name.txt”;
if (feof($file)) echo "End of file";
? >



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.
Next time.. Please more informative
You can contact us on Discord.
[utopian-moderator]

thanks! will do!

Very interesting brother. Seems like you know your stuff

thanks alot!

Your contribution cannot be approved yet because it does not have proof of work. See the Utopian Rules. Please edit your contribution and add proof ( screenshots, experiment this tutorial or etc) of your work, to reapply for approval.

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

hard to provide front-end because it is back-end..

Hey @brothermic I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

thanks alot!

Coin Marketplace

STEEM 0.17
TRX 0.15
JST 0.028
BTC 62205.55
ETH 2397.85
USDT 1.00
SBD 2.50