2

Include an error in writing the html file from php

 2 years ago
source link: https://www.codesd.com/item/include-an-error-in-writing-the-html-file-from-php.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Include an error in writing the html file from php

advertisements

I seem to have some problem with my code here. It creates a file from the php file, but I get an error on the include path.

include('../include/config.php');

$name = ($_GET['createname']) ? $_GET['createname'] : $_POST['createname'];

function buildhtml($strphpfile, $strhtmlfile) {
ob_start();
include($strphpfile);
$data = ob_get_contents();
$fp = fopen ($strhtmlfile, "w");
fwrite($fp, $data);
fclose($fp);
ob_end_clean();
}

buildhtml('portfolio.php?name='.$name, "../gallery/".$name.".html");

The problem seems to be here:

'portfolio.php?name='.$name

Any way I can replace this, and still send the variable over?


Here's the error I get when I put ?name after the php extension:

Warning: include(portfolio.php?name=hyundai) [function.include]: failed to open stream: No such file or directory in D:\Projects\Metro Web\Coding\admin\create.php on line 15

Warning: include(portfolio.php?name=hyundai) [function.include]: failed to open stream: No such file or directory in D:\Projects\Metro Web\Coding\admin\create.php on line 15

Warning: include() [function.include]: Failed opening 'portfolio.php?name=hyundai' for inclusion (include_path='.;C:\php\pear') in D:\Projects\Metro Web\Coding\admin\create.php on line 15


Now I saw your code in the comment to a previous answer I'd like to point few things out

function buildhtml($strhtmlfile) {
    ob_start(); // redundant
    $fp = fopen ($strhtmlfile, "w"); // redundant
    file_put_contents($strhtmlfile,
                      file_get_contents("http://host/portfolio.php?name={$name}"));
//                                       where does $name come from?? ---^
    close($fp); // also redundant
    ob_end_clean(); // also redundant
}
buildhtml('../gallery/'.$name.'.html');

In PHP as in many other languages you can do things in different ways. What you've done is you took three different ways and followed only one (which is absolutely enough). So when you use functions file_put_contents() and file_get_contents() you don't need the buffer, that is the ob_ family of functions, because you never read anything in the buffer which you should then get with ob_get_contents(). Nor you need the file handles created and used by fopen(), fclose(), because you've never written to or read from the file handle i.e. with fwrite() or fread().

If I'm guessing correctly that the purpose of your function is to copy html pages to local files, my proposal would be the following:

function buildhtml($dest_path, $name) {
    file_put_contents($dest_path,
                  file_get_contents("http://host/portfolio.php?name={$name}"));
}

buildhtml('../gallery/'.$name.'.html', $name);




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK