Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将电子邮件存储在文本文件中,每行一封_Php - Fatal编程技术网

Php 将电子邮件存储在文本文件中,每行一封

Php 将电子邮件存储在文本文件中,每行一封,php,Php,当用户进入“我的网站”页面时,此代码片段会呈现一封用户电子邮件: $email=$userprofile->email; 。。。你可以这样回应它: <?php echo $email;?> 但如何将它存储在我选择的目录或文件夹中的文本文件中,每行一封电子邮件?谢谢 我试过了,但没用 <?php $file = 'http://mydomain.com/file/email.txt'; // Open the file to get existing content

当用户进入“我的网站”页面时,此代码片段会呈现一封用户电子邮件:

$email=$userprofile->email;
。。。你可以这样回应它:

<?php echo $email;?>

但如何将它存储在我选择的目录或文件夹中的文本文件中,每行一封电子邮件?谢谢

我试过了,但没用

<?php
$file = 'http://mydomain.com/file/email.txt';
// Open the file to get existing content

// Write the contents back to the file
file_put_contents($file, $email);
?>

您无法使用域名打开要写入的文件。您必须在本地文件系统上打开它。像这样的东西可能适合你:

$myFile = "/local/path/to/email.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$email=$userprofile->email."\n";

fwrite($fh, $email);
fclose($fh);

您可以使用php函数

您需要使用 试试这个:

$fh = fopen('myfile.txt', 'a+');
fwrite($fh, $new_email . "\n");
fclose($fh);

正如乔所提到的,注意fopen的第二个参数一个+'将把指针放在文件的末尾。如果需要的话,这个加号可以让你从中读取信息。

首先,你不能使用url作为文件(确实可以,但你需要在php中启用它)。 那么,假设您希望将电子邮件存储在本地文件中:

$file = "myfile.txt"
file_put_contents()将覆盖文件(如果存在),因此您需要在变量中创建电子邮件列表,最后将其写入文件:

[in an emails loop]
$my_list += $email . '\n'
最后:

file_put_contents($file, $emails_list);

file_get_contents()不适合读取页面内容吗?我知道我用它来读取外部站点的身份验证响应……另一种方法是查看curl函数和输出缓冲。

这里的键是“a+”,意思是在末尾附加内容(而不是替换)。我有php ini访问权限。那么我如何使用url作为文件呢?上面的答案是正确的,但是您将为每封电子邮件打开、写入和关闭文件,最好只执行一次打开、写入和关闭操作,就像文件内容一样()。要使用URL,可以执行$handle=fopen(“,”w”);打开外部文件。