[英]php script to check files if the same exists
我正在編寫一個小的php腳本來檢查文件,該文件是否在磁盤的給定路徑上存在,
當前,腳本從文本文件(file.txt)中讀取文件名,然后檢查給定路徑中的文件,如果該文件存在或不存在,它將在搜索結果數組中推送鍵/值對。 但是問題是,即使所有文件都存在,即使我為所有文件都提供了相同的文件名,它也僅對file.txt中的最后一個文件顯示成功。
(盡管我可以用批處理/殼,power-shell等其他方法來完成它,但只是為了提高在Php中執行技巧的技巧)。 無法理解我想念的地方,是我從file.txt中讀取文件名時做錯了什么。
提前感謝您的幫助和感謝。 -桑巴夫
-File.txt格式
file1
file2
file3
-PHP腳本
<?php
if (ini_get('display_errors') != 1){ ini_set('display_errors',1); }
$filenames = file('file.txt');
$path = "D:\testpath";
$search_result = array();
foreach ($filenames as $filename)
{
if(file_exists($path.chr(92).$filename))
{
$search_result[$filename] = "File Exists";
}
else
{
$search_result[$filename] = "File not found";
}
}
print_r($search_result);
$path
未傳遞到checkfile
嘗試
array_walk($filenames,function($filename) use ($path) {
return checkfile($filename, $path);
});
但是問題是,即使所有文件都存在,即使我為所有文件都提供了相同的文件名 ,它也僅對file.txt中的最后一個文件顯示成功。
$search_result[$filename]
是一個由filename索引的關聯數組,這意味着每個唯一的$filename
它將包含1個值。 如果提供重復的$filenames
,它將簡單地覆蓋現有值。
如果要記錄每張支票,即使文件名重復,也不要使用關聯數組:
foreach ($filenames as $filename)
{
if(file_exists($path.chr(92).$filename))
{
$search_result[] = "$filename Exists";
}
else
{
$search_result[] = "$filename not found";
}
}
使用內置的php function.file_exists(); 參考http://www.php.net/manual/zh/function.file-exists.php
在Windows上,使用//計算機名/共享/文件名。
如果PHP啟用了safe_mode,則此方法將無效。 嘗試設置safe_mode_include_dir以添加異常,並使用語法\\ computername \\ share \\ filename引用該位置
好的,因為我懷疑它是從file.txt讀取文件名的問題。 添加可選參數FILE_IGNORE_NEW_LINES后,問題解決了。
file('file.txt',FILE_IGNORE_NEW_LINES);
因此,如果有人要在給定路徑中搜索文件,這是工作代碼,當然,如果有人要使用數據庫存儲的文件名代替純文本文件進行輸入或根據要求進行其他修改,則可以對其進行修改。 這只是一個基本腳本,它從file.txt中讀取文件名並在$ path變量中提供的給定目錄中搜索文件名。
PHP腳本
<?php
if (ini_get('display_errors') != 1){ ini_set('display_errors',1); }
$filenames = file('file.txt', FILE_IGNORE_NEW_LINES);
$path = "D:\testfolder";
$search_result = array();
foreach ($filenames as $filename)
{
if(file_exists($path.chr(92).$filename))
{
$search_result[$filename] = "File Exists";
echo $path.chr(92).$filename."<BR />";
}
else
{
$search_result[$filename] = "File not found";
echo $path.chr(92).$filename."<BR />";
}
}
print_r($search_result);
file.txt的格式
file1
file2
file3
嗯,文件名沒有擴展名嗎? 除非我錯過了一些東西,而它們只是占位符(也許最后一個是文件夾?哈哈)。
<?php
ini_set('display_errors',1); //no need to check just turn it on
$f = fopen('file.txt', 'r'); //use a file stream
$path = "D:/testpath/"; //forward slash works windows and linux
$search_result = array();
while(false !== ( $filename = fgets($f))) //process 1 line at a time how big will file get?
{
$filename = trim($filename); //always remove whitespace
if(file_exists($path.$filename) && is_file($path.$filename))
//file_exists returns true on directories
{
$search_result[$filename] = "File Exists";
}
else
{
$search_result[$filename] = "File not found";
}
}
print_r($search_result);
fclose($f);
-note-代碼未經測試,但這是我會做的。
這是另一個正確的腳本(盡管我在上一個答案中發布的腳本也可以使用,但這只是改進)。 在此我添加了Trim來修剪數組值中的空白,並添加了&& is_file($ path.chr(92).trim($ filename))[由@ArtisiticPhoenix建議]以檢查文件名是否不是目錄
<?php
if (ini_get('display_errors') != 1){ ini_set('display_errors',1); }
$filenames = file('file.txt');
$path = "D:\Ebooks\php";
$search_result = array();
foreach ($filenames as $filename)
{
if(file_exists($path.chr(92).trim($filename)) && is_file($path.chr(92).trim($filename)))
{
$search_result[$filename] = "File Exists";
echo $path.chr(92).$filename."<BR />";
}
else
{
$search_result[$filename] = "File not found";
echo $path.chr(92).$filename."<BR />";
}
}
print_r($search_result);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.