2017-07-04 82 views
1

我通讀了關於該主題的所有內容,但沒有爲我工作。我運行PHP 5.3.29。僅從根開始工作

我有這樣的PHP代碼,從根的作品,而不是從文件夾:

// Fetch latest poster 
    $base_url = 'posters'; 
    $newest_mtime = 0; 
    if ($handle = opendir($base_url)) { 
    while (false !== ($latestFile = readdir($handle))) { 
     if (($latestFile != '.') && ($latestFile != '..')) { 
     $mtime = filemtime("$base_url/$latestFile"); 
     if ($mtime > $newest_mtime) { 
      $newest_mtime = $mtime; 
      $show_file = "$base_url/$latestFile"; 
     } 
     } 
    } 
    }     
    // Show latest poster 
    $show_name = basename(trim($show_file, ".jpg")); 
    echo ' 
    <a href="/' . $show_file . '"> 
    <img src="' . $show_file . '" class="jpg" width="150" alt="latest poster" title="' . $show_name . '"> 
    </a>'; 

我很想只需添加一個斜線拿到根目錄相對路徑,但是,這並不工作, PHP。

這是這個問題的部位:

http://flamencopeko.net(這裏也沒有問題)

http://flamencopeko.net/disco_pages/sundays.php(這其中有問題)

+1

'$ base_url = getcwd()。 '/海報';'? – Darren

+0

謝謝。這也給了我http://home/flamenco/public_html/posters/08-05-2017_01.jpg。 –

+0

哪個沒有解決問題。 –

回答

1

我想,你要開始爲$_SERVER['DOCUMENT_ROOT']

1

將您的$ base_url更改爲

$base_url = $_SERVER['DOCUMENT_ROOT'].'/posters'; 

爲了得到一個PHP項目的根目錄路徑:

對於PHP> = 5.3.0

使用:DIR

注:文件的目錄。如果在include中使用,則返回包含文件的目錄。這相當於dirname(FILE)。除非目錄名是根目錄,否則該目錄名沒有結尾斜槓。

對於PHP 5.3.0 <

使用:目錄名(FILE)或真實路徑(目錄名(FILE))

還是以最常見的爲獲取服務器文檔根目錄下的項目所在:

$ _ SERVER [ 'DOCUMENT_ROOT']或filter_input(INPUT_SERVER, 'DOCUMENT_ROOT')

+0

Thanks. I do use include, or rather require, here yes. Like this: 使用 $ base_url = $ _SERVER ['DOCUMENT_ROOT']。'/ posters'; 給了我:http://home/flamenco/public_html/posters/08-05-2017_01.jpg 我不知道如何格式化這個評論。對於那個很抱歉。 –

1

的原因,你只能d顯示根目錄上的圖像與您的腳本無關。它必須處理這個網址的標籤,如下圖所示;

<img src='/posters/NAME_OF_IMG.jpg' ... /> 

這會解決的東西,如:http://www.yourwebsite.com/posters/NAME_OF_IMG.jpg

子迪爾 這會解決的東西,如:http://www.yourwebsite.com/disco_pages/posters/NAME_OF_IMG.jpg - 這沒有按」 t存在。

你需要做的是修改代碼來生成$show_file網址是這樣的:

$base_url = getcwd() . 'posters'; 
$newest_mtime = 0; 
if ($handle = opendir($base_url)) { 
while (false !== ($latestFile = readdir($handle))) { 
    if (($latestFile != '.') && ($latestFile != '..')) { 
     $mtime = filemtime("$base_url/$latestFile"); 
     if ($mtime > $newest_mtime) { 
      $newest_mtime = $mtime; 
      $show_file = "http://www.yourdomain.com/posters/" . $latestFile; 
     } 
    } 
    } 
} 

另外,如果需要,您可以使用$_SERVER['DOCUMENT_ROOT'],而不是getcwd()

這應該適用於您嘗試的每個目錄。你必須明白你的代碼在做什麼來理解它爲什麼不能按你的想法工作。

  1. 您正在遍歷文件級別的目錄以查找最新的文件,對嗎?因此,您需要文件的路徑來檢查這些方面的絕對
  2. 您正在提供絕對路徑<img ...標記,這將不起作用。您的網站期待相對路徑(http://www.yourwebsite.com/posters/THE_IMAGE_NAME.jpg)。
0

非常感謝。

從根,導致:

警告:執行opendir(/家/弗拉門戈/ public_htmlposters)[function.opendir]:未能打開目錄:在/ home /弗拉門戈/的public_html/left_test沒有這樣的文件或目錄.PHP第40行

所以我改變

base_url = getcwd() . 'posters'; 

base_url = getcwd() . '/posters'; 

這顯示了海報,但它鏈接到:

http://flamencopeko.net/http://flamencopeko.net/posters/08-05-2017_01.jpg

從子文件夾,它會導致:

警告:執行opendir(/家/弗拉門戈/的public_html/disco_pages /海報)功能。 opendir]:無法打開目錄:第12行中的/home/flamenco/public_html/disco_pages/left_test2.php中沒有這樣的文件或目錄

我能夠寫出一個混亂的變通辦法,

$path = getcwd(); 
if ($path == '/home/flamenco/public_html') { 
    $base_url = 'posters'; 
    $newest_mtime = 0; 
    if ($handle = opendir($base_url)) { 
    while (false !== ($latestFile = readdir($handle))) { 
     if (($latestFile != '.') && ($latestFile != '..')) { 
     $mtime = filemtime("$base_url/$latestFile"); 
     if ($mtime > $newest_mtime) { 
      $newest_mtime = $mtime; 
      $show_file = "$base_url/$latestFile"; 
     } 
     } 
    } 
    } 
    // Show latest poster 
    $show_name = basename(trim($show_file, ".jpg")); 
    echo ' 
    <a href="/' . $show_file . '"> 
    <img src="' . $show_file . '" class="jpg" width="150" alt="latest poster" title="' . $show_name . '"> 
    </a>'; 
    } 
    else 
    { 
    $base_url = '../posters'; 
    $newest_mtime = 0; 
    if ($handle = opendir($base_url)) { 
    while (false !== ($latestFile = readdir($handle))) { 
     if (($latestFile != '.') && ($latestFile != '..')) { 
     $mtime = filemtime("$base_url/$latestFile"); 
     if ($mtime > $newest_mtime) { 
      $newest_mtime = $mtime; 
      $show_file = "$base_url/$latestFile"; 
     } 
     } 
    } 
    } 
    // Show latest poster 
    $show_name = basename(trim($show_file, ".jpg")); 
    echo ' 
    <a href="/' . $show_file . '"> 
    <img src="' . $show_file . '" class="jpg" width="150" alt="latest poster" title="' . $show_name . '"> 
    </a>'; 
    }