Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 文件目录写入报错

文件目录写入报错

Jun 23, 2016 pm 02:18 PM

我用php建立一个文件夹。设置0777权限。用file_put_content写入文件报错:
failed to open stream: Permission denied'

1、首先想到的就是权限问题,我用函数判断创建的文件夹是否有可写权限。我创建test.txt测试,可以成功。
还有奇怪的是,程序往里面写入十几个文件都成功,但写到某个文件就报错了。如果没有可写权限,应该是一个文件都没有创建成功才是呀。

2、路径是正确的,这点检查很多遍了。

此外我想不到其他可能性了。麻烦高手指点一下,谢谢。


回复讨论(解决方案)

可能是目标文件存在,并被其他软件打开了

出现错误而时候,应给出完整的错误信息和出错行的代码(如果涉及到变量,则变量值应一并给出)

可能是目标文件存在,并被其他软件打开了

出现错误而时候,应给出完整的错误信息和出错行的代码(如果涉及到变量,则变量值应一并给出)

出错情况我已经简练的概括了要点了。详细报错如下:

Uncaught error with message 'E_WARNING: file_put_contents(E:\AppServ\www\phpwind\upload\/windid/attachment/avatar/000/00/05/515_middle.jpg) [function.file-put-contents]: failed to open stream: Permission denied'

出错行:$put_re = file_put_contents($avatar_dir_save, $img_re);//写入头像
$img_re这个变量是一个图像。我换成随便的一个字符串比如“3453”也不行。
$avatar_dir_save这个变量主要是提示报错中的那个路径,路径检查正确无误。

在写入文件之前,我都会判断文件是否存在才会执行这些代码。而且我看了当报错说写入不了那个文件时,我看了文件夹,的确没有那个文件。

想不出还有什么可能性了。

此外,http进程是system用户组。查看文件夹的权限,这个用户组是有权限的。

is_writable -- 判断给定的文件名是否可写

is_writable -- 判断给定的文件名是否可写

我用的是discuz的函数

public static  function dirWriteable($dir) {
$writeable = 0;
if(!is_dir($dir)) {
exit('sdg');
@mkdir($dir, 0777);
}
if(is_dir($dir)) {
if($fp = @fopen("$dir/test.txt", 'w')) {
@fclose($fp);
@unlink("$dir/test.txt");
$writeable = 1;
} else {
$writeable = 0;
}
}
return $writeable;
}

如果文件夹不可写,应该是一个文件都写不进去才是,为何写入十几个才出错?这个很麻烦

贴出你代码瞧瞧,是不是你写循环出现问题。

命名问题?如果是写到了某个文件,是单独只有一个还是有规律的几个,还是散乱的,这些都去试试

命名问题?如果是写到了某个文件,是单独只有一个还是有规律的几个,还是散乱的,这些都去试试

假设命名是胡乱命名的,但是出错的时候,显示的文件路径也是对的。所以文件名已经不重要了。路径对就行了。

命名问题?如果是写到了某个文件,是单独只有一个还是有规律的几个,还是散乱的,这些都去试试

散乱的。有时候一运行就出现错误,有时候第5个,第11个都有可能。


命名问题?如果是写到了某个文件,是单独只有一个还是有规律的几个,还是散乱的,这些都去试试

散乱的。有时候一运行就出现错误,有时候第5个,第11个都有可能。
整个文件路径长度,试试段路径,若不行,换机试试- -若还不行,建议放弃原有的DZ论坛的文件,自己写一个

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)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles