Last active
May 2, 2019 10:20
-
-
Save abdullahbutt/8e58e00d5491156fe7bd3e560f3d09a6 to your computer and use it in GitHub Desktop.
php curl to upload file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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