Home > front end >  file_get_contents() expects parameter 1 to be a valid path, array given in a php file
file_get_contents() expects parameter 1 to be a valid path, array given in a php file

Time:09-26

I have a php code as shown below in which I extracting a title from a good.php file.

good.php

print_r($good_files);   // Line Y

$good_title = ExtractGoodTitle($good_files);  // Line Z
        
function ExtractGoodTitle($filename)
{
    $html = file_get_contents($filename);   // Line A
    .
    .
    .
    .
    .
    .
    .
    .
    return $title;
}   

At Line A, I am getting the following error: file_get_contents() expects parameter 1 to be a valid path, array given in ./good.php file.

For debug purpose, I have added print_r($good_files) in the code. Line Y prints the following:

Array ( [180] => /home/alex/public_html/good/web/gooddump/GOOD_2.html 
        [375] => /home/alex/public_html/good/web/gooddump/GOOD_4.html 
        [471] => /home/alex/public_html/good/web/gooddump/GOOD_5.html 
        [571] => /home/alex/public_html/good/web/gooddump/GOOD_6.html 
        [618] => /home/alex/public_html/good/web/gooddump/GOOD_7.html 
        [622] => /home/alex/public_html/good/web/gooddump/GOOD_8.html 
        [626] => /home/alex/public_html/good/web/gooddump/GOOD_9.html 
        [6] => /home/alex/public_html/good/web/gooddump/GOOD_10.html 
        [15] => /home/alex/public_html/good/web/gooddump/GOOD_11.html 
        [24] => /home/alex/public_html/good/web/gooddump/GOOD_12.html 
        [27] => /home/alex/public_html/good/web/gooddump/GOOD_13.html 
        [36] => /home/alex/public_html/good/web/gooddump/GOOD_14.html 
        [46] => /home/alex/public_html/good/web/gooddump/GOOD_15.html 
        [74] => /home/alex/public_html/good/web/gooddump/GOOD_18.html 
        [85] => /home/alex/public_html/good/web/gooddump/GOOD_19.html
        [113] => /home/alex/public_html/good/web/gooddump/GOOD_22.html
        [160] => /home/alex/public_html/good/web/gooddump/GOOD_27.html
        [170] => /home/alex/public_html/good/web/gooddump/GOOD_28.html 
    )

Problem Statement:

I am wondering what changes I need to make in the php code at Line Z or Line A so that it doesn't throw the error.

CodePudding user response:

You need to iterate over the $good_files array as such, since it is an array you will get a lot of titles, not only one.:

foreach ($good_files as $good_file) {
    $good_title = ExtractGoodTitle($good_file); 
}
  • Related