請注意,當 PHP 由 f.e. apache 或 nginx 呼叫,而不是直接從命令列呼叫時,touch() 不會新增呼叫指令碼的位置,因此提供的檔名必須包含一個絕對路徑。
如果指令碼從 /home/user/www 啟動,則不會輕觸 "/home/user/www/somefile"
<?php
touch( 'somefile' );
?>
但如下操作會輕觸
<?php
touch( __DIR__ . '/somefile' );
?>
(PHP 4,PHP 5,PHP 7,PHP 8)
touch — 設定檔案的訪問和修改時間
嘗試將 filename
引數中指定的檔案的訪問和修改時間設定為 mtime
中給定的值。請注意,無論引數有多少,訪問時間始終都會被修改。
如果該檔案不存在,則會建立該檔案。
版本 | 描述 |
---|---|
8.0.0 |
mtime 和 atime 現在可以為 null。 |
示例 #1 touch() 示例
<?php
if (touch($filename)) {
echo $filename . ' 修復時間已被修改為現在的事件';
} else {
echo ' 抱歉,無法修改 . $filename 的修訂時間';
}
?>
示例 #2 touch() 使用 mtime
引數
<?php
// 這是輕觸時間,我們將其設定為過去一小時。
$time = time() - 3600;
// 輕觸檔案
if (!touch('some_file.txt', $time)) {
echo '糟糕,有些地方出了差錯...';
} else {
echo '成功輕觸了檔案';
}
?>
注意:
請注意,時間解析度可能因檔案系統而異。
請注意,當 PHP 由 f.e. apache 或 nginx 呼叫,而不是直接從命令列呼叫時,touch() 不會新增呼叫指令碼的位置,因此提供的檔名必須包含一個絕對路徑。
如果指令碼從 /home/user/www 啟動,則不會輕觸 "/home/user/www/somefile"
<?php
touch( 'somefile' );
?>
但如下操作會輕觸
<?php
touch( __DIR__ . '/somefile' );
?>
更新訪問時間,但不更新修改時間
Unix 命令:touch -a filename
PHP:touch(filename, date('U', filemtime(filename)), time())
我一直在嘗試使用 PHP5 的 touch() 將 filemtime 設為將來。
touch $time 似乎有個大約 1000000 秒(約 11 天)的將來限制。超出此限制,它會還原為先前的 $time。
這沒什麼道理,但我可以為您節省幾個小時的時間。
$time = time()+1500000;
touch($cachedfile,$time);
至少在 Linux 中,touch 本身不會更改符號連結的時間,但它會更改其指向的檔案/目錄的時間。解決此問題的唯一方法是取消連結符號連結,然後重新建立它。
發現這一點花了點時間。作業系統本身沒有提供可用來完成此操作的方法。許多人想知道為什麼有人想要這樣做。我在 Web 樹的內部使用符號連結來指向 Web 樹外部的檔案。一段時間後,我希望這些符號連結失效,這樣檔案便無法成功熱連結到。
如果要觸控不屬於自己的檔案,操作會變得容易得多。
<?php
function touchFile($file) {
fclose(fopen($file, 'a'));
}
?>
更新其中一些檔案時,我需要使用它來觸控 /etc/cron.d 目錄。我知道文件中說這不是必需的,但為了讓我的更改能夠快速得到採納,我發現需要這樣做。
我也遇到了許可權錯誤,並且我發現使用 chmod 777 /etc/cron.d 可以解決此問題。
因此,您應該能夠對具有開放寫入許可權的目錄使用 PHP touch 函式。
當然,這不是最安全的方式,但是在我們的應用程式中,該資料夾的安全性不是特別重要。
我已經完成了一個小測試來檢查哪個函式建立新檔案的速度更快。
file_put_contents 與 touch
<?php
for($i = 0; $i < 100; $i++)
{
file_put_contents('dir/file'.$i, '');
}
?>
平均時間:0.1145 秒
<?php
for($i = 0; $i < 100; $i++)
{
touch('dir/file'.$i);
}
?>
平均時間:0.2322 秒
因此,file_put_contents 的速度比 touch 快兩倍。
如果你要刪除(取消連結)你
不擁有,僅僅為了修改
檔案中的修改時間,那你最好 chown() 該檔案
恢復到其原始所有權,並在完成後
將其 chmod() 恢復為正確的許可權。否則
你幾乎肯定會有所破壞。此外列出的
用於 touch() 你不擁有的檔案的程式碼應該
將檔案建立的時間設定回其原始時間,如果
所需要的是僅僅修改修改時間。
另外,列出的程式碼如果出現 i/o
錯誤(例如磁碟已滿或目錄中檔案太多),將會破壞事物。
以下是該程式碼應當編寫的格式
首先而不是最後建立新檔案,使用不同的
諸如 $file.tmp. 的名稱。
讀取舊檔案的擁有權、許可權和建立日期。
將新檔案的許可權和建立日期設定與舊檔案相同。
將新檔案的名稱重新命名為舊檔案的名稱。
將新檔案的 chown() 設定為擁有要替換的檔案的使用者。
如果你
從未學習過程式設計 101,請謹慎新增文件。
在目錄中修改日期的唯一方法是透過 touch() 建立檔案,然後透過 unlink() 刪除檔案
<?php
$dir = 'temp';
$files1 = scandir($dir);
$files1 = array_slice($files1, 2);
foreach ($files1 as $key => $val)
{
if (!is_dir($val)) continue;
if (!touch($val))
{
touch($val . "/plik.txt");
unlink($val . "/plik.txt");
}
}
?>
一個簡潔的小指令碼,它將為你提供某個資料夾中某個日期之後所有已修改檔案列表
$filelist = Array();
$filelist = list_dir("d:\\my_folder");
for($i=0;$i<count($filelist);$i++){
$test = Array();
$test = explode("/",date("m/d/Y",filemtime($filelist[$i])));
// 比下面列出的檔案示例更晚
//06/17/2002
if(($test[2] > 2001) && ($test[1] > 16) && ($test[0] > 5)){
echo $filelist[$i]."\r\n";
}
clearstatcache();
}
function list_dir($dn){
if($dn[strlen($dn)-1] != '\\') $dn.='\\';
static $ra = array();
$handle = opendir($dn);
while($fn = readdir($handle)){
if($fn == '.' || $fn == '..') continue;
if(is_dir($dn.$fn)) list_dir($dn.$fn.'\\');
else $ra[] = $dn.$fn;
}
closedir($handle);
return $ra;
}
我發現設定一個負*mtime*會刪除檔案。下面的程式碼始終刪除$path中的檔案,而$touch返回true。
<?php
$path = '/folder/file';
$timestamp = -1;
$touch = touch($path, $timestamp);
?>
執行PHP 7.4.5
更好的解釋
針對儲存在變數 $access 和 $modified 中的檔案 $file 和 UNIX 時間
- 僅更改訪問時間
\touch($file, \filemtime($file), $access);
- 僅更改修改時間
\touch($file, $modified, \fileatime($file));
- 更改訪問時間和修改時間
\touch($file, $modified, $access);
檢視結果
//使用儲存在自定義資料夾中的會話 Cookie
$file = '/var/www/test_com/session/sess_qfn587cudfpgsijm1bs4d81s75';
echo 'sess_qfn587cudfpgsijm1bs4d81s75 的統計資訊<br/>';
\clearstatcache();
echo '訪問時間:'.\date("Y-m-d H:i:s", \fileatime($file)).'<br/>';
echo '修改時間:'.\date("Y-m-d H:i:s", \filemtime($file)).'<br/>';
echo '將訪問時間更改為現在,修改時間為 1 小時後<br/>';
\touch($x, \filemtime($file)+3600, time());
\clearstatcache();
echo '訪問時間:'.\date("Y-m-d H:i:s", \fileatime($file)).'<br/>';
echo '修改時間:'.\date("Y-m-d H:i:s", \filemtime($file)).'<br/>';
注意反覆呼叫 clearstatcache()!
以下是一種變通辦法,可使 PHP 使用者編輯自己不擁有的檔案
<?php
$target_file = "/path/to/file/filename.txt"; //system filepath to your file
$file_content = implode("",file($target_file));
@unlink($target_file);
if($savetofile = fopen($target_file, "w")) {
fputs($savetofile, $file_content);
fclose($savetofile);
}
$new_date = strtotime("23 April 2005"); // set the required date timestamp here
touch($target_file,$new_date);
?>
當然,PHP 需要具有對要編輯的檔案所在資料夾的寫訪問許可權,但這樣很容易實現。
在命令列上的 unix 中,你可以編輯不屬於自己的檔案 - 但正如此頁面中的其他評論中所述 - PHP 的內建觸控功能無效。
(在 unix 中)一個簡單的替代方法是
<?php
function touch_it_good($filename)
{
exec("touch {$filename}");
}
?>
之前的評論引用了一段程式碼示例,顯示 file_put_contents() 建立檔案比 touch 更快。我重新在 PHP 5.5.9 上運行了同一測試,這似乎不再是這種情況。
<?php
$startTime = microtime(true);
for($i = 0; $i< 100000; $i++)
{
file_put_contents('dir/file'.$i, '');
unlink('dir/file'.$i);
}
echo "時間: ".(microtime(true)-$startTime)."\n"; // 時間: 2.6902809143066
$startTime = microtime(true);
for($i = 0; $i < 100000; $i++)
{
touch('dir/file'.$i);
unlink('dir/file'.$i);
}
echo "時間: ".(microtime(true)-$startTime)."\n"; // 時間: 2.3343770503998
?>