让我们考虑一个示例php脚本,它通过用户输入删除一行:
$DELETE_LINE = $_GET['line'];
$out = array();
$data = @file("foo.txt");
if($data)
{
foreach($data as $line)
if(trim($line) != $DELETE_LINE)
$out[] = $line;
}
$fp = fopen("foo.txt", "w+");
flock($fp, LOCK_EX);
foreach($out as $line)
fwrite($fp, $line);
flock($fp, LOCK_UN);
fclose($fp);
Run Code Online (Sandbox Code Playgroud)
我想知道某个用户当前是否正在执行此脚本并且文件"foo.txt"被锁定,同时或在其执行完成之前,如果其他用户调用此脚本,那么会发生什么?第二个用户是否会等待第一个用户解锁文件?或第二个用户输入的行删除将失败?
我有一个脚本,使用php -l检查php文件中的语法错误.它在Windows中工作正常但在Linux中输出不正确:
正在检查语法错误的文件exec_ip.php的内容是(它有要检查的语法错误):
<?php
$arr['12] = 'asd';
?>
Run Code Online (Sandbox Code Playgroud)
并且脚本是:
$slash = file_get_contents('exec_ip.php');
//echo $slash;
$tmpfname = tempnam("tmp", "PHPFile");
file_put_contents($tmpfname, $slash);
exec("php -l ".$tmpfname,$error);
$errtext = '';
foreach($error as $errline) $errtext.='<br>'.$errline;
unlink($tmpfname);
echo 'ERR:'.$errtext;
Run Code Online (Sandbox Code Playgroud)
结果在WINDOWS(WAMP){CORRRECT}:
ERR:
Parse error: syntax error, unexpected T_STRING, expecting ']' in C:\WINDOWS\Temp\PHP1F1.tmp on line 2
Errors parsing C:\WINDOWS\Temp\PHP1F1.tmp
Run Code Online (Sandbox Code Playgroud)
LINUX中的结果(Centos/cPanel){UNKNOWN OUTPUT}:
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
Content-type: text/html
ERR:
... too …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用工作箱,但问题不是工作箱特有的。
谁能告诉我如何从特定缓存中检索所有网址的列表并将其返回给回调函数。我正在尝试向用户显示所有缓存网址的列表以及删除它们的选项。但我无法填充/显示所有缓存网址的列表。
我正在做的是:
function queryCache(){
var url = [];
caches.open('mycache').then(function (cache){
cache.keys().then(function(keys){
return new Promise(function(resolve, reject) {
keys.forEach(function (request, index, array) {
cache.match(request).then(function (response){
url.push(response.url);
})
})
resolve();
});
}).then(function(){
console.log(url); // lists all urls in console as array
cacheList(url);
});
})
}
function cacheList(Items)
{ for(var i = 0; i < Items.length; i++){
console.log('Items: '+Items[i]); //never reaches
}
console.log(Items.length); // always show 0
}
Run Code Online (Sandbox Code Playgroud)
当我从控制台调用queryCache()时,正确调用了cacheList(Items),但问题是,Items始终是一个空数组,并且其长度始终为0。
可能我没有正确使用 Promise 解析。请帮忙谢谢
按一个键值(asc或desc)对多维数组进行排序的次数要求太多.但我没有找到任何解决我的问题的方法,你必须通过预定义的自定义顺序中的一个键值对多维数组进行排序.
以数组为例
$array[0] = array('id'=> '111', value=>'abc');
$array[1] = array('id'=> '100', value=>'abc');
$array[2] = array('id'=> '132', value=>'abc');
$array[3] = array('id'=> '222', value=>'abc');
$array[4] = array('id'=> '112', value=>'abc');
$array[5] = array('id'=> '200', value=>'abc');
Run Code Online (Sandbox Code Playgroud)
并按下面数组中定义的顺序按子键'id'对此数组进行排序:
$sort_order_id = array('112','111','132','100');
Run Code Online (Sandbox Code Playgroud)
所以结果会是
$array[0] = array('id'=> '112', value=>'abc');
$array[1] = array('id'=> '111', value=>'abc');
$array[2] = array('id'=> '132', value=>'abc');
$array[3] = array('id'=> '100', value=>'abc');
$array[4] = array('id'=> '222', value=>'abc');
$array[5] = array('id'=> '200', value=>'abc');
Run Code Online (Sandbox Code Playgroud)
注意:如果某些id在$ sort_order_id数组中不可用,请将它们保留在最后(如上例所示,id 222和200不在$ sort_order_id数组中,因此它按照之前的顺序排在最后)
我尝试了很多,但无法为此创建算法...我希望有人会帮助我......