0

Trying to save an attachment from email into my server, the script I have so far works fine however, it saves the file in my wp root folder.

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

How can I save the file into a different folder?

trying with this code but not working.

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

Any Idea?

Federkun
  • 36,084
  • 8
  • 78
  • 90
SNos
  • 3,430
  • 5
  • 42
  • 92

3 Answers3

0

You're adding the path correctly, it's just that get_stylesheet_directory_uri() is for the web path : https://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri

Try something like get_home_path() : https://codex.wordpress.org/Function_Reference/get_home_path

Cheers!

Mikel Bitson
  • 3,583
  • 1
  • 18
  • 23
  • using "get_home_path() . '/email_attachments/' . $name" just stops my script. – SNos May 27 '15 at 17:14
  • Can you turn on error reporting and get the error that is stopping your script? It's possible that it's a permission issue or syntax error at this point. https://codex.wordpress.org/Debugging_in_WordPress#PHP_Errors.2C_Warnings.2C_and_Notices – Mikel Bitson May 27 '15 at 17:21
  • Would love too seriously however, i dont know why my wp does not debug. I have changed debug from false to true in the wp-config but still cannot output php errors – SNos May 27 '15 at 17:23
0

For Future Reference here how to find the path:

foreach ($attachments as $key => $attachment) {
        $name = $attachment['name'];
        $contents = $attachment['attachment'];
        file_put_contents(STYLESHEETPATH . '/email_attachments/' . $name, $contents);
    }
SNos
  • 3,430
  • 5
  • 42
  • 92
0

You're adding the path correctly, it's just that get_stylesheet_directory_uri() is for the web path : https://codex.wordpress.org/Function_Reference/get_stylesheet_directory_uri

Try something like get_home_path() : https://codex.wordpress.org/Function_Reference/get_home_path

That's incorrect. You need to use the get_stylesheet_directory() function instead of get_stylesheet_directory_uri().

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

Should print 4