2011-10-31 177 views
2

我爲WordPress創建了一個插件,要求存在兩個文件才能正常運行。PHP,檢查URL和文件是否存在?

第一個文件被定義爲文件系統路徑,第二個文件被定義爲URL。

比方說,第一個文件是:

/home/my_site/public_html/some_folder/required_file.php 

和第二個文件是:

http://www.my_site.com/some_folder/required_url_file.php 

注意,這兩個文件是不一樣的文件到文件系統。 required_file.php的內容不是required_url_file.php,它們的行爲絕對不同

關於如何驗證兩個文件的存在的任何想法?

+0

的可能重複(http://stackoverflow.com/questions/981954/how -can-one-check-see-if-a-remote-file-exists-using-php) - 用於文件使用['is_file'](http://php.net/is_file)。 – hakre

回答

5

可以選中這兩個:

$file = '/home/my_site/public_html/some_folder/required_file.php'; 
$url = 'http://www.my_site.com/some_folder/required_url_file.php'; 

$fileExists = is_file($file); 
$urlExists = is_200($url); 

$bothExists = $fileExists && $urlExists; 

function is_200($url) 
{ 
    $options['http'] = array(
     'method' => "HEAD", 
     'ignore_errors' => 1, 
     'max_redirects' => 0 
    ); 
    $body = file_get_contents($url, NULL, stream_context_create($options)); 
    sscanf($http_response_header[0], 'HTTP/%*d.%*d %d', $code); 
    return $code === 200; 
} 
+0

is_200()不是有效的WordPress功能。該代碼在PHP 5.2的WordPress 3.3中失敗。 –

+2

@CharlestonSoftwareAssociates:那麼,誰寫的是* Wordpress的一部分?如果您從答案中複製代碼,請不要忘記複製函數定義。 ;) – hakre

+0

好的,錯過了那部分,但該方法仍然不是WordPress設置的最佳方法。首先,200不是遠程URL項目存在的唯一有效響應。其次,WordPress中的wp_remote_head()方法在file_get_contents之外有多個後備包括curl和直接套接字操作。我會刪除downvote作爲您的答案將工作,但不是爲WordPress項目提供的最好的解決方案......但是它已經鎖定它。:/ –

1

要檢查文件是否存在,請使用file_exists方法。

自PHP 5.0.0,這個功能也可以用一些 URL 封裝協議。請參閱Supported Protocols and Wrappers以確定哪些包裝支持stat()系列功能。

if(! (file_exists($url1) && file_exists($url2))) { 
    die("Files don't exist - throw error here."); 
} 

// Continue as usual - files exist at this point. 
+0

您通常不希望依賴file_exists,因爲如果目錄存在,它將返回肯定結果,除非您確定將文件url和文件名返回的內容是什麼,否則您通常會以目錄結果爲肯定一個文件名被返回。使用is_file()。 – Jake

1

如果你有PECL http_head功能可用,你可以檢查它是否返回狀態代碼200的遠程文件。

要檢查您是否可以訪問本地文件,可以使用file_exists,但這並不表示您將能夠訪問該文件。要檢查您是否可以讀取該文件,請使用is_readable

1

使用功能file_exists()

file_exists('http://www.my_site.com/some_folder/required_url_file.php'); 

會得到你的結果真的還是假的。

+3

我不相信這適用於網址。 – 2011-10-31 11:26:27

0

正在檢查文件是否存在:

if (file_exists('path/to/file.txt')) { 
    echo "File exists!"; 
} else { 
    echo "File doesn't exist."; 
} 

檢查網址是否有效:

$data = @file_get_contents("http://url.com/"); 
if (!$data) { 
    echo "URL not valid."; 
} else { 
    echo "URL is valid."; 
} 

注:

理想情況下,你不應該試圖和預測文件系統。雖然諸如file_exists之類的方法非常有用,但不應該依賴它們,而應該嘗試寫入文件,從中讀取文件等,然後捕獲並處理髮生的任何異常或錯誤。

2
$file_exists = file_exists($path); 
$url_accessable = http_get($url, array("timeout"=>10), $info); // should not be FALSE 
$status_code = $info['response_code'] //should be 200 
4

至於驗證網址,所有這些答案都在考慮正確的,WordPress的方式來完成這項任務。

應該使用wp_remote_head()這個任務。

這是我寫的關於How To Check Whether an External URL Exists with WordPress’ HTTP API的文章。檢查出來並弄清楚它是如何工作的。

+0

優秀的解決方案。您的文章中提供的代碼是這種類型測試的正確設置。我將在下面的「webItemExists()」函數中發佈我的代碼的簡化版本。我會在這裏做,但評論不適合代碼共享。 –

+0

謝謝@CharlestonSoftwareAssociates!我很高興你發現它很有用。 –

1

遠程:

$file = 'http://www.my_site.com/some_folder/required_url_file.php' 
if (@fclose(@fopen($file,"r"))) echo "File exists!"; 

地方:

$file = '/home/my_site/public_html/some_folder/required_file.php'; 
if (is_file($file)) echo "File exists!"; 
5

基於H. Maor的代碼示例,這裏是我用我的插件功能:

/** 
* Check if an item exists out there in the "ether". 
* 
* @param string $url - preferably a fully qualified URL 
* @return boolean - true if it is out there somewhere 
*/ 
function webItemExists($url) { 
    if (($url == '') || ($url == null)) { return false; } 
    $response = wp_remote_head($url, array('timeout' => 5)); 
    $accepted_status_codes = array(200, 301, 302); 
    if (! is_wp_error($response) && in_array(wp_remote_retrieve_response_code($response), $accepted_status_codes)) { 
     return true; 
    } 
    return false; 
} 

我我已經在輔助類中做了這個方法,但是將它放在主題的functions.php文件中應該使它在任何地方都可以訪問。但是,你應該總是寫在課堂上並實例化它們。隔離你的插件和主題功能要好得多。

有了這個地方,你可以簡單地使用:

如果(webItemExists( 'http://myurl.com/thing.png')){打印 '它iexists'; }

大多數情況下,您將使用WordPress調用通過相對或完全限定的URL訪問所有項目。如果您具有相對於/uploads/2012/12/myimage.png之類的內容的引用,只需添加get_site_url()即可將其轉換爲完全限定的URL與WordPress相對URL。調用webItemExists函數時的$ string。

1

這似乎爲我工作:[?怎樣才能檢查,看看是否在遠程文件是否存在使用PHP]