đây là cách tiếp cận của tôi.
nó tạo tệp php tạm thời và bao gồm nó.
nhưng cách này nếu mã bạn muốn chạy trên chức năng này có thoát khỏi chương trình lỗi trước khi tháo tạm thời tập tin
vì vậy tôi thực hiện một thủ tục autoclean trong chức năng. theo cách này, nó sẽ xóa các tệp tạm thời cũ bằng một hàm hết thời gian chờ chạy. bạn có thể đặt thời gian chờ hoặc tắt tính năng này từ các tùy chọn khi bắt đầu chức năng
tôi cũng thêm tùy chọn lỗi bỏ qua để giải quyết các tệp tạm thời không bị xóa. nếu lỗi bị bỏ qua, chương trình sẽ tiếp tục và xóa tệp tạm thời.
cũng một số dự án phải vô hiệu hóa autoclean vì nó quét toàn bộ thư mục mọi lúc nó chạy. nó có thể làm tổn thương hiệu suất đĩa.
function eval2($c) {
$auto_clean_old_temporary_files=false; //checks old temporary eval2 files for this spesific temporary file names generated by settings below
$ignore_all_errors=true; //if you ignore errors you can remove temporary files even there is an error
$tempfiledirectory=''; //temporary file directory
$tempfileheader='eval2_'; // temporary file header
$tempfiletimeseperator='__'; // temporary file seperator for time
$tempfileremovetimeout=200; // temp file cleaning time in seconds
if ($auto_clean_old_temporary_files===true) {
$sd=scandir('.'); //scaning for old temporary files
foreach ($sd as $sf) {
if (strlen($sf)>(32+strlen($tempfileheader)+strlen($tempfiletimeseperator)+3)) { // if filename long enough
$t1=substr($sf,(32+strlen($tempfileheader)),strlen($tempfiletimeseperator)); //searching time seperator
$t2=substr($sf,0,strlen($tempfileheader)); //searching file header
if ($t1==$tempfiletimeseperator && $t2==$tempfileheader) { //checking for timeseperator and file name header
$ef=explode('.',$sf);
unset($ef[count($ef)]);//removing file extension
$nsf=implode('.',$ef);//joining file name without extension
$ef=explode($tempfiletimeseperator,$nsf);
$tm=(int)end($ef); //getting time from filename
$tmf=time()-$tm;
if ($tmf>$tempfileremovetimeout && $tmf<123456 && $tmf>0) { // if time passed more then timeout and difference with real time is logical
unlink($sf); // finally removing temporary file
}
}
}
}
}
$n=$tempfiledirectory.$tempfileheader . md5(microtime().rand(0,5000)). $tempfiletimeseperator . time() .'.php'; //creating spesific temporary file name
$c='<?php' . PHP_EOL . $c . PHP_EOL; //generating php content
file_put_contents($n,$c); //creating temporary file
if ($ignore_all_errors===true) { // including temporary file by your choise
[email protected]($n);
}else{
$s=include($n);
}
return $s;
}
Nguồn
2017-11-02 00:10:38
Cảm ơn câu hỏi này. Nó đã giúp với điều này: https://github.com/tedivm/Stash/pull/135 – CMCDragonkai