


Troubleshooting on file operations in PHP development_PHP tutorial
作者:杨宗威
前言:
PHP中对各类数据库的操作有着支持,对文件的操作也同样有着很丰富的操作方法,很多朋友现在的操作还是基于文件操作可是有的时候在操作文件的时候还存在不少的困惑和疑点,以下是我在日常编写过程中碰到的以及坛上朋友所碰到的关于文件操作的一些问题收藏吧。
问:如何新建一个文件?
答:
1、使用fopen("要建立的文件名","参数"),参数可选w,w+,a,a+
2、使用exec("echo > 要建立的文件名");这样是使用系统方式建立这个文件,你还可以使用touch这个linux命令来建立
问:为什么我无法建立文件?
答:
1、如果你使用了fopen建立文件,是否正确的使用了参数
2、系统权限问题,请询问你的WEBMASTER你的FTP目录是否有写的权限
3、FTP权限问题,你要确认你的PHP文件所要写文件所在目录要有写的权限,也就是你的FTP软件登陆后other组要有写这个权限,
如果没有请修改权限后尝试
问:如何将文件读入数组?
答:使用file函数
问:如何将文件全部读出?
答:
1、使用fread($fp);
2、如果你的PHP版本>=4.3.0的话可以使用file_get_contents();
问:如何判断文件是否存在?
答:使用file_exists();
再问:为什么不使用fopen()来判断呢?
答:原因是有时候是因为权限问题导致fopen返回的数据引导我们错误的判断
问:为什么当我读取一个WEB页面的时候出错?
答:
1、可能是你的传递参数错,当读取WEB页面的时候你只可以使用r方式读取页面
2、确保你要读取的WEB页面可以访问
问:我如何才能获得文件的相关属性?
答:PHP提供了一组获得文件属性的方法,例如 filemtime(),fileowner(),filegroup(),filectime(),fileatime()...详细的使用请参阅手册。
问:PHP打开文件后是否可以象C一样进行文件“游标”的定位呢?
答:可以的,使用fseek();
问:我想在访问文件的时候不允许其他人也访问此文件,怎么办?
答:
1、你可以采用其他方面程序限制用户接入文件操作的页面
2、使用flock();详细的参数以及使用方法请参阅手册
问:如何删除文件内第一行,或指定一行数据?
答:
PHP并没有提供这样的操作方法,不过我们可以通过组合使用,以下代码演示我们将删除文件"test.dat"中的第三行数据(test.dat 文件中数据不止三行)
<?php
$filename="test.dat";//定义操作文件
$delline=3; //要删除的行数
if(!file_exsits($filename)){
die("指定文件未发现!操作中断!");
}
$farray=file($filename);//读取文件数据到数组中
for($tmpa=0;$Tmpa
if(strcmp($Tmpa+1,$delline)==0){
//判断删除的行
continue;
}
//重新整理后的数据
$newfp.=$farray[$Tmpa]." ";
}
$fp=@fopen($filename,"a") or die("写方式打开文件 $filename 失败");//我们以写的方式打开文件
@fputs($fp,$newfp) or die("文件写入失败");
@fclose($fp);
?>
以上代码演示的是删除一行文件,不过你如果仔细的看的话,其实也给你提供了其他的文件操作的相关提醒~
问:当我试图打开一个不存在的文件的时候,我如何不让错误显示出来以避免我的路径泄露!!
答:在你要打开文件的方法前增加@符号用来屏蔽错误,@是PHP提供的错误信息屏蔽的专用符号或您可以在这个要操作的步骤前增加(通常是在页首)error_reporting(0);用来屏蔽页面内所有错误信息的显示一个不推荐的方法就是去修改php.ini(ISP除外)。
问:我使用的是虚拟主机,我如何防止其他用户窃取我的数据?
答:建议ISP修改php.ini中的open_basedir进行限制,不推荐的ISP设置是将fopen,file等文件操作加入disable_function中。
问:为什么我用PHP建立文件后我FTP登陆要删除这些文件无法删除??
Answer: Mainly because the files created by PHP belong to the WEB user group, that is, the files created do not belong to your FTP user! ! ! The solution to this problem is to use chmod, unlink, etc. of the PHP program. It is recommended that users remember to chmod file permissions when using PHP to create files. It is recommended to be 777
Question: How to use text files as data warehouse? Some guestbooks, forums, etc. all use this!
Answer: In fact, this is just a typical example of using file and combining it with explode to read and split data.
Question: How to change the file name?
Answer: rename();
Question: How to delete files?
Answer: unlink(); exec("del(rm -vf) filename");
Note: rm -vf is for use under Linux
Question: How to clear files?
Answer: Use fopen(filename,"w"); or exec("echo > filename");
Question: How to edit the file content?
Answer: I remember I once answered a question about deleting file content. In fact, editing content only requires variable substitution based on deleting the content. I hope you can look up and change my continue above to replace the variable data.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
