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

mq0036 阅读:225 2025-06-02 22:19:02 评论:0

我想将文件引用作为函数参数传递。我不确定这在 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() 的范围内不可用。请引用手册页Variable Scope .

将函数声明更改为:

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

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

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

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


标签:PHP
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号