Home Backend Development PHP Tutorial PHP读取文件并可支持远程文件的代码分享_PHP

PHP读取文件并可支持远程文件的代码分享_PHP

Jun 01, 2016 pm 12:08 PM
read file remote file

php读取文件

案例一
复制代码 代码如下:
$file = 'bitsCN.com.php';
//本案例不支持远程
$fso = fopen($file, 'r');
echo $data = fread($fso, filesize($file));
fclose($fso);
?>

fopen() 将 file 指定的名字资源绑定到一个流上.
filesize 返回文件大小的字节数,如果出错返回 FALSE.
注: 因为 PHP 的整数类型是有符号的,并且大多数平台使用 32 位整数,filesize() 函数在碰到大于 2GB 的文件时可能会返回非预期的结果.对于 2GB 到 4GB 之间的文件通常可以使用 sprintf("%u", filesize($file)) 来克服此问题.
fread() 从文件指针 handle 读取最多 length 个字节. 该函数在读取完 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时就会停止读取文件,视乎先碰到哪种情况.
说明:低版本用法!建议php5用file_get_contents

案例二
复制代码 代码如下:
$file = 'bitsCN.com.php';
//支持远程
$file = 'http://www.bitsCN.com';//
echo $data = implode('', file($file));
?>

file -- 把整个文件读入一个数组中
说明
读取二进制的文件

案例三
复制代码 代码如下:
$file = 'http://www.bitsCN.com';
echo file_get_contents($file);
?>

file_get_contents -- 将整个文件读入一个字符串
说明
string file_get_contents ( string filename [, int use_include_path [, resource context]])
和 file() 一样,只除了 file_get_contents() 将文件返回为一个字符串.
file_get_contents() 函数是用来将文件的内容读入到一个字符串中的首选方法.如果操作系统支持还会使用内存映射技术来增强性能.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to read text file contents using File.ReadAllText function in C# How to read text file contents using File.ReadAllText function in C# Nov 18, 2023 pm 03:23 PM

How to use the File.ReadAllText function in C# to read the contents of a text file. In C# programming, we often need to read the contents of a text file. File.ReadAllText is a very convenient function that can help us quickly read the entire contents of a text file. This article will introduce how to use the File.ReadAllText function and provide specific code examples. First, we need to introduce the System.IO namespace in order to use the related methods of the File class.

PHP reads file content: steps to implement data import and parsing PHP reads file content: steps to implement data import and parsing Sep 06, 2023 pm 12:45 PM

Reading file contents with PHP: Steps to implement data import and parsing Importing and parsing file contents is one of the very common operations in web development. File importing and parsing can be easily achieved using PHP, and this article will describe the steps to achieve this process and provide code examples. Step 1: Select the file to import and parse In PHP, you first need to select the file to import and parse. You can use the file selection form or specify the file path manually. Here is sample code for a file selection form: <formmethod

Use the ioutil.ReadFile function to read the file contents and return a byte slice Use the ioutil.ReadFile function to read the file contents and return a byte slice Jul 26, 2023 pm 05:40 PM

Title: Use the ioutil.ReadFile function to read file contents and return byte slices Article content: In the standard library of the Go language, there is a very commonly used function ioutil.ReadFile(), which can be used to read from a specified file content and returns a byte slice. This function provides a simple and convenient way to read files and conveniently process the file contents for further processing. Below, we will show you how to use ioutil.R with a simple code example

How to read file contents using file_get_contents function in PHP How to read file contents using file_get_contents function in PHP Jun 26, 2023 pm 12:01 PM

In PHP, we often need to read data from files. In this case, we can use the file_get_contents function. This function can simply read everything from a file and return it as a string. This is very useful in many scenarios, such as reading configuration files, reading log files, etc. In this article, we will explain how to read file contents using the file_get_contents function in PHP. Step 1: Open the file using file

Get all the knowledge about reading files in Python in one article Get all the knowledge about reading files in Python in one article Apr 11, 2023 pm 11:22 PM

Files are everywhere, no matter which programming language we use, handling files is essential for every programmer. File processing is a process used to create files, write data and read data from them, Python There are a wealth of packages for processing different file types, which makes it easier and more convenient for us to complete file processing work. Outline of this article: Use the context manager to open files. File reading mode in Python. Read text files. Read CSV files. Get JSON File Open File Before accessing the contents of the file, we need to open the file. Python provides a built-in function that helps us open files in different modes. The open() function accepts two bases

Analysis of Python's underlying technology: how to implement file reading and writing Analysis of Python's underlying technology: how to implement file reading and writing Nov 08, 2023 am 11:15 AM

Analysis of Python's underlying technology: How to implement file reading and writing, specific code examples are required. In Python programming, file operations are one of the most common and important operations. File reading and writing involves Python's underlying I/O technology. This article will explore how to use Python to implement file reading and writing operations, and provide specific code examples. 1. File reading Python provides a variety of methods for reading file contents. The most common ones include using the open() function and using the with statement. Use the open() function

Use the ioutil.ReadFile function to read the file contents and return a string Use the ioutil.ReadFile function to read the file contents and return a string Jul 25, 2023 pm 10:41 PM

Title: Use the ioutil.ReadFile function to read file contents and return a string In Go language, there are many ways to read file contents and process them, one of them is to use the ReadFile function in the ioutil package. This article will introduce how to use the ioutil.ReadFile function to read a file and return its contents as a string. The ioutil.ReadFile function is a convenient method for reading file contents provided in the Go language standard library. it accepts a

PHP and FTP: Encrypt and decrypt remote files PHP and FTP: Encrypt and decrypt remote files Jul 30, 2023 pm 05:09 PM

PHP and FTP: Encryption and decryption of remote files Overview: With the development of network technology, File Transfer Protocol (FTP) inevitably faces security challenges when transferring files. This article will explore how to use the PHP programming language combined with FTP to implement encryption and decryption of remote files to protect the security of files during transmission. FTP file transfer basics FTP (FileTransferProtocol) is a standard protocol for file transfer on the network. Via FTP

See all articles