Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QUESTION write to file doesn't like absolute path?
#1
I'm curious and maybe I've just missed something but when I try to use get_theme_url as part of the file path I can't write to file, however when I use a relative path then all works fine.

It's Friday, it been a long week so maybe I'm just missing something stupid. Just wondered if anyone had an idea.

Absolute path  - Used within a file in the theme folder:
Code:
$dataStr = "testing 123";

$fpath = get_theme_url(false)."data33.txt";
                           
file_put_contents($fpath,$dataStr);

And yet if I use the relative path without including the function 'get_theme_url()', all works fine.

Code:
file_put_contents("theme/helpdesks/data33.txt",$dataStr);
Reply
#2
get_theme_url(false) does not return a filepath, but an URL (http://....)

You have to use GSTHEMESPATH (and optionally global var $TEMPLATE for theme folder), e.g.:
Code:
<?php
global $TEMPLATE;
echo 'Theme filepath: ', GSTHEMESPATH.$TEMPLATE;
?>
Reply
#3
Superb.

Strange that I can use get_theme_url to read a file from a directory but it doesn't work when writing.
I'll use the method you suggested from now on

Thanks Carlos,
much appreciated
Reply
#4
You can read files both from filepaths and URLs with file_get_contents(), but file_put_contents() can only write to filepaths.
Reply
#5
Thanks again
Reply




Users browsing this thread: 1 Guest(s)