6

I'm using this code for uploading 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, it is possible that it might. When I test a path with spaces, I get the curl error "failed creating formpost data".

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

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

Anyone have an idea?

2
  • Rename the file so it has no spaces in the filename?! Is this an option :-)?
    – strauberry
    Apr 14, 2011 at 11:49
  • Sorry, no, the path could also contain spaces, and i don't know if i'll have a /tmp folder on the server this script will be put.. (to make a local copy) :-(
    – Tominator
    Apr 14, 2011 at 11:53

5 Answers 5

2

Current versions of cURL (and PHP-cURL) handle spaces in filenames with no problems. Older versions may have had bugs, I'm not sure. Check you're using reasonably recent versions.

I can confirm spaces in filenames are no problem at all under PHP 5.2.13 and libcurl/7.19.4 on Linux.

The error message you're getting is the same one as when PHP/cURL can't find a file. Odds are the problems are somewhere else in your code. Probably an issue of having under- or over-escaped the spaces in the filename but probably not an issue with cURL itself.

I'd suggest looking elsewhere in your code for the problem or producing a hard-coded example of the problem occurring (where $filename is being set in the PHP directly rather than read from elsewhere).

3
  • I'm on PHP Version 5.2.9-2 (windows) with libcurl/7.19.4 OpenSSL/0.9.8k zlib/1.2.3, so i would expect it to work too. I tried setting the file hardcoded, but it didn't help..
    – Tominator
    Apr 14, 2011 at 12:50
  • Update: i output this: echo "file: " . (file_exists(realpath($filepath)) ? 'yes' : 'no'); and get "no".. However, i can open the file just fine when i copy paste the path in Explorer.. Mysterious!
    – Tominator
    Apr 14, 2011 at 12:52
  • Okay.. fixed. I was trying to upload a file from "c:\users\my user name". PHP has no right to enter that folder.. so yes, with "c:\temp\path with spaces\" it does work!
    – Tominator
    Apr 14, 2011 at 13:00
1

You need the 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;

Worked even on Windows. I'm pretty sure it will work on *NIX systems

7
  • realpath made no difference, unfortunately :(
    – Tominator
    Apr 14, 2011 at 12:49
  • what is your curl_errno($ch) ? Place it after curl_exec sentence.
    – Nemoden
    Apr 14, 2011 at 12:50
  • The error is 26: CURLE_READ_ERROR (26) There was a problem reading a local file or an error returned by the read callback. Probably related to php not being able to find the file as well (see other answer)..
    – Tominator
    Apr 14, 2011 at 12:54
  • @Tominator Maybe your web user simply don't have enough privileges to read file you've chosen to upload w/ curl?
    – Nemoden
    Apr 14, 2011 at 12:57
  • @Tominator, could you provide output of var_export(realpath($filepath));
    – Nemoden
    Apr 14, 2011 at 12:58
1

I faced the same problem and I couldn't upgrade CURL on the system.

So I wrote a simple escape function that iterated on the string to replace the space character for the + sign, which is the traditional way to represent URLs with spaces.

For my surprise, it worked. :)

C++ code:

#include <algorithm>
#include <string>

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

You will need to escape the spaces. Not sure of the syntax you would use but maybe something like this:

'This is the file name with spaces.txt'

to

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

again I'm not sure of the syntax it's just an example

or you might just need to add quotes to the file name, like this:

'This is the file name with spaces.txt'
1
  • No, i'm sorry, all of these strategies fail :(
    – Tominator
    Apr 14, 2011 at 12:22
-1

Put the target file in a variable, such as 'while read', and pass the variable in the curl the command. Be sure the variable is in double quotes and curl will handle it.

For example:

curl -T /PATH/TO/YOUR/FILE/DIRECTORY/"$f"
1
  • My answer is correct. I don't get why it is showing -1. Tripp Kinetics arbitrarily down voting? anyone can easily prove my answer works.
    – Warren W
    Mar 19, 2015 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.