2014-02-21 95 views
0

我有一些在屏幕上輸出圖像的腳本,間隔爲5秒。 所有圖像位於一個名爲images/easter的目錄中。 顯示網頁的URL是http://'localhost'/index.php。我有一個變量:$directory = 'images/easter/PHP解析URL圖像目錄

我們希望用戶創建一個新的圖像目錄並上傳圖片(例如images/holiday)。 新圖像必須在url中進行簡單更改才能顯示。

http://'localhost'/index.php?holiday 

我認爲這可以通過PHP中的URL解析完成。但如何編寫腳本?

謝謝先進。

+0

你可以讓你的URL是這樣的http:// 'localhost' 的/指數。 php?q =假期,並使用$ _GET ['q']獲得您的參數ameter –

+0

axact是如何合成的?這將無法正常工作:$ directory ='images /'。((isset($ _ GET ['q'])); with http://localhost/harald/index.php?q = test – Quinox

回答

1

只是讓你的網址類似

http://localhost/index.php?dir=holiday 

或只是

http://localhost/index.php 

在腳本執行以下操作:

<?php 
$directory = 'images/'.((isset($_GET['dir'])) ? $_GET['dir'] : 'easter'); 

但一定要檢查抵禦輸入有效,因此無法輸出私人文件夾的內容。

如果你想要一個像

http://localhost/holiday 

的url你shold看看mod_rewrite

+0

使用你的例子。 太感謝了! – Quinox

0

使用$ _GET從URL檢索所需的值。儘管如此,你必須稍微調整一下。也許你想要類似http://'localhost'/index.php?dir=holiday。這樣,你可以在目錄上使用$ _GET。 PHP Form handling

0

感謝迄今爲止的答案。

我們不想編輯文件,所以更改必須位於拍攝照片的URL中。

我對PHP的知識非常有限。

0

您可以使用$ _GET參數。

網址:

http://'localhost'/index.php?q=holiday 

代碼:

$directory = 'images/'((isset($_GET['q']))?$_GET['q']:''); 

if (isset($_GET['q'])) 
    $directory = 'images/'.$_GET['q']; 
else 
    $directory = 'images';