2012-02-03 132 views
2

我想使用PHP函數opendir()在linux文件系統上打開一個目錄。包含空格的PHP Opendir()路徑

該路徑可能包含空格或其他特殊字符。

當我試圖打開目錄,我得到:

Warning: opendir(/home/user/_demo/test/salamis\ test): failed to open dir: No such file or directory 

我首先嚐試使用,以取代\(斜線空間)空格字符:

str_replace(" ","\ ","salamis test"); 

但不幸的是不工作。有什麼建議麼?

+2

你試過'opendir(「/ home/user/_demo/test/salamis test」);'? – 2012-02-03 23:09:17

回答

0

將引用路徑參數(雙引號或單引號)括起來。

例如

opendir('/home/user/_demo/test/salamis test'); 
opendir("/home/user/_demo/test/salamis test"); 

$path = '/home/user/_demo/test/salamis test'; 
opendir($path); 

$path = "/home/user/_demo/test/salamis test"; 
opendir($path);  
+0

如果他沒有用引號括住這條路徑,他會得到一個解析錯誤。該腳本甚至不會運行。 – webbiedave 2012-02-03 23:15:34

+0

@webbiedave我猜測他在空格之前包含'\\'。 – ThinkingMonkey 2012-02-03 23:16:48

+0

Necro評論,但這不是解決方案。它不能解決這個問題。 – Niddro 2015-09-24 07:11:59

0

今天同樣的問題。我發現添加引號並沒有幫助。我意識到共享文件夾是用不同的用戶帳戶登錄的,在用正確的uid & gid(apache)卸載和更新/ etc/fstab之後,我能夠在共享上裝載和處理文件。

This answer也有幫助。