簡體   English   中英

將文件引用作為函數參數傳遞

[英]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是對該函數外部的“實際”文件句柄的引用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM