将文件引用作为函数参数传递


Passing a file reference as a function parameter

我想传递一个文件引用作为函数参数。我不确定这在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('.');
?>

在PHP中将文件描述符(资源)传递给函数并没有什么神奇之处。它们将被当作普通参数处理。

下面的示例将按原样工作。(比如你)

<?php
// define your function
function write($fd, $message) {
    fwrite($fd, $message);
}
// open a file
$fd = fopen('/tmp/a.txt', 'w');
// make sure that opening succeeded
if(!is_resource($fd)) {
    die('fopen failed');
}
// write to the file
write($fd, 'hello world');
// close the file
fclose($fd);

查看原始代码后更新

您只是错过了将这些文件描述符作为参数传递给函数searchFiles。所以它们不在searchFiles()的范围内。请参阅手册页可变作用域。

将函数声明改为:

function searchFiles($dir, $one_html, $two_html, $three_html){ 

并在调用时将这些值传递给函数:

searchFiles('.', $one_html, $two_html, $three_html);

我希望你明白你做错了什么。

我真的看不出上面的代码有什么问题…我猜是

writeHeader($myfileref);                     <-- -you are calling this
$myfileref = fopen('log.txt', 'w');          <-- -- Before tHis 
function writeHeader($fileref){
  fwrite($fileref, 'some text here'n');
}

切换位置,它应该工作,或者只使用SplFileObject的方式,你可以easily force type to your function示例:

$file = new SplFileObject("log.txt", "w");
if (! $file->isWritable())
    throw new Exception("I can't write to this");
writeHeader($file);

function writeHeader(SplFileObject $file) {
    $file->fwrite("The 1st Header'r'n");
    $file->fwrite("The 2nd Header'r'n");
    $file->fwrite("The 3rd Header'r'n");
}

PHP对大多数东西都是按值传递的,所以你需要强制按引用传递:

function writeHeader(&$myfileref) {
                     ^--- 
 ...
}

使writeHeader()中的myfileref是对函数外部的"真实"文件句柄的引用。