2016-03-09 78 views
0

我試圖打開連接到不同用戶名的目錄中的最新文件。然而,一些用戶名沒有上傳目錄,我想跳過這些用戶或創建目錄無論哪一個。這是我現在的代碼。如果目錄不存在,跳過或製作目錄php

<?php $path = base_url(''.$value['username']) . '/' . 'uploaded';) 

$latest_ctime = 0; 
$latest_filename = '';  

$d = dir($path); 

while (false !== ($entry = $d->read())) { 
    $filepath = "{$path}/{$entry}"; 
    // could do also other checks than just checking whether the entry is a file 
    if (is_file($filepath) && filectime($filepath) > $latest_ctime) { 
    $latest_ctime = filectime($filepath); 
    $latest_filename = $entry; 
    } 
}?> 

當我上傳此我得到的錯誤是

A PHP Error was encountered 

Severity: Warning 

Message: dir(https://www.domain.com/pdfs/bob/uploaded): failed to open dir: not implemented 

Filename: views/search.php 

Line Number: 71 

我試圖尋找並嘗試了一些東西的mkdir我發現,沒有工作。即使是我在stackoverflow上找到的最新文件。

+0

你還沒有在代碼中做任何目錄或檢查是否存在目錄 – soni8010

回答

1

如何在php中創建目錄如果不存在。

if (!is_dir('path/to/directory')) { 
    mkdir('path/to/directory', 0777, true); 
} 

在這裏,我介紹你的代碼..

<?php $path = base_url(''.$value['username']) . '/' . 'uploaded';) 

    $latest_ctime = 0; 
    $latest_filename = ''; 

    if (!is_dir($path)) { 
     mkdir($path, 0777, true); 
    } 

    $d = dir($path); 

    while (false !== ($entry = $d->read())) { 
     $filepath = "{$path}/{$entry}"; 
     // could do also other checks than just checking whether the entry is a file 
     if (is_file($filepath) && filectime($filepath) > $latest_ctime) { 
     $latest_ctime = filectime($filepath); 
     $latest_filename = $entry; 
     } 
    } 
?> 

注意:您必須通過在is_dir()mkdir()功能的路徑。您無法在這些功能中傳遞網址。

+0

我不得不使用MajicBob在我的路徑下面發佈的代碼。但除此之外,上述所有其他內容都有效,同樣的內容也發佈在了spaniard上。多謝你們! –

0

在嘗試打開它之前,您應該檢查dir是否存在。嘗試使用is_dir(),如:

if (is_dir($path)) { 
    $d = dir($path); 
} else { 
    mkdir($path, 0700);   // Change the permission as you need. 
    $d = dir($path);   // Since the folder is being just created you could return 
} 

如果你喜歡跳過這些用戶你可以改變else{}exit腳本,或任何你喜歡裏面的代碼。

0

在PHP dir()工作在文件系統上,而不是URL上。您需要使用的內容取決於文件的確切位置以及您的主機設置。根據信息嘗試類似:

$path = '<wwwroot>/pdfs/' . $value['username'] . '/uploaded'; 

如果它存在,應該得到一個Directory對象。如果你不知道你的WWW根是你可以嘗試的東西像什麼:

$path = __DIR__ . '/pdfs/' . $value['username'] . '/uploaded'; 

這將根據您正在執行的文件的當前目錄。

0

您正試圖從URL中讀取目錄。您需要將它傳遞給文件系統中的目錄路徑。 例如。

'/home/your_app/pdfs/' . $value['username']) . '/' . 'uploaded'