2012-03-23 71 views

回答

8

您應該使用文件系統上文件的絕對路徑或相對路徑。

<?php 

$absolute_path = '/full/path/to/filename.php'; 
$relative_path = '../posts/filename.php'; 

// use one of $absolute_path or $relative_path in fopen() 

?> 
+0

謝謝,工作。 – user1142872 2012-03-23 10:43:41

+0

@ user1142872您應該將其標記爲答案。 – 2017-01-29 05:23:17

3

您可以使用相對路徑從該文件的父目錄內的目錄打開文件。

例如,從/foo/y/foo/x的相對路徑是../x。正如你可能知道的那樣,雙點意味着「上面的目錄」。所以,/foo/../foo/bar/foo/bar相同。通常使用絕對路徑更安全,因爲相對路徑可能取決於進程當前目錄。 你應該從來沒有硬編碼絕對路徑 - 計算它。

所以,這應該從管理/ upload.php的打開物品/ thefile.php:

// path to admin/ 
$this_dir = dirname(__FILE__); 

// admin's parent dir path can be represented by admin/.. 
$parent_dir = realpath($this_dir . '/..'); 

// concatenate the target path from the parent dir path 
$target_path = $parent_dir . '/articles/' . $theFile . '.php'; 

// open the file 
$ourFileHandle = fopen($target_path, 'w') or die("can't open file"); 

你真的應該熟悉paths

+0

感謝您的高級信息! – user1142872 2012-03-23 10:43:32

+0

隨時!請不要忘記[關閉此問題](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – jpic 2012-03-23 10:49:15

2

您總是可以通過$ _SERVER ['DOCUMENT_ROOT']訪問http://www.yourdomain.com/的本地路徑表示。

<?php 
$f = fopen($_SERVER['DOCUMENT_ROOT'] . '/posts/filename.php'); 
?> 
相關問題