Home  >  Article  >  Backend Development  >  php fwrite writes the file successfully but the file has no content

php fwrite writes the file successfully but the file has no content

WBOY
WBOYOriginal
2016-12-01 00:26:052675browse

I found a very strange problem. When I used fwrite to write a file, the program did not report an error. fwrite also returned the number of words written, but the content in the file was empty.

<code>        $path = Yii::$app->basePath."/upload/tt.txt";
        $file = fopen($path,'w');
        fwrite($file,'aaaaa');
        fclose($file);</code>

Using the Yii2.0 framework.
Try using file_put_contents again, but still no response O__O "…

Reply content:

I found a very strange problem. When I used fwrite to write a file, the program did not report an error. fwrite also returned the number of words written, but the content in the file was empty.

<code>        $path = Yii::$app->basePath."/upload/tt.txt";
        $file = fopen($path,'w');
        fwrite($file,'aaaaa');
        fclose($file);</code>

Using the Yii2.0 framework.
Try using file_put_contents again, but still no response O__O "…

Please check if the file has writable permissions

It feels like the writing was successful, please look for possible paths.

<code>is_writable($file) 
</code>

Determine write permission

But the author seems to have the wrong path

It should be that the path is written wrong. You can print out $path and have a look

Raise the error level of php and debug it

Please check whether the file encoding format conflicts.

Enter $path and confirm whether the path is correct

Statement:
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