Laravel API - Access files from my server using a url

-1

Very good people,

Today I have the following doubt; I have a server that as soon as I log in as a regular user, I redirect it to a specific route ("/front/resources/Timmy/hello.txt"). I would like this to be able to see certain files on my server through a url ( url: "server.es/front/resources/$cliente/$nombre_archivo" -> file from inside the server: "/ web / reporting /client_data/Clients_Reports/$cliente/hola.txt ").

I show you what I have done so far:

  • This is the controller that receives the data from the url to choose the client and the name of the file (the path of the controller corresponds to that of the clients):

    public function resources($client, $name){

        $client1 = str_replace('..', '', $client);
        $name1 = str_replace('..', '', $name);
        $urlComplete = __DIR__ . '../../client_data/Clients_Reports/'.$client1.'/'.$name1;
    

    //Muestra el contenido del archivo:

            echo $client." -- ".$name.": ";
            echo "<br>";
            $output = file_get_contents($urlComplete);
            echo $output;
    
    }
    
  • Here are the routes of interest ( web.php ):

    Route::get('/front/resources/{client}/{name}','FrontController@resources');

  • And this the error you give me:

file_get_contents(/usr/home/servidor.es/web/reporting/app/Http/Controllers../../client_data/Clients_Reports/Timmy/holi.txt): failed to open stream: No such file or directory

I think the route that I found is wrong (since it worked for me locally) and therefore I am looking for the correct path so that the client can access the text file in question.

Thanks in advance.

    
asked by Adrià Muñoz 24.07.2018 в 10:53
source

2 answers

0

Very good netizens,

I have finally solved this problem using the following code:

        $client1 = str_replace('..', '', $client);
        $name1 = str_replace('..', '', $name);
        $urlComplete = 'ftp://user:[email protected]/web/reporting/client_data/Clients_Reports/'.$client1.'/'.$name1;

        if(file_exists($urlComplete) == 1){
            include $urlComplete;

        }
        else{
            echo "Sorry, the page you are looking for could not be found.";
            echo "<br>Url: ".$urlComplete;
        }

    }

I have simply used the "ftp" method to call the server file directly and we only need to execute the php code using "include file_name.php". My mistake was due to several factors: the route and the execution.

  • The route: The path specified by the php file did not correspond to other files that it called this file and I missed other errors (file not exists).

  • Execution: The code was executed from the "public" directory and I called it from another directory, therefore the ftp did not correspond to the url.

Conclusion: Keep in mind where your application is pointing and rewrite if the executable code is needed such that:

 $rutafiles = '../client_data/Clients_Reports/nueva_Url';

To point where you should.

Thank you very much everyone for answering and greetings.

    
answered by 27.07.2018 / 12:49
source
0

I have come to show the content of the txt by using " ftp://servidor.es/web/ ... / hol.txt ".

Now my question is: if I want to run a php file using this procedure, how should I run it?

echo $client." -- ".$name.": ";
            echo "<br>";
            //$output = file_get_contents($urlComplete);
            //echo $output;
            include $urlComplete;

With this he tells me that I have the following error:

include(): ftp:// wrapper is disabled in the server configuration by allow_url_include=0

I have already enabled the "allow_url_fopen" on my server (which includes the "include"), but it still gives me an error. How do I execute it then?

    
answered by 24.07.2018 в 13:00