2012-09-03 98 views
0

我在我的頁面上有一個刪除按鈕,刪除按鈕必須刪除我的數據庫中的某個條目(不是太難),它必須刪除保存刪除按鈕的文件所在的整個文件夾(也可以),但我也希望它刪除另一個文件夾放在其他地方,我不知道該怎麼做。使用編輯文件路徑PHP

dirname(__FILE__); 

我能夠得到文件路徑,其中包含刪除按鈕的文件所在的位置。這導致在此:

mywebsite.nl/subdomains/dongen/httpdocs/s-gravenmoer/aandrijvingenenbesturingen/logo4life 

,我也想刪除的文件路徑是相當similair,但有一點不同。最後3個文件夾是一個變量,所以它們的長度(以字符表示)總是不同的。然而,倒數第二個文件夾已被從該文件路徑刪除,所以這仍然是:

mywebsite.nl/subdomains/dongen/httpdocs/s-gravenmoer/logo4life 

有沒有辦法用PHP來做到這一點?使用substr或者類似的東西?

謝謝!

回答

2

我想這應該做的伎倆:「水珠」

$folderToRemove = preg_replace('#^(.*)/(.*?)/(.*?)$#', "$1/$3", dirname(__FILE__)); 
+0

謝謝,這個伎倆! – user1555076

1

您可以嘗試使用在www.php.net/glob

詳細信息您可以試試:

$files = glob('subdomains/dongen/httpdocs/*/logo4life'); 
foreach ($files as $file) { 
    unlink($file); // note that this is very dangerous though, you may end up deleting a lot of files 
} 
+0

感謝您的幫助,我去了,採取了不太危險的方法;) – user1555076

0

你不需要任何幻想,你猜到了,一個簡單的str_replace函數將做到這一點: -

$file = 'mywebsite.nl/subdomains/dongen/httpdocs/s-gravenmoer/aandrijvingenenbesturingen/logo4life'; 
var_dump(str_replace('aandrijvingenenbesturingen/', '', $file)); 

輸出: -

string 'mywebsite.nl/subdomains/dongen/httpdocs/s-gravenmoer/logo4life' (length=62) 
+0

不幸的是,這不會工作,因爲最後3個文件夾是可變的,aandrijvingenenbesturingen並不總是隻是.. – user1555076

0
$path = "mywebsite.nl/subdomains/dongen/httpdocs/s-gravenmoer/aandrijvingenenbesturingen/logo4life"; 
$regex = "/(.*\/).*\/(.*)/"; 
$matches = array(); 
preg_match($regex, $path, $matches); 
// var_dump($matches); 
$new_path = $matches[1].$matches[2]; 
echo $new_path; 

上面的代碼使用s preg_match用於匹配字符串中的正則表達式。