1

c:\wamp\www\ 内にmywebsiteという名前の php Web アプリケーションがあります。このフォルダーは、featured/fashionWeek/database/Mysql.phpファイルで構成されます。dirname( FILE ) 関数を使用してルートからフォルダーのパスを取得し、ライブ サーバーの public_html フォルダー内の任意の場所にフォルダーを配置できるようにします。

私のローカルホストでは問題なく動作します。wamp\www\mywebsite\featured\fashionWeek\database\Mysql.php 内に次のコードがあります include(dirname( FILE ); は正常に動作し、次のパスを返します: C:/wamp/www/mywebsite/featured/fashionWeek/database

<?php  include(dirname(__FILE__).'/../config/Dbconfig.php') ;?>

しかし、ライブサーバーの public_html フォルダーの下に mywebsite フォルダーを配置したとき。次のエラーが発生します。

Warning: include(/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php) [function.include]: failed to open stream: No such file or directory in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3

Fatal error: include() [function.include]: Failed opening required '/home/websitename/public_html/featured/fashionWeek/database/../config/Dbconfig.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/websitename/public_html/featured/fashionWeek/database/Mysql.php on line 3
4

3 に答える 3

0

https://stackoverflow.com/a/2152320/1528331

それが役立つかどうかを確認してください。また、

dirname(__FILE__) is the same as __DIR__
于 2013-03-10T11:51:19.620 に答える
0

逆方向、つまり「..」に移動するには、dirname関数を使用します。

<?php
include(dirname(dirname(__FILE__)).'/config/Dbconfig.php');

動作するはずです。

もっと

最終的にこれらを管理するのは面倒なので、元のファイルで ROOT 定数を定義することをお勧めします。これには、プロジェクトのルートへの絶対パスが含まれます。とても簡単です。

defined('ROOT') or define('ROOT', dirname(__FILE__) . DIRECTORY_SEPARATOR);
于 2013-03-10T11:49:58.463 に答える
0

少したるんだですが、それはあなたを助けるかもしれません

if(!function_exists('getRootdir'))
{
function getRootdir($NFILE)
{
$filefitter="";
while(!file_exists($filefitter.$NFILE))
$filefitter="../".$filefitter;
return $filefitter;
}
}

使用する

like  $rootpath=getRootdir("Root identifier folder / php file");
于 2013-03-10T12:13:21.493 に答える