How to upload a file with spaces - is it a name with curl + php?

I use this code to upload a file to my server using HTTP POST:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
    "upload" => '@' . $filepath
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$curl_result = curl_exec($ch);

This code works when $ filepath does not contain spaces. However, this is possible. When I check the path with spaces, I get a curl error "Failed to create shaping data."

The curl guide does not say what to do, all it gives me are unix files without spaces. I tried following http://curl.haxx.se/mail/archive-2006-01/0079.html , but that didn't help me either:

"upload" => '"@' . $filepath . '"'
"upload" => '@"' . $filepath . '"'

Does anyone have an idea?

+5
source share
5 answers

cURL ( PHP-cURL) . , , , . , .

, PHP 5.2.13 libcurl/7.19.4 Linux.

, , , PHP/cURL . , - . , , , , cURL.

( $filename PHP, ).

+1

realpath

$filepath = 'D:\7 habits copy.txt';
$url = 'http://www.speedyshare.com/upload.php?'.rand(10000000, 99999999);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
$post = array(
    "upload" => '@' . realpath($filepath)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$curl_result = curl_exec($ch);
echo $curl_result;

Windows. , * NIX

+1

, CURL .

, escape, +, URL- .

.:)

++:

#include <algorithm>
#include <string>

std::string s = "";
std::replace(s.begin(), s.end(), ' ', '+');    
+1

. , , :

'This is the file name with spaces.txt'

'This\ is\ the\ file\ name\ with\ spaces.txt'

,

, :

'This is the file name with spaces.txt'
-1

, "while read", curl . , , .

:

curl -T /PATH/TO/YOUR/FILE/DIRECTORY/"$f"
-1

All Articles