简体   繁体   English

在php中写入文件时出错

[英]Error while writing to a file in php

While testing the following code on my local machine, no error was reported. 在我的本地计算机上测试以下代码时,未报告任何错误。 But after testing it on our server at work, I got the following strange error: php is not writing files on the server. 但是在工作的服务器上对其进行测试后,出现了以下奇怪的错误:php未在服务器上写入文件。

code I am using : 我正在使用的代码:

 $myfile = fopen( "results/co".$a[$i].".txt", "w") or die("Unable to open file!");
 fwrite($myfile,"some text");
 fclose($myfile);

So no file is created in this case. 因此,在这种情况下不会创建文件。 when I try to replace the file name "results/co".$a[$i].".txt" with its value : "results/co00112test.txt" : 当我尝试将文件名“ results / co”。$ a [$ i]。“。txt”替换为其值时:“ results / co00112test.txt”:

 $myfile = fopen( "results/co00112test.txt", "w") or die("Unable to open file!");
 fwrite($myfile,"some text");
 fclose($myfile);

it works just fine. 它工作正常。

I also tried the following: 我还尝试了以下方法:

 $name = "results/co".$a[$i].".txt";
 $myfile = fopen( $name, "w") or die("Unable to open file!");
 fwrite($myfile,"some text");
 fclose($myfile);

yet with no hope. 但是没有希望。

What could be the reason for this error ? 此错误的原因可能是什么?

If your code works fine when you're putting some static filename, it's obvious that your variable $a[$i] is wrong. 如果在放置一些静态文件名时代码工作正常,则很明显变量$a[$i]是错误的。

You can proceed that way to debug it : 您可以通过这种方式进行调试:

$name = "results/co".$a[$i].".txt";
var_dump($name);

The name should show something weird, and will tell you what's happening to your file. 该名称应该显示一些奇怪的内容,并且可以告诉您文件的内容。

it might be all because of permissions. 可能全是因为权限。 Your php user don't have permission to folder, where you want to create file or don't have permission to file, which you are trying to edit (if file is already there). 您的php用户无权访问要在其中创建文件的文件夹,也无权访问要尝试编辑的文件(如果文件已经存在)。

You can read full article about permissions here (it's about ubuntu, but it doesn't really matter which OS): https://help.ubuntu.com/community/FilePermissions 您可以在此处阅读有关权限的完整文章(关于ubuntu,但实际上与哪个操作系统无关): https : //help.ubuntu.com/community/FilePermissions

you can try permission 644 or if it's not working you can use 777. 644 is more safe, because only the owner of the file will be able to edit it, not any user of your OS. 您可以尝试使用权限644,或者如果它不起作用,则可以使用777。644更安全,因为只有文件所有者可以编辑它,而操作系统的任何用户都不能。

you can change permissions to folder and all files/folders in it like this: 您可以像这样更改文件夹及其所有文件/文件夹的权限:

chmod -R 644 /path/to/folder/

I think 644 is standard for files and 755 for directories. 我认为644是文件的标准配置,755是目录的标准配置。

you can change owner of the file/folder like this: 您可以这样更改文件/文件夹的所有者:

chown name_of_user:name_of_group /path/to/folder

Your file name is starting with double zero char so it might be an issue. 您的文件名以双零字符开头,因此可能是一个问题。

this will not work : 这将不起作用:

$number = 112;
$file_name = "results/co".$number."test.txt";

this should work : 这应该工作:

$number = 112;
$number_pad = str_pad($number, 5, '0', STR_PAD_LEFT);
$file_name = "results/co".$number_pad."test.txt";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM