Can't upload image using curl - php

I want to upload images from my Server to another Server with php curl. I tested my script on localhost it works but on server it not working.
Here are my steps:
First step
I upload an image to my server. Path of the image is "imgs/myimage.jpg"
Second step
I excute this code for upload image from my server to another server.
$file_name_with_full_path = realpath("imgs/myimage.jpg");
$postdata = array('myfile'=>'#'.$file_name_with_full_path);
$headers = array("Content-Type:multipart/form-data");
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, "http://www.secondserver.com/image.php");
$data = curl_exec($ch);
curl_close($ch);
This code in image.php from second server.
print_r($_FILES["myfile"]);
I tested this code on localhost it works. But on Server it not working and display error.
Notice: Undefined index: myfile in ....
I try to get full path of the image on the server with command
echo realpath("imgs/myimage.jpg");
It shows this:
#/home/admin/domains/mydomain.com/public_html/member/imgs/myimage.jpg
I'm not sure what it is about Permission? But I have also set up file permissions are 777.
What should I do to fix this problem?
I'm trying to find a solution for the entire day, but was not successful.
Update
I apply the code to test on another server. It worked.
I try to get full path of the image on the server with command
echo realpath("imgs/myimage.jpg");
It shows this:
#/var/www/vhosts/myuser/mydomain.com/imgs/myimage.jpg
I think probably about Config Server.
Should I set up?

Not sure if you updated code for posting here but $postdata is never set before. In your 2nd line, you have $postimg = array('myfile'=>'#'.$file_name_with_full_path);
and later you do curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
Please use the correct variable ($postimg) and you should be good to go. Wondering how it worked on local :)

Related

Can't load file from URL, but works with local path

I am trying to load an XML file, but for some reason I can't use a URL.
allow_url_fopen is set to On.
I used this, which didn't work:
$xml = simplexml_load_file("https://example.corn/test.xml");
But this does work:
$xml = simplexml_load_file("../test.xml");
I also tried file_get_contents:
$xml = file_get_contents("https://example.corn/test.xml");
and even cURL:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.corn/test.xml");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
curl_close($ch);
For each example I am displaying the output with var_dump($xml);, which always shows bool(false) when using a URL and shows the correct XML data when using a local path.
I would just use the local path, but this is just for a test and the actual XML file I am going to fetch is from a different site.
I tested it on a local server and it worked, but on my hosted server, it doesn't work at all. They are using the same php.ini. My hosted server is using php-fpm, I'm unsure if that could be a factor, or if there is something that has to be changed with it.
This might be a configuration error that I am unaware of, because the code should work, and the link definitley exists, because I can click on it and see the correct xml in the browser.
What could be causing this issue?
Turns out that I had my openssl.cafile property set to some location that didn't exist.
For some reason no errors were showing when using the commands above.
I think the specified url is not valid or doesn't exist
Read about curl in the PHP docs
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);

API not work on Third Level Domain

i have some problems.
I need to keep data from coinmarketcap. When I was developing on localhost it worked well.
But on third level domain coinfollow.altervista.org i can not receive the data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.coinmarketcap.com/v1/ticker/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
curl_close($ch);
$outputdecoded = json_decode($output, true);
echo $outputdecoded;
I try in another domain mywebsite.com and it worked. I think that the problem is coinfollow.altervista.org domain.
I need to save coinmarketcap data into my database with a simple query.
Does anyone know a solution?
It sounds like there is a server config issue on coinfollow.altervista.org, such as curl not being enabled for php.
You can run phpinfo(); to see if curl is installed.
If it is installed try running echo curl_error($ch) to see if there are any errors returning from curl

Send data from server to another by curl or any other way

I have two servers.
The first (Local) is the server which has the script.
The second (Remote) is the server which has uploaded files only (just for storage).
Now I'm confused about how to upload the files, and I have 2 ways and I don't know what the best one of them.
The ways:
Upload the file to the local server first and make all security
operations like (check file size,file type, and so on), then upload
again to the second server (remote server).
Upload the file to the second server (remote server) directly,
and make all checks and security operations in there, then send the file information to the first server (local) to store the info into database.
Or there is another way is better than them ?
I have tried to apply the second way, by send the file directly to the second server (remote server) and then after everything is ok, the second server (remote server) send to the first server (local) all information about the uploaded file by curl.
For example:
The following code written in file exists in php file in the second server (remote server).
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://localServer/script/receiveReques.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_POST, 3);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('fileName' => 'filename.zip', 'size' => 1000, 'path'=> 'http://remoteserver.com/files/filenmae.zip'));
curl_exec($ch);
curl_close($ch);
But the problem I don't know how to get the sent information.
The summary of my questions is:
What the best way to do that ?
How to receive the file information from the second server (remote
server) that has been sent by curl ?
you have
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
all you need to do is use the data it returns
$data = curl_exec($ch);
all of the data is now in $data

Unable to save picture from url to my server

I'm try to save some pictures from url to my server, but i'm not able to do it.
this is my code (it's standard, i've found on internet):
$ch = curl_init($url);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
but for each link i put inside $url variable, on my server is always saved a 26 byte image (that's not the original image).
What's wrong?
I can successfully download image using your curl code. It can happen that your server is not allowed to connect the outside web links.
This is a curl equivalent code that download images as well. I believe from your server, you can not download image using this code.
file_put_contents("img.jpg", file_get_contents("http://www.letteratu.it/wp-content/uploads/2014/03/cielo.jpg"));
Run your curl with verbose mode to see the curl's debug messages, and show that us.
curl_setopt($ch, CURLOPT_VERBOSE, 1);
I'm pretty sure you need to include the http:// in the URL. I'm fairly certain it thinks that it is a local file without it (i.e., an implicit file://).

Server ignores cUrl code lines php

iam using that php code in the localhost and working fine
$ch = curl_init($src);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
when i move it to the remote server the script ignores that code
and doesn't give me a result (still giving me the original url )
note: i use cURL here to give me the true url after redirect
Maybe there is some difference in the configuration of the remote server.
phpinfo();
Shows the configuration of php and of the curl module

Categories