I have a scrip that downloads a zip archive and I need to extract the contents to the directory that zip archive is in. I have tried various things, this being the last:
mkdir("/home/site/public_html/".$db."", 0777);
$url = 'http://wordpress.org/latest.zip';
$path = "/home/site/public_html/".$db."/latest.zip";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
$zip = new ZipArchive;
$zip->open("/home/site/public_html/".$db."/latest.zip");
$zip->extractTo("/home/site/public_html/".$db."/");
$zip->close();
The zip files downloads just fine but it won't extract. Is there another way I can extract the files?
This sounds like a permissions error; it's common for many hosting providers (and good security practice) for the web user (what PHP is running as) to to have limited permissions, such as no write within web directories. See if you can get more information on the failure by upping the error_reporting level (http://php.net/manual/en/function.error-reporting.php), and if this is the issue, it can be solved with suexec. (http://www.alain.knaff.lu/howto/PhpSuexec/) Be careful!
Related
I am working on a CMS that will be installed for many clients, but as I keep on improving it I make modifications to a few files. I want these updates to be automatically applied to all the projects using the same files.
I thought of executing a check file every time the CMS is opened. This file would compare the version of the local file with the remote file, for this I can keep a log or something for the versions, no big deal, but that's not the problem, here is some sample code I thought of:
$url = 'http://www.example.com/myfile.php';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$data = curl_exec($curl);
curl_close($curl);
The problem is getting the content of myfile.php since its a PHP file the server will execute it and return the output, but I want the actual content of the file. I understand that this is not possible as it would be a security problem, anybody would be able to get the php code of other sites, but is there any way to get the contents of a remote php file maybe by giving special permissions to a remote connection?
Thanks.
You should create a download script on your remote server which will return the original php code by using readfile().
<?php
$file = $_SERVER['DOCUMENT_ROOT'] . $_GET['file'];
// #TODO: Add security check if file is of type php and below document root. Use realpath() to check this.
header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=\"$file\"");
readfile($file);
?>
Get file contents by fethcing http://example.com/download.php?file=fileName
I have a PHP script that downloads automatically some zip files from certain URLs with cURL functions.
But there's a problem: zip archives downloaded with CURL, if opened with Windows native Zip Extractor, it gives me an "invalid archive" error. If I download the zip file from URL with my browser, it is ok.
For example: zip downloaded with CURL is 21,8 Kb and the one downloaded from browser is 21,4 Kb.
Here's my Curl Setup:
curl_setopt($this->ch, CURLOPT_URL, $link);
curl_setopt($this->ch, CURLOPT_HEADER, TRUE);
$data = curl_exec($this->ch);
Then I save the file ($data) locally on my website like this:
$file = fopen($full_path, "w+");
fputs($file, $data);
fclose($file);
With WinRar both zips are fine, but I need the script to download zip files that are 100% valid.
Can anyone help me with this?
Figured out the solution: CURLOPT_HEADER must be set to false, otherwise it will write HTTP headers in response (and so in my zip files).
I trying download a zip file using curl from one virtual host to another, in a same server. Zip file contains *.php and *.jpg files.
The problem is: sometimes JPG files get corrupt, like this:
Here is my code :
$out = fopen(ABSPATH.'/templates/default.zip','w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, 'http://share.example.com/templates/default.zip');
curl_exec($ch);
curl_close($ch);
$zip = new ZipArchive;
if ($zip->open(ABSPATH.'/templates/default.zip') === TRUE)
{
if($zip->extractTo(ABSPATH.'/templates'))
{
echo 'OK';
}
$zip->close();
}
//$zip->close();
I don't understand what happen to my jpg. I also tried using pclzip.lib.php, but no luck. How to solve this problem ?
Thanks in advance
Have you tried downloading the file via curl and unzipping it normally (i.e. without php)? To figure out whether the download causes the problem or the unzip.
You might also try to replace one of both parts using shell_exec (wget instead of curl, unzip instead of ZipArchive). I mean just for debugging, not for production maybe.
Finally i found what is the problem.
I'm using Nginx web server, when i change nginx config files:
sendfile on;
became
sendfile off;
My image not corrupt anymore. So its not php or curl problem. Interesting article: http://technosophos.com/node/172
I am a realtor/web designer. I am trying to write a script to download a zip file to my server from a ftp server. My website is all in php/mysql. The problem that I am having is that I cannot actually log in to the ftp server. The file is provided through a link that is available to me, but access to the actual server is not available. Here is the link i have access to.
ftp://1034733:ze3Kt699vy14#idx.living.net/idx_fl_ftp_down/idx_ftmyersbeach_dn/ftmyersbeach_data.zip
php's normal functions for accomplishing this give me a connection error. Php does not have permission to access this server... Any solutions to this problem would be a life saver for me. I am looking to use a cron job to run this script every day so i don't have to physically download this file and upload it to my (godaddy) server, which is my current solution (not a good one, i know!).
Also, I can figure out how to unzip the file myself, as I have done some work with php's zip extension, but any tips for an efficient way to do this would be appreciated as well. I am looking to access a text file inside of the zip archive called "ftmyers_data.txt"
Do you have shell access to the server on which your PHP application runs? If so, you might be able to automate the retrieval using a shell script and the ftp shell command.
Use php_curl
$curl = curl_init();
$fh = fopen("file.zip", 'w');
curl_setopt($curl, CURLOPT_URL, "ftp://1034733:ze3Kt699vy14#idx.living.net/idx_fl_ftp_down/idx_ftmyersbeach_dn/ftmyersbeach_data.zip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
fwrite($fh, $result);
fclose($fh);
curl_close($curl);
Easy as pi. :)
I have a folder which php uploads to located in the directory above www (/home/user/upload). I have made the folders permissions 770 and made a custom group, which includes user apache and nobody, to own it.
When I do a chgrp mygrp upload, php is able to upload to it, but after it does one time... it all of a sudden can't write or read from it anymore...
Anyone have any ideas?
====EDIT====
Here is the code I'm currently using to dl a remote file:
$ch = curl_init($url);
$image = TMP_DIR.'/'.getRandomString(10).".{$info->extension}";
while (file_exists($image))
$image = TMP_DIR.'/'.getRandomString(10).".{$info->extension}";
$image_handle = fopen($image, 'w');
curl_setopt($ch, CURLOPT_FILE, $image_handle);
if (curl_exec($ch) === false)
throw new Exception('Curl error: '.curl_error($ch), curl_errno($ch));
curl_close($ch);
Well i feel a bit stupid now... after adding and changing users and groups I never restarted php or apache. So if anyone else ever has this problem make sure you do that first.
Thanks for your time and help guys!