I've been trying to delete a .json file with php on my site, and can't get it to work. It seems I can't get the path to the file right, or it's invisible. So I thought I'd check with file_exists(), and no matter what I try, I cannot get the system to see a file that is on my server.
the json folder exists in this path /mytheme/assets/js/geofences/1069.json
The php is being run from this path /mytheme/inc/filelookingforjson.php
I have tried "../assets/js.." to send path pointer up one level using relative path...
I have tried absolute filepaths. and it's behaving like the file isn't there. Is there some htaccess or some other thing i need to do on a wordpress site? Permissions on the geofences folder are set to read, writeable etc... the code below sends me an email saying there is no file. I'm sure I have the path wrong, if i can get this correct than i can work on the unlink... thank you for any input.
$file = '/wp-content/themes/mytheme/assets/js/geofences/1069.json';
if(file_exists($file)){
mail("me@email.com","file exists","file name is there");
} else {me@email.com","file does not exist found","file name is
not there");
}
If the folder structure is:
mytheme/
inc/
filelookingforjson.php
assets/
js/
geofences/
1069.json
then the path to the json file from the PHP file would be:
../assets/js/geofences/1069.json
However, considering that your PHP file most likely gets included by other PHP files, the path will be relative from the first accessed PHP file.
Imagine this file structure:
index.php
folder/
foo.php
subfolder/
bar.php
assets/
hello.txt
Let's say that you want to read the file hello.txt
in the file bar.php
, then the relative path to hello.txt
would be ../../assets/hello.txt
. This would work if you run the file bar.php
directly.
The "gotcha" is when you're not accessing bar.php
directly.
Let's now say that index.php
includes foo.php
and foo.php
includes bar.php
.
If you run index.php
, the relative path in bar.php
won't work anymore since it isn't relative from the first accessed PHP file in the include chain, which is index.php
.
This does make sense if you think of include
and require
as a "copy/paste". PHP "copys" the code from the included file and "pastes" it into the file that does the including.
Note: this is of course a simplification of how it actually works
The magic constant __DIR__
.
If you add that before your relative path, PHP will replace it with the absolute path to the file it was written in.
The code:
$file = __DIR__ . '/../../assets/hello.txt';
will be read by PHP as:
$file = '/folder/subfolder/../../assets/hello.txt';
Now it won't matter what file includes bar.php
since the path will stay absolute.
The same thing goes when you're trying to include files using relative paths.
On a real server, the absolute path would probably look something like /path/from/the/file-systems/root/folder/assets/hello.txt
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.