PHP common functions for reading local files

Source: Internet
Author: User
Tags flock fread
Below we use the. txt file as an example to introduce PHP read local file function, read the file we can be used fopen or file_get_contents to read, file_get_ Contents is simpler and fopen requires fread mates to display what is read.

1. First introduce the fopen () function

Here we give a direct open local file code demo, where necessary we have added a note.

Open the instance code of a local file directly:

<?php

If our local file is a text named Xmlas.txt

$filedemo = "Xmlas.txt";

$fpdemo = fopen ($filedemo, "R");

if ($fpdemo) {

while (!feof ($fpdemo)) {

1000 Number of characters read

$datademo = Fread ($fpdemo, 1000);

}

Fclose ($fpdemo);

}

Echo $datademo;

?>

2. Use the file_get_contents () function to output the entire file in a string, if our xmlas.txt text file has such a sentence: Now the film is more and more not passionate, want to all climax please see Japanese love action movie, then we file_get_ The instance code for the contents () function is as follows:

<?php

Echo file_get_contents ("Xmlas.txt");

At this point the output will be the content in Xmlas.txt:

Now the movie more and more no passion, want to all climax please see Japanese love action movie!

?>

3. How to read a local folder instead of a separate file:

Take a look at the example code below, in which we will read a folder named Xmlas, with the following code:

<?php

$dirdemo = Opendir ('/xmlas ');

while (($filedemo = Readdir ($dirdemo))!=false) {

if ($filedemo! = "." && $filedemo! = "...") {

$nsdemo = Explode ('. ', $filedemo);

echo $nsdemo [0];

}

}

Closedir ($dirdemo);

?>

4. We can also use fopen to write files with the following code:

/**

* Write files with fopen

* @param string $filename

* @param string $contents

* @return Boolean

*/

function Wirte ($filename,& $contents)

{

$FP =fopen ($filename, "WB");

if ($FP)

{

Flock ($FP, LOCK_EX);//Lock a file at the same time, only one person can operate it

Fwrite ($fp, $contents);

Flock ($FP, lock_un);//save data grip to unlock file and save

Fclose ($FP);//Open Source code phpfensi.com

return true;

}else

{

return false;

}

}

So long as the use of fopen with fwrite can realize the file read and write.

Note: To open a file, before opening the file, we need to know the path to the file and whether the file exists.

With $_server["Document_root"] built-in global variables, to obtain the relative path of the site, as follows:

$root = $_server["Document_root"];

Use the function file_exists () to detect if a file exists, as follows:

If (!file_exists ("$root/order.txt")) {echo ' file does not exist ';}

This may be more reasonable and practical.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.