CodeIgniter Forums
Read file to another server - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Read file to another server (/showthread.php?tid=90485)



Read file to another server - pippuccio76 - 03-25-2024

HI , i want read file from another server , the file are xls and  i want read with phpspreadsheet ,  i try to use curl :

Code:
        $client = \Config\Services::curlrequest([
            'baseURI' => 'https://myserver/',
        ]);

        // GET file
        $client->get('480060_08_06_2022.xls');

        $code  = $this->response->getStatusCode(); 
        $reason = $this->response->getReason(); 

but i have always $code equals to 200 and $reason equal to ok i f file doesn't exist too.

How can i check if file exist and read or download ?


RE: Read file to another server - kenjis - 03-25-2024

See https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#using-responses


RE: Read file to another server - pippuccio76 - 03-26-2024

(03-25-2024, 06:35 PM)kenjis Wrote: See https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#using-responses

the documentation doesn't explain how i can get the file ( to read or download) ,there is an example ?
i try to :

Code:
        $client = \Config\Services::curlrequest([
            'baseURI' => 'https://myserver/',
        ]);

        // GET file
        $client->request('GET','480060_08_06_2026.xls');

        $code  = $this->response->getStatusCode();
        $reason = $this->response->getReason();


        echo "<pre>";
        print_r($client->getBody());


But i return a 404

Edit :

i miss a x (xslx) now i haven't error , but how can i read the file ? print_r($client->getBody()) return a blank page


RE: Read file to another server - pippuccio76 - 03-26-2024

Why cannot i  use 
Code:
$file = new \CodeIgniter\Files\File($url);
?


RE: Read file to another server - kenjis - 03-26-2024

I recommend you read the user guide, because it tells everything you need in most cases.
https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#using-responses

PHP Code:
        $client = \Config\Services::curlrequest();

        $url      'https://www.cmu.edu/blackboard/files/evaluate/tests-example.xls';
        $response $client->request('GET'$url);

        $content $response->getBody();

        file_put_contents(WRITEPATH 'sample.xls'$content); 



RE: Read file to another server - pippuccio76 - 03-27-2024

(03-26-2024, 05:07 PM)kenjis Wrote: I recommend you read the user guide, because it tells everything you need in most cases.
https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html#using-responses

PHP Code:
        $client = \Config\Services::curlrequest();

        $url      'https://www.cmu.edu/blackboard/files/evaluate/tests-example.xls';
        $response $client->request('GET'$url);

        $content $response->getBody();

        file_put_contents(WRITEPATH 'sample.xls'$content); 


sorry but 

Code:
file_put_contents
it is not written in this page, I love codeigniter , but i think documentation it needs to be much improved , we need examples like
 Build your first application part...


RE: Read file to another server - kenjis - 03-27-2024

file_put_contents() is a basic PHP function. It is not CodeIgniter's function.
CodeIgniter is build on PHP, so you can use PHP functions,
and you should know PHP.

Also, you can improve the user guide. Because CodeIgniter is made by contributions.
https://codeigniter4.github.io/CodeIgniter4/libraries/curlrequest.html
You can see "Edit this page" in right of the top.