绝对文件路径

发布于 2024-10-20 09:40:46 字数 2251 浏览 3 评论 0原文

这个问题仍然没有解决

我正在关注的视频PHP教程正在构建一个名为initialize.php的文件,其中它使用PHP预定义的常量Directory_Separator,然后定义一个site_root。 site_root 是 PHP 查找所需文件的绝对文件路径(不是网络服务器路径)。他给了我们以下代码

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

    defined('SITE_ROOT') ? null :
    define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');

我假设他计算机上的文件路径是 root/users/kevin/sites/photogallery

我不是在我的计算机上构建该网站,而是直接在线构建该网站。我不知道要插入哪个文件路径。

正如他强调的那样,这不是网络服务器路径,而是文件系统路径,我该放什么。就这样的域名。

define('SITE_ROOT', DS. 'www.example.com');

他不想要网络服务器路径,但文件位于网上?所以我不明白。

更新

视频教程使用了以下代码

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null :
define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');
defined('LIB_PATH') ? null : define('LIB_PATH',SITE_ROOT.DS.'includes');

我使用了此代码:

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));
defined('LIB_PATH') ? null : define('LIB_PATH',SITE_ROOT.DS.'includes');

我收到此错误消息

require_once(/hsphere/local/home/c263430/quoralist.com/includes/includes/config.php): 无法打开流:没有这样的文件或 目录在 /hsphere/local/home/c263430/quoralist.com/includes/initialize.php 第 11 行致命错误: require_once(): 打开失败 必需的 '/hsphere/local/home/c263430/quoralist.com/includes/includes/config.php' (include_path='.:/hsphere/shared/apache/libexec/php5ext/php/') 在 /hsphere/local/home/c263430/quoralist.com/includes/initialize.php 第 11 行

更新 使用experimentX进行以下3次编辑,以及以下

define('SITE_ROOT', DS.'hsphere'.DS.'local'.DS.'home'.DS.'c263430'.DS.'quoralist.com');

我总是收到错误消息

警告: require_once(LIB_PATH/config.php): 无法打开流:没有这样的文件或 目录在 /hsphere/local/home/c263430/quoralist.com/includes/initialize.php 第 11 行致命错误: require_once(): 打开失败 需要“LIB_PATH/config.php” (include_path='.:/hsphere/shared/apache/libexec/php5ext/php/') 在 /hsphere/local/home/c263430/quoralist.com/includes/initialize.php 第 11 行

此外,

This problem is still not solved

A video PHP tutorial I am following is building a file called initialize.php in which it is using the PHP pre-defined constant Directory_Separator and then defining a site_root. The site_root is the absolute file path (not the webserver path) for PHP to locate the files it needs. He gave us the following code

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

    defined('SITE_ROOT') ? null :
    define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');

I am assuming the file path on his computer is root/users/kevin/sites/photogallery

I am not building the site on my computer, but rather directly online. I don't know what file path to insert instead.

As he emphasized that it's not the webserver path, but rather the file system path, what do I put instead. Just the domain name like this.

define('SITE_ROOT', DS. 'www.example.com');

He doesn't want the webserver path but the files are located online? so I don't get it.

UPDATE

The video tutorial used the following code

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null :
define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');
defined('LIB_PATH') ? null : define('LIB_PATH',SITE_ROOT.DS.'includes');

I used this code:

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));
defined('LIB_PATH') ? null : define('LIB_PATH',SITE_ROOT.DS.'includes');

I got this error message

require_once(/hsphere/local/home/c263430/quoralist.com/includes/includes/config.php):
failed to open stream: No such file or
directory in
/hsphere/local/home/c263430/quoralist.com/includes/initialize.php
on line 11 Fatal error:
require_once(): Failed opening
required
'/hsphere/local/home/c263430/quoralist.com/includes/includes/config.php'
(include_path='.:/hsphere/shared/apache/libexec/php5ext/php/')
in
/hsphere/local/home/c263430/quoralist.com/includes/initialize.php
on line 11

UPDATE
using the 3 edits below by experimentX, as well as the following

define('SITE_ROOT', DS.'hsphere'.DS.'local'.DS.'home'.DS.'c263430'.DS.'quoralist.com');

I always got the error message

Warning:
require_once(LIB_PATH/config.php):
failed to open stream: No such file or
directory in
/hsphere/local/home/c263430/quoralist.com/includes/initialize.php
on line 11 Fatal error:
require_once(): Failed opening
required 'LIB_PATH/config.php'
(include_path='.:/hsphere/shared/apache/libexec/php5ext/php/')
in
/hsphere/local/home/c263430/quoralist.com/includes/initialize.php
on line 11

Also,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见 2024-10-27 09:40:46

不如

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));

echo SITE_ROOT;

使用 define('SITE_ROOT', 'www.domain.com') 来代替 define('SITE_ROOT', realpath(dirname(__FILE__))

www.domain.com 是(您网站的)基本 url,而 realpath(dirname(__FILE__)) 是定义 FILE 的绝对文件路径。您可以为您的网站设置一个根文件夹,

例如:您不能执行 unlink('www.example.com\img1.jpg') 来删除 img1

您应该这样做。 do unlink(SITE_ROOT.DS.'img1.jpg');

对于 move_uploaded_file 或任何其他目录或文件功能,即您的文件正在被服务器计算机移动和删除。 需要绝对文件路径

,您可以导航到服务器上的文件夹并访问文件

(因为 php 脚本是在服务器中执行的),因此

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null : 
    define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');

。 -root 在 Mac 上是 Users/kevin/Sites/,而 Users/kevin/Sites/photogallery 是网站根目录。

导师知道这一点,所以他才这样做。但是当我们的项目上传到网络服务器时我们可能不知道这一点,所以

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));

这是最合适的。

至于DS,它只是一个CONSTANT(DIRECTORY_SEPARATOR),定义它是因为DIRECTORY_SEPARATOR写起来太长。即

  SITE_ROOT'.DIRECTORY_SEPARATOR.'Users'.DIRECTORY_SEPARATOR.'kevin'.DIRECTORY_SEPARATOR.'Sites'.DIRECTORY_SEPARATOR.'photo_gallery'

,因此,在定义 SITE ROOT 之前先定义它,

defined('DS')? null: define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));

因为本教程是在 MAC 上完成的,DIRECTORY_SEPARATOR/。示例是在 localhost 上完成的,导师知道他的网络服务器的绝对文件路径,因此他正在执行

[错误:更新]

您收到此错误是因为在 < code>includes/initialize.php 假设您的 SITE_ROOTyourwebroot/yourproject/includes,它必须是 yourwebroot/yourproject/includes< /代码>。

有两种方法,一种方法是

initialize.php 放在 yourwebroot/yourproject

,另一种方法是

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)).DS."..".DS);

OR

defined('SITE_ROOT')? null: define('SITE_ROOT', DS.'..'.DS.realpath(dirname(__FILE__)));

(我不确定,但其中一个应该有效)

另外试试这个

defined('SITE_ROOT')? null: define('SITE_ROOT', '/../'.realpath(dirname(__FILE__)));

How about

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));

echo SITE_ROOT;

Instead of define('SITE_ROOT', 'www.domain.com') you should define('SITE_ROOT', realpath(dirname(__FILE__)).

www.domain.com is the base url (of your site) while realpath(dirname(__FILE__)) is the absolute file path of FILE where it is defined. Using this path, you can set up a ROOT FOLDER for you site.

For example: you cannnot do unlink('www.example.com\img1.jpg') to delete img1

You should do unlink(SITE_ROOT.DS.'img1.jpg');

Similarly for move_uploaded_file or any other directory or file function. i.e. your files are being moved and deleted by server computer(as php script is executed in server), so absolute file path is required.

Using this absolute path, you can navigate to the folder and access files on server.

[UPDATE]

defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null : 
    define ('SITE_ROOT', DS.'Users'.DS.'kevin'.DS.'Sites'.DS.'photo_gallery');

Though I don't own mac, the web-root is Users/kevin/Sites/ on Mac and the Users/kevin/Sites/photogallery is the site root.

The tutor knows this so he is doing this. But we might not know this when our project is uploaded in webserver, so

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));

Would be the most appropriate.

And as to DS, it is just a CONSTANT (DIRECTORY_SEPARATOR), which is defined because DIRECTORY_SEPARATOR would be too long to write. i.e.

  SITE_ROOT'.DIRECTORY_SEPARATOR.'Users'.DIRECTORY_SEPARATOR.'kevin'.DIRECTORY_SEPARATOR.'Sites'.DIRECTORY_SEPARATOR.'photo_gallery'

So, define it before you define SITE ROOT

defined('DS')? null: define('DS', DIRECTORY_SEPARATOR);
defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)));

As the tutorial is done on MAC DIRECTORY_SEPARATOR is /. And the sample is done on localhost, the tutor knows the absolute file path of his webserver so he is doing

[ERROR: UPDATE]

You got this error because defining this on includes/initialize.php assumes that your your SITE_ROOT is yourwebroot/yourproject/includes where it must have been yourwebroot/yourproject/includes.

There are two ways, one way is

to put initialize.php in yourwebroot/yourproject

And the other way is

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)).DS."..".DS);

OR

defined('SITE_ROOT')? null: define('SITE_ROOT', DS.'..'.DS.realpath(dirname(__FILE__)));

(I am not sure but one of them should work)

Also try this

defined('SITE_ROOT')? null: define('SITE_ROOT', '/../'.realpath(dirname(__FILE__)));
〗斷ホ乔殘χμё〖 2024-10-27 09:40:46

答案

抱歉,我对 IMAGES not displayed in Photograph.php,只需删除 DS

public function image_path() {
  return $this->upload_dir.$this->filename;
}

工作行的

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)).DS."..".DS);

是:谢谢!

sorry, i have the answer for IMAGES not displaying

in photograph.php, just remove DS

public function image_path() {
  return $this->upload_dir.$this->filename;
}

working line is:

defined('SITE_ROOT')? null: define('SITE_ROOT', realpath(dirname(__FILE__)).DS."..".DS);

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文