Reputation: 11
I'm trying to use the following code to transfer an image file from my website to a Rackspace Cloud Files container:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://storage101.dfw1.clouddrive.com/v1/MossoCloudFS/myContainer/myFile.png');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
$cFile = curl_file_create("myFolder/myFile.png", 'image/png','testpic');
$imgdata = array('myimage' => $cFile);
curl_setopt($ch, CURLOPT_POSTFIELDS, $imgdata);
$headers = array();
$headers[] = 'Content-Type: image/png';
$headers[] = 'X-Auth-Token: '.$myID;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
The file transfers and it appears to be the correct size, but it is corrupted. If I open the PNG file in a text editor, I see that it looks almost identical to the original image file but it has some extra text at the top and an extra line of text at the bottom as shown in bold below:
--------------------------454fae24975d785a Content-Disposition: attachment; name="myimage"; filename="testpic" Content-Type: image/png
‰PNG
IHDR `ï šMÝ pHYs
šœ OiCCPPhotoshop ICC profile xÚSgTSé=÷ÞôBKˆ€”KoR RB‹€‘&*!Jˆ!¡ÙQÁEEÈ ˆŽŽ€ŒQ,Š Ø ä!¢Žƒ£ˆŠÊûá{£kÖ¼÷æÍþµ×>ç¬ó³Ï À–H3Q5€©BàƒÇÄÆáä.@ ... ... --------------------------454fae24975d785a--
Is there a way to send just the PNG image file without injecting this extra text?
Upvotes: 0
Views: 51
Reputation: 11
Thanks to everyone that helped guide me through this especially @ADyson. Using their link I was able to get the following code to transfer my PNG file properly:
#Initiate cURL object
$ch = curl_init();
#Set your URL
curl_setopt($ch, CURLOPT_URL, 'https://storage101.dfw1.clouddrive.com/v1/MossoCloudFS_..../myContainer/myFile.png');
#Indicate, that you plan to upload a file
curl_setopt($ch, CURLOPT_UPLOAD, true);
#Indicate your protocol
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
#Set flags for transfer
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$headers = array();
$headers[] = 'Content-Type: image/png';
$headers[] = 'X-Auth-Token: '.$myID;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
#Set HTTP method to PUT
curl_setopt($ch, CURLOPT_PUT, 1);
#Indicate the file you want to upload
curl_setopt($ch, CURLOPT_INFILE, fopen('myFolder/myFile.png', 'rb'));
#Indicate the size of the file (it does not look like this is mandatory, though)
curl_setopt($ch, CURLOPT_INFILESIZE, filesize('myFolder/myFile.png'));
#Execute
curl_exec($ch);
curl_close($ch);
Upvotes: 1