PHP_ file related operations

One, create a file

php opens a file is to create a file

1. Open the file

  • fopen()
parameter description
r Read only. Start at the beginning of the file.
r+ Read/write. Start at the beginning of the file.
w just write. Open and clear the contents of the file; if the file does not exist, create a new file.
w+ Read/write. Open and clear the contents of the file; if the file does not exist, create a new file.
a Append. Open and write to the end of the file. If the file does not exist, create a new file.
a+ Read/append. Keep the contents of the file by writing to the end of the file.
x just write. Create a new file. If the file already exists, FALSE and an error are returned.
x+ Read/write. Create a new file. If the file already exists, FALSE and an error are returned.

ps: Note: If the fopen() function cannot open the specified file, it returns 0 (false).

2. Read the file line by line (EOF)

  • The fget() function is used to read the file line by line from the file.
    PS: After calling this function, the file pointer will move to the next line.
$file = fopen("welcome.txt", "r") or exit("无法打开文件!");
// 读取文件每一行,直到文件结尾
while(!feof($file))
{
    echo fgets($file). "<br>";
}
fclose($file);

3. Read the file character by character

  • The fgetc() function is used to read the file character by character from the file
    ps: after calling this function, the file pointer will move to the next character.
$file=fopen("welcome.txt","r") or exit("无法打开文件!");
while (!feof($file))
{
    echo fgetc($file);
}
fclose($file);

4, the end of the file

  • The feof() function detects whether the end of the file has been reached (EOF).
    ps: In w, a and x modes, you cannot read the opened file!
if (feof($file)) echo "文件结尾";

5. Move files

  • Move the file with copy, and then delete the source file idea
  $file=’userfile/a/abc.txt’; //旧目录
  $newFile=’userfile/b/newabc.txt’; //新目录
  copy($file,$newFile); //拷贝到新目录
  unlink($file); //删除旧目录下的文件

2. File upload

php file upload uses the global function $_FILES

1,$_FILES

parameter description
$_FILES[“file”][“name”] The name of the uploaded file
$_FILES[“file”][“type”] Type of uploaded file
$_FILES[“file”][“size”] The size of the uploaded file in bytes
$_FILES[“file”][“tmp_name”] The name of the temporary copy of the file stored on the server
$_FILES[“file”][“error”] Error code caused by file upload

2. Upload a simple script

// 允许上传的图片后缀
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
echo $_FILES["file"]["size"];
$extension = end($temp);     // 获取文件后缀名
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 204800)   // 小于 200 kb
&& in_array($extension, $allowedExts))
{
    if ($_FILES["file"]["error"] > 0)
    {
        echo "错误:: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
        echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
        echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
        echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"] . "<br>";
        
        // 判断当前目录下的 upload 目录是否存在该文件
        // 如果没有 upload 目录,你需要创建它,upload 目录权限为 777
        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " 文件已经存在。 ";
        }
        else
        {
            // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
            echo "文件存储在: " . "upload/" . $_FILES["file"]["name"];
        }
    }
}
else
{
    echo "非法的文件格式";
}

Guess you like

Origin blog.csdn.net/weixin_43272542/article/details/113376880