php 如何在PHP中写入文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1768894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to write into a file in PHP?
提问by brilliant
I have this script on one free PHP-supporting server:
我在一台支持 PHP 的免费服务器上有这个脚本:
<html>
<body>
<?php
$file = fopen("lidn.txt","a");
fclose($file);
?>
</body>
</html>
It creates the file lidn.txt, but it's empty.
它创建了文件lidn.txt,但它是空的。
How can I create a file and write something into it, for example the line "Cats chase mice"?
我如何创建一个文件并向其中写入一些内容,例如“猫追逐老鼠”这一行?
回答by Savageman
You can use a higher-level function like:
您可以使用更高级别的函数,例如:
file_put_contents($filename, $content);
which is identical to calling fopen(), fwrite(), and fclose()successively to write data to a file.
这与依次调用fopen()、fwrite()和fclose()将数据写入文件相同。
Docs: file_put_contents
回答by sathish
回答by Pesse
$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase');
fwrite($fp, 'mice');
fclose($fp);
回答by lemon
$text = "Cats chase mice";
$filename = "somefile.txt";
$fh = fopen($filename, "a");
fwrite($fh, $text);
fclose($fh);
You use fwrite()
你用 fwrite()
回答by Tr?n Kh?i Hoàng
It is easy to write file :
写文件很容易:
$fp = fopen('lidn.txt', 'w');
fwrite($fp, 'Cats chase mice');
fclose($fp);
回答by starkeen
I use the following code to write files on my web directory.
我使用以下代码在我的 Web 目录中写入文件。
write_file.html
写文件.html
<form action="file.php"method="post">
<textarea name="code">Code goes here</textarea>
<input type="submit"value="submit">
</form>
write_file.php
写文件.php
<?php
// strip slashes before putting the form data into target file
$cd = stripslashes($_POST['code']);
// Show the msg, if the code string is empty
if (empty($cd))
echo "Nothing to write";
// if the code string is not empty then open the target file and put form data in it
else
{
$file = fopen("demo.php", "w");
echo fwrite($file, $cd);
// show a success msg
echo "data successfully entered";
fclose($file);
}
?>
This is a working script. be sure to change the url in form actionand the target file in fopen()function if you want to use it on your site.
这是一个工作脚本。如果您想在您的网站上使用它,请务必更改表单操作中的 url和fopen()函数中的目标文件。
Good luck.
祝你好运。
回答by mallikarjun S.K.
In order to write to a file in PHPyou need to go through the following steps:
为了写入文件,PHP您需要执行以下步骤:
Open the file
Write to the file
Close the file
$select = "data what we trying to store in a file"; $file = fopen("/var/www/htdocs/folder/test.txt", "a"); fwrite($file , $select->__toString()); fclose($file );
打开文件
写入文件
关闭文件
$select = "data what we trying to store in a file"; $file = fopen("/var/www/htdocs/folder/test.txt", "a"); fwrite($file , $select->__toString()); fclose($file );
回答by user10603371
Here are the steps:
以下是步骤:
- Open the file
- Write to the file
Close the file
$select = "data what we trying to store in a file"; $file = fopen("/var/www/htdocs/folder/test.txt", "w"); fwrite($file, $select->__toString()); fclose($file);
- 打开文件
- 写入文件
关闭文件
$select = "data what we trying to store in a file"; $file = fopen("/var/www/htdocs/folder/test.txt", "w"); fwrite($file, $select->__toString()); fclose($file);
回答by A.D.
fwrite()is a smidgen faster and file_put_contents()is just a wrapper around those three methods anyway, so you would lose the overhead.
Article
fwrite()是一个更快的速度,file_put_contents()无论如何只是这三种方法的包装器,所以你会失去开销。
文章
file_put_contents(file,data,mode,context):
file_put_contents(文件,数据,模式,上下文):
The file_put_contentswrites a string to a file.
将file_put_contents一个字符串写入文件。
This function follows these rules when accessing a file.If FILE_USE_INCLUDE_PATH is set, check the include path for a copy of filenameCreate the file if it does not exist then Open the file and Lock the file if LOCK_EX is set and If FILE_APPEND is set, move to the end of the file. Otherwise, clear the file content Write the data into the file and Close the file and release any locks. This function returns the number of the character written into the file on success, or FALSE on failure.
此函数在访问文件时遵循这些规则。如果设置了 FILE_USE_INCLUDE_PATH,请检查文件名副本的包含路径 如果文件不存在则创建文件 如果设置了 LOCK_EX 并且设置了 FILE_APPEND,则打开文件并锁定文件,移动到文件末尾。否则,清除文件内容 将数据写入文件并关闭文件并释放所有锁定。此函数在成功时返回写入文件的字符数,失败时返回 FALSE。
fwrite(file,string,length):
fwrite(文件,字符串,长度):
The fwritewrites to an open file.The function will stop at the end of the file or when it reaches the specified length,
whichever comes first.This function returns the number of bytes written or FALSE on failure.
在fwrite一个开放的文件。该函数将停止在文件末尾或当它达到指定的长度,以先到者为准first.This函数返回失败写入的字节或FALSE的数量。

