我从控制器加载一个文件,并试图在模型中从该文件中找到一个字符串。
我写的控制器中的功能有:
<?php
public function file() {
$filename = "C:/Users/Declan/Desktop/foo.txt";
}
?>
在我想要的模型中:
<?php
function find{
if( exec('grep '.escapeshellarg($_GET['bar']).'$filename')) {
echo "string found";
}
}
?>
我用这个问题作为参考。
如有任何帮助,我们将不胜感激。
当您使用exec()方法时,该方法用于运行外部程序。你显然是在windows上,grep只适用于unix系统(Linux…)
<?php
function find($filePath, $stringToLookFor){
$handle = fopen($filePath, 'r');
$stringFound = false; // Init false
while (($buffer = fgets($handle)) !== false) { // Loop through each file line
if (strpos($buffer, $stringToLookFor) !== false) { // Check if in $buffer
$stringFound = true; // String found
}
}
fclose($handle);
return $stringFound;
}
?>