Corrupt image when extract from zip - php

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

Related

Update local php file from remote php file

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

Archives downloaded with CURL not valid

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).

PHP open .txt file with full URL

I want to be able to read the contents of a file from its full domain and filepath, so something like:
http://www.example.com/files/file.txt
What I can't do in this instance is:
../files/file.txt
I have tried curl, fopen, file_get_contents and would rather use curl but cannot get it to work for any of them.
Is there an obvious reason why this isn't working that I am missing?
Here are the code snippets for each attempt, parhaps someone knows what's wrong with one of them?
Incidentally, if I could do ../files/file.txt it works for each option.
$file = "http://www.example.com/files/file.txt";
fopen:
$f=fopen($file,'r');
$data='';
while(!feof($f))
$data.=fread($f,$size);
fclose($f);
curl:
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$data = curl($file);
file_get_contents:
$data = file_get_contents($file);
Thanks in advance for all help.
file_get_contents works fine for URLs with PHP 4 >= 4.3.0 and PHP 5:
"A URL can be used as a filename with this function if the fopen wrappers have been enabled"
Many shared hosters have this option disabled, or are using an older version of PHP and are blocking loading external files using PHP's safe mode.
Start with enabling error_reporting and have a look at your hosters web site to see if he's blocking external files.
Perhaps url fopen are disabled on your hosting and urls from another domains are just disabled?
php.ini var allow_url_fopen must be on

file_exists not working for a file present in a file server

I am facing a issue such that i am able to link to a file that is present in an http server or a ftp server.. but i am not able to link to a file that is present in a file server..
ie, More Clearly..
if the URL is http://serverpath.com/images/image.jpg or ftp://serverpath.com/images/image.jpg or a remote path the file_exists function gives a true value but if a give a file server path like the similar path as //serverpath/public/images/image.jpg the file_exists function is giving a false value.
UPDATE
I am using PHP 5.4.4
There was a bug reported regarding this issue in the previous version of php ie below 5.3 and it was told that this issue has been solved in the versions above 5.3. Is there any change in the php.ini that we need to make change to enable. I searched but did not get the answer i expected
Please Help
use CURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http:\\www.somesite.com\somefile.html');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
curl_exec($ch) will return FALSE if no file is present or the actual data on success
However:
You'd better bind your shared folder to a logical hard drive with
net use E: \\servername\shareddirectory
doing this will allow you to access your remote file as it were on a local disk (say E:).
file_exists() should work if you give the "local" path E:\filepath\filename
Your php.ini allow_url_fopen is on?

Unzip file with php?

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!

Categories