0

EDIT: I'm pretty sure the issue has to do with the firewall, which I can't access. Marking Canis' answer as correct and I will figure something else out, possibly wget or just manually scraping the files and hoping no major updates are needed.

EDIT: Here's the latest version of the builder and here's the output. The build directory has the proper structure and most of the files, but only their name and extension - no data inside them.

I am coding a php script that searches the local directory for files, then scrapes my localhost (xampp) for the same files to copy into a build folder (the goal is to build php on the localhost and then put it on a server as html).

Unfortunately I am getting the error: Warning: copy(https:\\localhost\intranet\builder.php): failed to open stream: No such file or directory in C:\xampp\htdocs\intranet\builder.php on line 73.

That's one example - every file in the local directory is spitting the same error back. The source addresses are correct (I can get to the file on localhost from the address in the error log) and the local directory is properly constructed - just moving the files into it doesn't work. The full code is here, the most relevant section is:

// output build files
foreach($paths as $path)
{
    echo "<br>";
    $path = str_replace($localroot, "", $path); 
    $source = $hosted . $path;
    $dest = $localbuild . $path;

    if (is_dir_path($dest))
    {
        mkdir($dest, 0755, true);
        echo "Make folder $source at $dest. <br>";
    }
    else
    {
        copy($source, $dest);
        echo "Copy $source to $dest. <br>";
    }
}

1 Answers1

1

You are trying to use URLs to travers local filesystem directories. URLs are only for webserver to understand web requests. You will have more luck if you change this:

copy(https:\\localhost\intranet\builder.php)

to this:

copy(C:\xampp\htdocs\intranet\builder.php)

EDIT

Based on your additional info in the comments I understand that you need to generate static HTML-files for hosting on a static only webserver. This is not an issue of copying files really. It's accessing the HMTL that the script generates when run through a webserver.

You can do this in a few different ways actually. I'm not sure exactly how the generator script works, but it seems like that script is trying to copy the supposed output from loads of PHP-files.

To get the generated content from a PHP-file you can either use the command line php command to execute the script like so c:\some\path>php some_php_file.php > my_html_file.html, or use the power of the webserver to do it for you:

<?php

$hosted = "https://localhost/intranet/";              <--- UPDATED

foreach($paths as $path)
{
    echo "<br>";
    $path = str_replace($localroot, "", $path); 
    $path = str_replace("\\","/",$path);    <--- ADDED
    $source = $hosted . $path;
    $dest = $localbuild . $path;

    if (is_dir_path($dest))
    {
        mkdir($dest, 0755, true);
        echo "Make folder $source at $dest. <br>";
    }
    else
    {
        $content = file_get_contents(urlencode($source));
        file_put_contents(str_replace(".php", ".html", $dest), $content);
        echo "Copy $source to $dest. <br>";
    }
}

In the code above I use file_get_contents() to read the html from the URL you are using https://..., which in this case, unlike with copy(), will call up the webserver, triggering the PHP engine to produce the output.

Then I write the pure HTML to a file in the $dest folder, replacing the .php with .htmlin the filename.

EDIT

Added and revised the code a bit above.

Canis
  • 4,130
  • 1
  • 23
  • 27
  • Ok... my goal is to copy the html output of the php files, so I can automatically build changes to the header, etc. Is there any way to do that? The local files are unprocessed php. – Kris with a K Aug 09 '17 at 15:18
  • 1
    @KriswithaK So, what you are saying is that you want to use PHP to produce HTML based on dynamic data, and then store the output as a static file on a disk, rather than have the HTML produced when the person visits your page? – Canis Aug 10 '17 at 10:59
  • Yes, exactly :) The server isn't configured to serve php and apparently cannot be. I'm working on an intranet for a small company. – Kris with a K Aug 10 '17 at 15:14
  • 1
    @KriswithaK May I ask why? Why not just show it dynamically? – Canis Aug 10 '17 at 15:15
  • I'm working on an intranet for a small company, and their server isn't configured to serve up PHP, nor do they really want to. I'm using the PHP to be able to automatically include changes to the header/footer as well as links to all files in a given directory, for example. – Kris with a K Aug 10 '17 at 15:50
  • Heh, not to demand more help but for anyone with similar problems, the fix has not worked. It's giving a "failed to open stream - no such file or directory" for the source URL (w/ and w/o urlencode). I also verified my htmls wrapper is available and tried the contextOptions and cURL (no experience with cURL, tho) fixes, all three described here: https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https At this point I may look into some command line html-scraping tools or something, not sure where to go. – Kris with a K Aug 11 '17 at 17:17
  • 1
    @KriswithaK Can you post updated code, debug and error output? Just add an edit like I did, so we dont loose the original question. – Canis Aug 11 '17 at 17:19
  • I will edit that in as soon as I can on Monday, thanks for all the work you're doing. – Kris with a K Aug 12 '17 at 20:52
  • I edited the OP with links to the code and error output. Thanks again. – Kris with a K Aug 14 '17 at 15:51
  • 1
    @KriswithaK Sorry for the late answer. I see you still have the wrong slashes in the URL. I.e. you have https:\\ not https://. Might be helpful to try to replace those. ;) – Canis Aug 16 '17 at 19:31
  • 1
    @KriswithaK Made an update to the code to show what I mean. – Canis Aug 16 '17 at 19:43