GetSimple Support Forum
QUESTION write to file doesn't like absolute path? - Printable Version

+- GetSimple Support Forum (http://get-simple.info/forums)
+-- Forum: GetSimple (http://get-simple.info/forums/forumdisplay.php?fid=3)
+--- Forum: Developer Discussions (http://get-simple.info/forums/forumdisplay.php?fid=8)
+--- Thread: QUESTION write to file doesn't like absolute path? (/showthread.php?tid=10732)



write to file doesn't like absolute path? - craiga - 2019-02-22

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);



RE: write to file doesn't like absolute path? - Carlos - 2019-02-22

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;
?>



RE: write to file doesn't like absolute path? - craiga - 2019-02-23

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


RE: write to file doesn't like absolute path? - Carlos - 2019-02-23

You can read files both from filepaths and URLs with file_get_contents(), but file_put_contents() can only write to filepaths.


RE: write to file doesn't like absolute path? - craiga - 2019-02-23

Thanks again