I am trying to write some php (new to php) that downloads a zip file from a remote server using curl and unzips it to a wordpress theme directory. The unzipping through php is failing reporting a result of 19 which from what I've found indicates no zip file. However when I check the directory, the zip file is there. If I unzip it I end up with a zip.cpgz file. I'm not sure if this is a problem with my code or if it's a way the server is sending the file. Here's my code. Thanks.
$dirpath = dirname(__FILE__);
$themepath = substr($dirpath, 0, strrpos($dirpath, 'wp-content') + 10)."/themes/";
//make call to api to get site information
$pagedata = file_get_contents("https://ourwebsite/downloadzip.php?industryid=$industry");
$jsondata = json_decode($pagedata);
$fileToWrite = $themepath.basename($jsondata->zip_file_url);
$zipfile = curl_init();
curl_setopt($zipfile, CURLOPT_URL, $jsondata->zip_file_url);
curl_setopt($zipfile, CURLOPT_HEADER, 1);
curl_setopt($zipfile, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($zipfile, CURLOPT_BINARYTRANSFER, 1);
$file = curl_exec($zipfile);
if ($file == FALSE){
echo "FAILED";
}else{
$fileData = fopen($fileToWrite,"wb");
fputs($fileData,$file);
}
curl_close($zipfile);
if (file_exists($fileToWrite)){
$zip = new ZipArchive;
$res = $zip->open($fileToWrite);
if ($res === TRUE)
{
$zip->extractTo($themepath);
$zip->close();
echo 'Theme file has been extracted.';
}
else
{
echo 'There was a problem opening the theme zip file: '.$res;
}
}
else{
echo("There was an error downloading, writing or accessing the theme file.");
}
This should do it:
<?php
set_time_limit(0);
$industry = "industryid"; //replace this
$url = "https://ourwebsite/downloadzip.php?industryid=$industry";
$tmppath = "/tmp/tmpfile.zip";
$themdir = "/your/path/wp-content/themes/";
$fp = fopen ($tmppath, 'w+');//This is the file where we save the zip file
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $fp); // write curl response to file
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch); // get curl response
curl_close($ch);
fclose($fp);
if (file_exists($tmppath)){
$zip = new ZipArchive;
$res = $zip->open($tmppath);
if ($res === TRUE)
{
$zip->extractTo($themdir);
$zip->close();
echo 'Theme file has been extracted.';
}
else
{
echo 'There was a problem opening the theme zip file: '.$res;
}
}
else{
echo("There was an error downloading, writing or accessing the theme file.");
}
?>
Related
I want to ask
So I have a function to download all files stored in the folder according to the user ID (in zip format using ZipArchive).
it works when i run in xampp locally, windows or cpanel shared hosting. but when i want to move it to Apache and Redhat, all downloaded files are corrupt, the all file inside zip become 0 Kb. All the file names in the zip are correct, but the size is 0kb
here is my code
$url = url('')."/storage/uploads/file/".$id_pra."/";
//$url = "https://www.mbsb.insko.my/public/storage/uploads/file/".$id_pra."/";
$zip = new \ZipArchive();
$uploaddir = public_path().'/tmp';
$tmp_file = tempnam($uploaddir,'');
$zip->open($tmp_file, \ZipArchive::CREATE);
foreach($files as $file){
$url2 = $url.$file->upload;
$url2 = str_replace(' ', '%20', $url2);
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$output = curl_exec($ch);
curl_close($ch);
$download_file = $output;
$type = substr($url2, -5, 5);
#add it to the zip
$zip->addFromString(basename($url.$file->upload),$download_file);
}
// close zip
$zip->close();
# send the file to the browser as a download
ob_start();
$strFile = file_get_contents($tmp_file);
header('Content-disposition: attachment; filename=DOC-'.$id_pra.'-'.'.zip');
header('Content-type: application/zip');
echo $tmp_file;
while (ob_get_level()) {
ob_end_clean();
}
chmod($tmp_file, 0755);
readfile($tmp_file);
exit;
I tried to download a file via Icecat. The file need an access to be downloaded.
My problem is :
the file_get_content does'nt download the file. My directory is on 777 and the path is correct.
if I insert the document inside the directory, the file is not unzipped.
public function getIceCatFile() {
set_time_limit (0);
$url = 'https://data.Icecat.biz/export/freexml/EN/daily.index.xml.gz';
$context = stream_context_create(array('http' => array( 'header' => "Authorization: Basic " . base64_encode($this->username . ":" . $this->password) )));
if ($this->checkDirectoryIceCat() === true) {
// does'nt download the file inside the server directory
file_get_contents($url, true, $context);
$file = $this->IceCatDirectory . 'daily.index.xml.gz';
if (is_file($file)) {
$zip = new \ZipArchive;
// error failed same if I include the file inside the directory
$icecat_file = $zip->open($this->IceCatDirectory . 'files.index.xml.gz');
if ($icecat_file === true) {
$zip->extractTo($icecat_file);
$zip->close();
echo 'file downloaded and unzipped';
} else {
echo 'failed';
}
} else {
echo 'error no file found in ' . $file;
}
}
}
Make a try like this to download file that has username and password authentication using PHP Curl,
$localfile = fopen($file_store_locally, 'wb'); // open with write enable
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $localfile);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "username:password"); // see here for auth
curl_exec($ch);
curl_close($ch);
fclose($localfile); //store file data to local file
I am using hostmonster as hosting provider.
I have written below code.
$mp4_url = 'http://www.w3schools.com/html/mov_bbb.mp4';
$filename = 'sample.mp4';
file_put_contents( $filename , fopen($mp4_url, 'r' ) );
I am seeing strange behaviour. Sometimes it works but sometimes it copies only few bites like sometimes size of file is 100kb sometimes 600kb sometimes it copies whole file.
Please suggest what should I do to copy any mp4 file from any server to our server.
We have to copy large files, size can be 600MB or 1GB.
Try copy(). This will do your job:
$file = 'http://www.w3schools.com/html/mov_bbb.mp4';
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/sample.mp4';
if ( copy($file, $newfile) ) {
echo "Copy success!";
}else{
echo "Copy failed.";
}
use curl rather than using file_put_contents
$url='http://www.w3schools.com/html/mov_bbb.mp4';
$saveto='path';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$raw = curl_exec($ch);
curl_close($ch);
if (file_exists($saveto)) {
unlink($saveto);
}
$fp = fopen($saveto, 'x');
fwrite($fp, $raw);
fclose($fp);
I am using php to get an image from url and copy it to my server but got an error saying there is no such file
Image example:
http://www.google.co.in/intl/en_com/images/srpr/logo1w.png
Here is the solution I am using:
//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
I am getting the following error. Did i do anything wrong?
fopen(/location/to/save/image.jpg): failed to open stream: No such file or directory
✓ This worked for me.
Try it without the /location/to/save/
The file will be saved in the same folder you run the script in.
Such as:
<?php
//Get the file
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
//Store in the filesystem.
$fp = fopen("image_google.jpg", "w");
fwrite($fp, $content);
fclose($fp);
?>
function download_image($url,$destination_path = '')
{
// CHECKS IF CURL DOES EXISTS. SOMETIMES WEB HOSTING DISABLES FILE GET CONTENTS
if (function_exists('curl_version'))
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
} else
{
$content = file_get_contents($url);
}
// CHECKS IF DIRECTORY DOESNT EXISTS AND DESTINATION PATH IS NOT EMPTY
if(!file_exists($destination_path) && $destination_path != ''){
mkdir($destination_path, 0755, true);
}
// ATTEMPT TO CREATE THE FILE
$fp = fopen($destination_path.'/'.date('YmdHis').".jpg", "a+");
fwrite($fp, $content);
fclose($fp);
}
download_image('http://davidwalsh.name/wp-content/themes/jack/images/treehouse-1.png','images');
The folder (/location/to/save in here) should exist. You also need write permissions in it.
I am using this curl script to try to upload user selected files by FTP. It uploads the files to the server but they are all blank. Why is this happening?
if (!empty($_FILES['userfile']['name'])) {
$ch = curl_init();
$localfile = $_FILES['upload']['tmp_name'];
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp-addy-here'.$_FILES['userfile']['name']);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);
if ($error_no == 0) {
$error = 'File uploaded succesfully.';
} else {
$error = 'File upload error.';
}
} else {
$error = 'Please select a file.';
}
echo $error;
Line 3, right now it says:
$localfile = $_FILES['upload']['tmp_name'];
Change it to:
$localfile = $_FILES['userfile']['tmp_name'];
You didn't check if the initial file upload succeeded. Checking for the presence of the remote filename is NOT an indication that it got uploaded. The 100% reliable method to ensure the upload worked is
if ($_FILES['userfile']['error'] === UPLOAD_ERR_OK) {
... ftp stuff ...
} else {
die("Upload failed with error code: {$_FILES['userfile']['error']}");
}
The codes/constants are documented here: http://php.net/manual/en/features.file-upload.errors.php