Welcome Guest, Not a member yet? Register   Sign In
Downloading a file from a remote server
#1

[eluser]ChrisF79[/eluser]
I'm building a real estate site and the data sits in a zip file on a remote server. What's the easiest way to download the file, place it in a temp directory and unzip it?
#2

[eluser]boltsabre[/eluser]
From google:
http://www.google.com/search?q=Downloadi...ote+server

From Stackoverflow:
http://stackoverflow.com/questions/45042...ve-locally

Google is your friend!
#3

[eluser]ChrisF79[/eluser]
[quote author="boltsabre" date="1335444002"]
Google is your friend![/quote]
I googled already. The thing is, none of those answers involve CodeIgniter. I was hoping there was a clever way of doing it using CI.
#4

[eluser]bretticus[/eluser]
[quote author="ChrisF79" date="1335459805"][quote author="boltsabre" date="1335444002"]
Google is your friend![/quote]
I googled already. The thing is, none of those answers involve CodeIgniter. I was hoping there was a clever way of doing it using CI.[/quote]

Hi ChrisF79,

CodeIgniter doesn't try to substitute for most intrinsic PHP functionality. It's built on the premise that MVC is a better paradigm for Web development and provides tools for that. In addition there are many helpers/shortcuts for common use cases where shortcuts are feasible.

In this case, it's fairly simple to download a remote file already with PHP. You can use any of the fopen wrapper functions provided that allow_url_fopen is enabled. Although, I typically go the cURL route (my dev environs typically do not have allow_url_fopen enabled and cURL gives error handling capabilities), you can even use the copy function to download a file. Doesn't get any easier than that! Smile

My suggestion is to use an intrinsic download method and wrap it all up in a helper. That is, if you anticipate calling it more than once in your application. Even if you only call it once, a helper is a great way to organize code and make global changes anywhere application code calls your helper function. You can set your paths in your helper function(s) from a constant and make your application easier to maintain for the future (for example.)

Uncompressing a zip file, however is a different story. Typically you either have to have a PHP extension enabled or a complex code base. This is the kind of thing that CI provides for some instances. And, as luck would have it, they do! (I am referring to the $this->zip->get_zip() method call FYI. Please note, I have never used this CI class as I haven't had that use case pop up yet.)


Cheers.
#5

[eluser]ChrisF79[/eluser]
[quote author="bretticus" date="1335462926"][quote author="ChrisF79" date="1335459805"][quote author="boltsabre" date="1335444002"]
Google is your friend![/quote]
I googled already. The thing is, none of those answers involve CodeIgniter. I was hoping there was a clever way of doing it using CI.[/quote]

Uncompressing a zip file, however is a different story. Typically you either have to have a PHP extension enabled or a complex code base. This is the kind of thing that CI provides for some instances. And, as luck would have it, they do! (I am referring to the $this->zip->get_zip() method call FYI.[/quote]
Thanks so much for the reply. The thing is though, that zip class looks to be for creating zip archives and not unzipping an existing zip file on the server.
#6

[eluser]CroNiX[/eluser]
zip_open ( string $filename )
http://www.php.net/manual/en/function.zip-open.php
Doesn't get much simpler than that.
#7

[eluser]bretticus[/eluser]
Quote:Thanks so much for the reply. The thing is though, that zip class looks to be for creating zip archives and not unzipping an existing zip file on the server.

Ah yep, you're right I'm sure. I was thinking the read_file method might read an already zipped file. Perhaps not.

[quote author="CroNiX" date="1335472996"]zip_open ( string $filename )
http://www.php.net/manual/en/function.zip-open.php
Doesn't get much simpler than that.[/quote]

Just make sure you have the zip extension enabled though (this is the extension I was referring to.) But yes, extremely easy if it's available. If not, you could also use a shell command to use a "native" binary program (like unzip for example.)




Theme © iAndrew 2016 - Forum software by © MyBB