Skip to content

Instantly share code, notes, and snippets.

@abdullahbutt
Last active May 2, 2019 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdullahbutt/8e58e00d5491156fe7bd3e560f3d09a6 to your computer and use it in GitHub Desktop.
Save abdullahbutt/8e58e00d5491156fe7bd3e560f3d09a6 to your computer and use it in GitHub Desktop.
php curl to upload file
<?php
use Illuminate\Support\Facades\Log;
$imageUrl = $linPost->pic_1; // This is a complete URL Path
$pos = strrpos($imageUrl, '/');
$imageName = $pos === false ? $imageUrl : substr($imageUrl, $pos + 1);
$extension = substr(strrchr($imageUrl,'.'),1);
if($extension == 'mp4' || $extension == 'MP4' || $extension == 'mov' || $extension == 'MOV')
{
$path_image_save = public_path('assets/imagegallery/video_upload/' . $imageName);
}
else
{
$path_image_save = public_path('assets/imagegallery/image_upload/' . $imageName);
}
$path = $path_image_save;
$fp = fopen($path, 'rb');
$size = filesize($path);
//********** $token is a dynamic Token Data *************
$cheaders = array('Authorization: Bearer '.$token,
'Content-Type: application/octet-stream');
$ch = curl_init($uploadUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $cheaders);
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, $size);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
Log::info(curl_error($ch));
}
curl_close($ch);
fclose($fp);
//Log::info($response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment