无法通过file_exists()获得结果 [英] Can't get result with file_exists()

查看:167
本文介绍了无法通过file_exists()获得结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在我的网站上使用php删除.json文件,但无法使其正常工作.看来我找不到正确的文件路径,或者它是不可见的.因此,我认为我应该使用file_exists()进行检查,无论尝试什么,我都无法使系统看到服务器上的文件.

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.

此路径中存在json文件夹 /mytheme/assets/js/geofences/1069.json

the json folder exists in this path /mytheme/assets/js/geofences/1069.json

正在从此路径运行php /mytheme/inc/filelookingforjson.php

The php is being run from this path /mytheme/inc/filelookingforjson.php

我已经尝试过"../assets/js .."使用相对路径将路径指针向上发送一级.

I have tried "../assets/js.." to send path pointer up one level using relative path...

我尝试了绝对文件路径.并表现为文件不存在.我是否需要在wordpress网站上进行htaccess或其他操作? geofences文件夹上的权限设置为可读取,可写等.下面的代码向我发送了一封电子邮件,告诉我没有文件.我确定我的路径错误,如果我能正确解决这个问题,那么我可以进行取消链接的工作...谢谢您的任何输入.

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

推荐答案

问题

如果文件夹结构为:

The issue

If the folder structure is:

mytheme/
  inc/
    filelookingforjson.php
  assets/
    js/
      geofences/
        1069.json

然后从PHP文件到json文件的路径为:

then the path to the json file from the PHP file would be:

../assets/js/geofences/1069.json

但是,考虑到您的PHP文件很可能已被其他PHP文件包含,因此该路径将是从第一个访问的PHP文件开始的相对路径.

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

假设您要读取文件bar.php中的文件hello.txt,那么hello.txt的相对路径将是../../assets/hello.txt.如果直接运行文件bar.php,这将起作用.

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.

陷阱"是指您直接访问bar.php的情况.

The "gotcha" is when you're not accessing bar.php directly.

现在让我们说index.php包括foo.php,而foo.php包括bar.php.

Let's now say that index.php includes foo.php and foo.php includes bar.php.

如果运行index.php,则bar.php中的相对路径将不再起作用,因为它与包含链中第一个被访问的PHP文件index.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.

如果您将includerequire视为复制/粘贴",这确实有意义. 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

魔法常数__DIR__.

如果将其添加到相对路径之前,PHP会将其替换为写入该文件的绝对路径.

If you add that before your relative path, PHP will replace it with the absolute path to the file it was written in.

代码:

$file = __DIR__ . '/../../assets/hello.txt';

将被PHP读取为:

$file = '/folder/subfolder/../../assets/hello.txt'; 

现在,什么文件包含bar.php都无关紧要,因为路径将保持绝对.

Now it won't matter what file includes bar.phpsince the path will stay absolute.

当您尝试使用相对路径包含文件时,也会发生同样的事情.

The same thing goes when you're trying to include files using relative paths.

在真实服务器上,绝对路径可能类似于/path/from/the/file-systems/root/folder/assets/hello.txt.

On a real server, the absolute path would probably look something like /path/from/the/file-systems/root/folder/assets/hello.txt.

这篇关于无法通过file_exists()获得结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆