3

I want to open a server stored html report file on a client machine.

I want to bring back a list of all the saved reports in that folder (scandir). This way the user can click on any of the crated reports to open them.

So id you click on a report to open it, you will need the location where the report can be opend from

This is my dilemma. Im not sure how to get a decent ip, port and folder location that the client can understand

Here bellow is what Ive been experimenting with.

Using this wont work obviously: $path = $_SERVER['DOCUMENT_ROOT']."/reports/saved_reports/";

So I though I might try this instead.

    $host= gethostname();
    $ip = gethostbyname($host);
    $ip = $ip.':'.$_SERVER['SERVER_PORT'];
    $path    = $ip."/reports/saved_reports/";
    $files = scandir($path);

after the above code I loop through each file and generate a array with the name, date created and path. This is sent back to generate a list of reports in a table that the user can interact with. ( open, delete, edit)

But this fails aswell.

So im officially clueless on how to approach this.

PS. Im adding react.js as a tag, because that is my front-end and might be useful to know.

morne
  • 4,035
  • 9
  • 50
  • 96

2 Answers2

0

Your question may be partially answered here: https://stackoverflow.com/a/11970479/2781096

Get the file names from the specified path and hit curl or get_text() function again to save the files.

function get_text($filename) {

    $fp_load = fopen("$filename", "rb");

    if ( $fp_load ) {

            while ( !feof($fp_load) ) {
                $content .= fgets($fp_load, 8192);
            }

            fclose($fp_load);

            return $content;

    }
}


$matches = array();
// This will give you names of all the files available on the specified path.
preg_match_all("/(a href\=\")([^\?\"]*)(\")/i", get_text($ip."/reports/saved_reports/"), $matches);

foreach($matches[2] as $match) {
    echo $match . '<br>';
    // Again hit a cURL to download each of the reports.
}
Akshit Arora
  • 428
  • 3
  • 15
0

Get list of reports:

<?php
$path    = $_SERVER['DOCUMENT_ROOT']."/reports/saved_reports/";


$files = scandir($path);



foreach($files as $file){
    if($file !== '.' && $file != '..'){
        echo "<a href='show-report.php?name=".$file. "'>$file</a><br/>";
    }
}


?>

and write second php file for showing html reports, which receives file name as GET param and echoes content of given html report.

show-report.php

<?php

$path    = $_SERVER['DOCUMENT_ROOT']."/reports/saved_reports/";

if(isset($_GET['name'])){
    $name = $_GET['name'];

    echo file_get_contents($path.$name);

}
godot
  • 3,422
  • 6
  • 25
  • 42