File

The readfile() functin reads a file and writes it to the output buffer.

readfile('filename')

A better method to open files is with the fopen() function.This function gives you more options than the readfile() function.It also used to create a file.If you use fopen() on a file that it doesn't exist, it will create it, given that the file is opened for writing or appending.

fopen('filename', 'acccess')

Here are the parameters:

r -- open a file for read only

w -- open a file for write only  Erases the content of the file or creates a new file if it doesn't exist

a -- open a file for write only The existing data n file is preserved.File pointers starts at the end of the life.Create a new file if the file doesn't exist

x -- create a new file for write only Return false and an error if file already exists

r+ -- open a file for read/write  File pointer starts at the beginning of the file

w+ -- open a file for read/write  Erases the contents of the file or creates a new file if it doesn't exisit.File pointer starts at the beginning of the file

a+ -- open a file for read/write The existing data in file is preserved.File pointer starts at the end of the file.Creates a new file if the file doesn't exist

x+ -- create a new file for read/write return false and an error if file already exists

The fread() function reads from an open file.

fread($myfile,filesize()) // the first parameter of read()contains the name of the file to read from and the second parameter specifies the maximun number of bytes to read.

The fclose() function is used to close an open file.

fclose($myfile)

The fgets() function is used to read a single line from  a file.

fgets($myfile)

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>

The feof() function checks if the end-of-file(EOF) has been reached.It is useful for looping through data of unknown length.

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . "<br>";
}
fclose($myfile);
?>

The fgetc() function is used to read a single character from a file.

<?php
$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
  echo fgetc($myfile);
}
fclose($myfile);
?>

The fwrite() function is used to write to a file.

fwrite($myfile, $txt) // the first parameter is the name of the file to write to and the second parameter is the string to be written

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "John Doe ";
fwrite($myfile, $txt);
$txt = "Jane Doe ";
fwrite($myfile, $txt);
fclose($myfile);
?>

File Upload

First, ensure that PHP is configured to allow files uploads.In your php.ini file, set it to in : file_uploads = On

Some rules to upload for the HTML form above:

  • Make sure that the form uses method = "post"
  • The form also needs the following attribute: enctype="multipart/form-data".It specifies which content type to use when submitting the form
  • The type of file attributes of the <input> tag shows the input field as a file.

Without the requirements above, the file upload will not work.

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

<?php
$target_dir = "uploads/";    specifies the directory where the file is going to be placed
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);  //specifies the path of the file to be upload
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); // hold the file extension of the file
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1; // not used yet
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>

Check if file already exists.

if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}

Limit file size

if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}

Limit file type

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

原文地址:https://www.cnblogs.com/forerver-elf/p/5198780.html