Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP CURL PUT from file path

Tags:

put

php

curl

I'm trying to do a CURL PUT with a file but I'm having issues.

Here is my code:

$url_path_str = 'http://my_url';
$file_path_str = '/my_file_path';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);

$fh_res = fopen($file_path_str, 'r');
$file_data_str = fread($fh_res, filesize($file_path_str));

curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));
fclose($fh_res);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);

The script keeps timing out and I'm not sure why.

I'd appreciate some assistance. Thanks.

like image 263
ObiHill Avatar asked Sep 07 '11 21:09

ObiHill


1 Answers

I figured this out. It happened to be fclose that was causing the issue. I simply put it after curl_exec.

Here's the amended code:

$url_path_str = 'http://my_url';
$file_path_str = '/my_file_path';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, ''.$url_path_str.'');
curl_setopt($ch, CURLOPT_PUT, 1);

$fh_res = fopen($file_path_str, 'r');

curl_setopt($ch, CURLOPT_INFILE, $fh_res);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file_path_str));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curl_response_res = curl_exec ($ch);
fclose($fh_res);

Hope this is helpful to someone else later.

Cheers.

like image 71
ObiHill Avatar answered Oct 14 '22 20:10

ObiHill