Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 为什么在服务器上php里执行file_put_contents创建文件返回是false呢,文件目录已经设置为777的权限了,不解

为什么在服务器上php里执行file_put_contents创建文件返回是false呢,文件目录已经设置为777的权限了,不解

Jun 23, 2016 pm 02:01 PM

想用file_get_contents读取网络上的图片文件并由file_put_contesnts写入到服务器本地,file_get_contesnts执行没问题,可以读取信息,然后将读出的数据$data执行:
var_dump(file_put_contents($local_file_position, $data));在测试中会显示var_dump出的信息,但最后file_put_contents返回是false,不知道为什么,在网上搜说是aparche没有写权限,是这样的吗?如果是,如何设置呢?

环境:CentOS 6.5 + MySQL5.5.35 + tomcat7.0.47


回复讨论(解决方案)

补充一下,要写入的文件所在路径的权限已经设置为777

用的是绝对路径还是相对路径?

绝对路径和相对路径是一样的

先确认file_get_contents()获取的数据是否存在,数据类型是否合法。

这个已经确认数据是存在的,数据就是字符串,即使我用fopen 和fwrite也是存在问题。

估计是权限不够
echo substr(sprintf('%o', fileperms('你的目录')), -4); //看看是什么结果

绝对路径和相对路径是一样的
要求能写入的话 文件所在的目录也是需要有写的权限的
比如 /home/centos/123.txt
目录home、centos也都是需要写的权限的不只是单单123.txt需要写权限

估计是权限不够
echo substr(sprintf('%o', fileperms('你的目录')), -4); //看看是什么结果

返回0755

估计是权限不够
echo substr(sprintf('%o', fileperms('你的目录')), -4); //看看是什么结果

我修改成了777,执行返回:

0777

file_put_contents 失败时会有明确的错误信息输出
请贴出

php文件t.php放在目录(绝对路径)/usr/local/www/wxtest
$fp=fopen('txt/test.txt', 'wb');
if($fp) fwrite($fp, "TESTSETST");
else echo "fail to create file. 
";
fclose($fp);
?>

在windows浏览器地址直接输入该php文件
执行结果如下:
Warning: fopen(txt/test.txt): failed to open stream: Permission denied in /usr/local/www/wxtest/t.php on line 87 fail to create file. 
Warning: fclose() expects parameter 1 to be resource, boolean given in /usr/local/www/wxtest/t.php on line 90 

提示很清楚了,
Permission denied 
拒绝访问

你是要把文件写到 /usr/local/www/wxtest/txt 目录中
请检查目录存在且可写

目录的权限已经设置成了777权限。
并且防止上级目录权限不足,我把/usr/local/www/wxtest/txt所有目录都设置成了755,都包含写权限,还是不行。

1. 755权限指的是 rwxr-xr-x, 同组和其他用户是没有写权限的。 777才是全部用户拥有所有权限。

2. 建议使用绝对路径,写文件前在脚本内判断路径是否存在,不存在则先创建路径(目录)再写文件,这个是血淋淋的教训。

1. 755权限指的是 rwxr-xr-x, 同组和其他用户是没有写权限的。 777才是全部用户拥有所有权限。

2. 建议使用绝对路径,写文件前在脚本内判断路径是否存在,不存在则先创建路径(目录)再写文件,这个是血淋淋的教训。

权限修改为 777  也不行啊。路径肯定是存在的。

把/usr/local/www/wxtest/txt设成777

把/usr/local/www/wxtest/txt设成777

把/usr/local/www/wxtest/txt设成777

已经设置成了777

是不是apache里面还有需要设置的,或是系统里面那个地方还需要设置?

确定是对组外用户的权限是777?

确定是对组外用户的权限是777?

组外用户不就是other组吗?你指的是什么?

呵呵,解决了,结贴,谢谢各位的帮忙!

呵呵,解决了,结贴,谢谢各位的帮忙!
怎么解决的 可否分享下


呵呵,解决了,结贴,谢谢各位的帮忙!
怎么解决的 可否分享下

默认下selinux是开启的
查看SELinux状态:
1、/usr/sbin/sestatus -v      ##如果SELinux status参数为enabled即为开启状态
SELinux status:                 enabled
2、getenforce                 ##也可以用这个命令检查
关闭SELinux:
1、临时关闭(不用重启机器):
setenforce 0                  ##设置SELinux 成为permissive模式
                              ##setenforce 1 设置SELinux 成为enforcing模式
2、修改配置文件需要重启机器:
修改/etc/selinux/config 文件
将SELINUX=enforcing改为SELINUX=disabled
重启机器即可

另外,如果以上还是不能解决的话要设置一下目录的权限策略
chcon -R -t httpd_sys_content_t /home/html

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

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.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

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�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

See all articles