So I uploaded on server an folder .zip using curl :
function uploadCurs(){
$ch = curl_init("http://myServer.com/service.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CUROPT_POSTFIELDS, "archive = ".NOM_OF_ARCHIVE);
echo curl_exec($ch);
}
Now, how to test if upload with succes and how to download this .zip?
Help me please!!! Thx in advance
You could use the file_exists function. See here for manual
You could do something like this:
<?php
$filename = '/path/to/foo.zip';
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
Related
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 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.");
}
?>
How can I upload a file, either by using cURL or anything else, in PHP?
In other words, the user sees a file upload button on a form, the form gets posted to my PHO script, then my PHP script needs to re-post it to another script (eg on another server).
I have this code to receive the file and upload it:
echo"".$_FILES['userfile']."";
$uploaddir = './';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if ( isset($_FILES["userfile"]) ) {
echo '<p><font color="#00FF00" size="7">Uploaded</font></p>';
if (move_uploaded_file
($_FILES["userfile"]["tmp_name"], $uploadfile))
echo $uploadfile;
else echo '<p><font color="#FF0000" size="7">Failed</font></p>';
}
How can I send the file to the receiver server?
Use:
if (function_exists('curl_file_create')) { // php 5.5+
$cFile = curl_file_create($file_name_with_full_path);
} else { //
$cFile = '#' . realpath($file_name_with_full_path);
}
$post = array('extra_info' => '123456','file_contents'=> $cFile);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result=curl_exec ($ch);
curl_close ($ch);
You can also refer:
http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/
Important hint for PHP 5.5+:
Now we should use https://wiki.php.net/rfc/curl-file-upload but if you still want to use this deprecated approach then you need to set curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
I am going to have a php that upload json to the web.
Here is my file to upload (located in my local drive)
test.php
$string = file_get_contents("results.json");
$ch = curl_init();
$post_values = array('json_data' => $string);
curl_setopt($ch, CURLOPT_URL, '<dir path>/test1.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_values);
$data = curl_exec($ch);
if (!curl_errno($ch)) {
echo 'Received raw data' . $data;
}
curl_close($ch);
?>
This is the code to get the posted json
test1.php
<?php
$data = json_decode($_POST['json_data'], true);
header('Content-type: text/json');
foreach ($data['queue'] as $queue) {
echo "Wof = " . $queue['wof'];
echo "Input Date = " . $queue['input_date'] . "<br />";
}
?>
Both code works well.
But when I upload test1.php to the web server, it means I send my json located in my computer to the server. I doesn't work.
No Error msg I get, only it does not show the data. only show Received raw data
My question is : How to fix it?, is there something wrong with my code?
Thanks in advance.
I don't know what wrong why I cannot put the image in my folder?
Here's my php code:
<?php
$img = 'http://ecx.images-amazon.com/images/I/41pg1dHauUL._SL75_.jpg';
$target_path = 'product-images/';
$target_path= $target_path.basename($img);
if(move_uploaded_file($img,$target_path)){
echo 'Success';
}
else {
echo 'Error.';
}
?>
How about fetching the image using cURL and then saving it to the database as a BLOB?
From what I can tell in your code, you're not actually getting the image. You can download the image source to your server in several ways.
$image = file_get_contents($URL);
or
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
I believe that cURL is faster, if that matters. Then save the image source in a file,
file_put_contents( '/path/to/file/myimage.jpg', $image );
I hope that helps.