首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >fwrite不会将\r\n放入我的文件中(对于来自post的文本中提供的数据)

fwrite不会将\r\n放入我的文件中(对于来自post的文本中提供的数据)
EN

Stack Overflow用户
提问于 2011-02-02 04:58:11
回答 3查看 621关注 0票数 0

我正在尝试制作一个功能,保存我的配置数据,以便自动发送电子邮件。

数据来自textareas so $_POST,并放入此函数中。

我的保存功能不是保存\r\n字符,因此不会换行。所以当我发送一封电子邮件时,完整的消息在1个大字符串中,没有换行…

如果我打开文件,它是用空格保存的,而不是全部在一行上。

代码语言:javascript
复制
if ($_POST['gdwemail_obj']!="") { $gdwEConfig['gdwemail_obj'] = $_POST['gdwemail_obj']; }
if ($_POST['gdwemail_mes']!="") { $gdwEConfig['gdwemail_mes'] = $_POST['gdwemail_mes']; }
saveEConfig($gdwEConfig);



function saveEConfig($post) {
    $gdw_conffile = 'components'.DS.'com_gdwformulaire'.DS.'included'.DS.'econfig.gdw.php';
    $gdw_confopen = fopen($gdw_conffile,'w+');
    $gdw_cfgput  = '<?php';
    $gdw_cfgput .= ' $gdwEconf = array(); ';

    $gdw_cfgput .= ' $gdwEconf[\'gdwemail\'] = "'.$post['gdwemail'].'"; ';
    $gdw_cfgput .= ' $gdwEconf[\'gdwemail_obj\'] = "'.$post['gdwemail_obj'].'"; ';
    $gdw_cfgput .= ' $gdwEconf[\'gdwemail_mes\'] = "'.$post['gdwemail_mes'].'"; ';

    $gdw_cfgput .= '?>';
    if (!fwrite($gdw_confopen, $gdw_cfgput)) {
        echo "<script> alert('ERROR: Can't save configuration file, please verify CHMOD access on \'administrator/components/com_ai/\' for write access.'); window.history.go(-1); </script>\n";
        exit;
    }    
    fclose($gdw_confopen);
}

输出如下所示:

代码语言:javascript
复制
    <?php $gdwEconf = array();  $gdwEconf['gdwemail'] = "j.robidas@geantduweb.ca";  $gdwEconf['gdwemail_obj'] = "Réservation pour le cours {courschoisi} a été reçus";  $gdwEconf['gdwemail_mes'] = "Cher {nomdemandeur},

Votre demande de réservation a été reçus avec succès.

Information pour votre sélection:
{courschoisi}
{dateheurechoisi}

Merci
L'équipe de Belle vie de chien"; ?>
EN

回答 3

Stack Overflow用户

发布于 2011-02-02 05:04:40

因为您实际上没有将\r\n包含在$gdw_cfgput .=中...台词。

票数 2
EN

Stack Overflow用户

发布于 2011-02-02 05:04:55

您没有要求它在您的$gdw_cfgput字符串中插入任何换行符。

代码语言:javascript
复制
$gdw_cfgput  = '<?php';
$gdw_cfgput .= "\r\n";
$gdw_cfgput .= ' $gdwEconf = array(); ';

请注意双引号,它告诉PHP将\r\n解释为换行符。

票数 1
EN

Stack Overflow用户

发布于 2011-02-02 05:07:29

您的文件不包含换行符,因为您没有指定任何换行符。您组装$gdw_cfgput,但不附加任何. "\r\n";。如果你想添加它,你可以使用. PHP_EOL; btw。

但是,如果您使用var_export()生成输出文件,则是一种更好的选择。看起来你有一个相干的数组,var_export会简化它的输出。

代码语言:javascript
复制
$config = var_export($_POST, true);
fwrite($f, "<?php\n\n\$gdwEconf = $config;\n?>");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4867856

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档