0

尝试将电子邮件中的附件保存到我的服务器中,到目前为止我的脚本工作正常,但是它将文件保存在我的 wp 根文件夹中。

foreach ($attachments as $key => $attachment) {
    $name = $attachment['name'];
    $contents = $attachment['attachment'];
    file_put_contents($name, $contents);
}

如何将文件保存到不同的文件夹中?

尝试使用此代码但无法正常工作。

foreach ($attachments as $key => $attachment) {
    $name = $attachment['name'];
    $contents = $attachment['attachment'];
    file_put_contents(get_stylesheet_directory_uri() . '/email_attachments/' . $name, $contents);
}

任何想法?

4

3 回答 3

0

您正确添加了路径,只是 get_stylesheet_directory_uri() 用于网络路径: https ://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri

尝试类似 get_home_path() : https ://codex.wordpress.org/Function_Reference/get_home_path

干杯!

于 2015-05-27T17:00:15.223 回答
0

对于未来参考这里如何找到路径:

foreach ($attachments as $key => $attachment) {
        $name = $attachment['name'];
        $contents = $attachment['attachment'];
        file_put_contents(STYLESHEETPATH . '/email_attachments/' . $name, $contents);
    }
于 2015-05-27T17:35:51.893 回答
0

您正确添加了路径,只是 get_stylesheet_directory_uri() 用于网络路径: https ://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri

尝试类似 get_home_path() : https ://codex.wordpress.org/Function_Reference/get_home_path

这是不正确的。您需要使用 get_stylesheet_directory() 函数而不是 get_stylesheet_directory_uri()。

echo file_put_contents(get_stylesheet_directory().'/file.txt','test');

应该打印 4

于 2020-01-26T13:51:29.103 回答