Home Backend Development PHP Tutorial PHP开发中关于文件操作的疑难问答_PHP

PHP开发中关于文件操作的疑难问答_PHP

Jun 01, 2016 pm 12:29 PM
use about Can how develop operate document Difficulties Q&A

前言:

  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].\\r\\n;
 }
 $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登陆要删除这些文件无法删除??

  答:主要是因为PHP建立的文件归属WEB用户组,也就是建立的文件,并非是你FTP用户的!!!这个问题的解决就是,使用PHP程序的chmod,unlink等方式进行处理,建议用户在使用PHP建立文件的时候记得chmod文件权限,建议为777

  问:如何使用文本文件作为数据仓库?有的留言本,论坛之类的都是使用这个的啊!

  答:其实这个主要还是使用了file,结合explode进行数据读取与分割的典型范例而已。

  问:如何更改文件名?

  答:rename();

  问:如何删除文件?

  答:unlink(); exec(del(rm -vf) filename);

  注:rm -vf为linux下使用

  问:如何清空文件?

  答:使用fopen(filename,w);或exec(echo '' > filename);

  问:如何编辑文件内容?

  答:我记得我以前回答过一个删除文件内容的,其实编辑内容在删除内容的基础上,进行变量替换就可以了。希望你可以向上找找,将我上面的continue修改为替换变量数据就可以了。

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 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 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)

What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. What to do if the 0x80004005 error code appears. The editor will teach you how to solve the 0x80004005 error code. Mar 21, 2024 pm 09:17 PM

When deleting or decompressing a folder on your computer, sometimes a prompt dialog box "Error 0x80004005: Unspecified Error" will pop up. How should you solve this situation? There are actually many reasons why the error code 0x80004005 is prompted, but most of them are caused by viruses. We can re-register the dll to solve the problem. Below, the editor will explain to you the experience of handling the 0x80004005 error code. Some users are prompted with error code 0X80004005 when using their computers. The 0x80004005 error is mainly caused by the computer not correctly registering certain dynamic link library files, or by a firewall that does not allow HTTPS connections between the computer and the Internet. So how about

Four recommended AI-assisted programming tools Four recommended AI-assisted programming tools Apr 22, 2024 pm 05:34 PM

This AI-assisted programming tool has unearthed a large number of useful AI-assisted programming tools in this stage of rapid AI development. AI-assisted programming tools can improve development efficiency, improve code quality, and reduce bug rates. They are important assistants in the modern software development process. Today Dayao will share with you 4 AI-assisted programming tools (and all support C# language). I hope it will be helpful to everyone. https://github.com/YSGStudyHards/DotNetGuide1.GitHubCopilotGitHubCopilot is an AI coding assistant that helps you write code faster and with less effort, so you can focus more on problem solving and collaboration. Git

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

Learn how to develop mobile applications using Go language Learn how to develop mobile applications using Go language Mar 28, 2024 pm 10:00 PM

Go language development mobile application tutorial As the mobile application market continues to boom, more and more developers are beginning to explore how to use Go language to develop mobile applications. As a simple and efficient programming language, Go language has also shown strong potential in mobile application development. This article will introduce in detail how to use Go language to develop mobile applications, and attach specific code examples to help readers get started quickly and start developing their own mobile applications. 1. Preparation Before starting, we need to prepare the development environment and tools. head

Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Which AI programmer is the best? Explore the potential of Devin, Tongyi Lingma and SWE-agent Apr 07, 2024 am 09:10 AM

On March 3, 2022, less than a month after the birth of the world's first AI programmer Devin, the NLP team of Princeton University developed an open source AI programmer SWE-agent. It leverages the GPT-4 model to automatically resolve issues in GitHub repositories. SWE-agent's performance on the SWE-bench test set is similar to Devin, taking an average of 93 seconds and solving 12.29% of the problems. By interacting with a dedicated terminal, SWE-agent can open and search file contents, use automatic syntax checking, edit specific lines, and write and execute tests. (Note: The above content is a slight adjustment of the original content, but the key information in the original text is retained and does not exceed the specified word limit.) SWE-A

How to use Xiaomi Auto app How to use Xiaomi Auto app Apr 01, 2024 pm 09:19 PM

Xiaomi car software provides remote car control functions, allowing users to remotely control the vehicle through mobile phones or computers, such as opening and closing the vehicle's doors and windows, starting the engine, controlling the vehicle's air conditioner and audio, etc. The following is the use and content of this software, let's learn about it together . Comprehensive list of Xiaomi Auto app functions and usage methods 1. The Xiaomi Auto app was launched on the Apple AppStore on March 25, and can now be downloaded from the app store on Android phones; Car purchase: Learn about the core highlights and technical parameters of Xiaomi Auto, and make an appointment for a test drive. Configure and order your Xiaomi car, and support online processing of car pickup to-do items. 3. Community: Understand Xiaomi Auto brand information, exchange car experience, and share wonderful car life; 4. Car control: The mobile phone is the remote control, remote control, real-time security, easy

See all articles