我想将文件引用作为函数参数传递。我不确定这在 php 中是如何工作的,但在 js 中,只要变量是全局定义的,它似乎就可以了。显然这不是 js,但我不确定如何更改我的代码来解决这个问题。
在我的文件的开头,我定义了如下内容:
$myfileref = fopen('../some/path.txt', 'w');
function writeHeader($fileref){
fwrite($fileref, 'some text here\n');
}
然后我想这样称呼它:
writeHeader($myfileref);
我究竟做错了什么?
这是我的代码:
<?php
$outfile = fopen('outfile.txt', 'w');
$path = '.';
$one_html = fopen('../write_dir/one.html', 'w');
if(!is_resource($one_html)){
die("fopen failed");
}
$two_html = fopen('../write_dir/two.html', 'w');
if(!is_resource($two_html)){
die("fopen failed");
}
$three_html = fopen('../write_dir/three.html', 'w');
if(!is_resource($three_html)){
die("fopen failed");
}
function searchFiles($dir){
$outpath = getcwd() . '/../write_dir/';
$in_dir = 'none';
$f_one = $f_two = $f_three = false;
$thisdir = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($thisdir, RecursiveIteratorIterator::SELF_FIRST);
global $one_html, $two_html, $three_html;
foreach($files as $object){
if($object->isDir()){
if(strpos($object->getPathname(), 'one') == true){
if(!$f_one){
echo "in one \n";
fileHeader($one_html);
$f_one = true;
}
$in_dir = 'one';
}
else if(strpos($object->getPathname(), 'two') == true){
if(!$f_two){
echo "in two \n";
fileHeader($two_html);
$f_two = true;
}
$in_dir = 'two';
}else if(strpos($object->getPathname(), 'three') == true){
if(!$f_three){
echo "in three \n";
fileHeader($three_html);
$f_three = true;
}
}
}
}
}
function fileHeader($fileref){
fwrite($fileref, "<table border=\"1\">\n");
fwrite($fileref, '<tr bgcolor=\"#CCCCCC\">\n');
fwrite($fileref, '<th>name</th>');
fwrite($fileref, '<th>description</th>');
fwrite($fileref, '<th>authors</th>');
fwrite($fileref, '<th>version</th>');
fwrite($fileref, '</tr>');
}
searchFiles('.');
?>