简体   繁体   English

如何上传一个有空格的文件是用curl + php命名的?

[英]How can I upload a file that has spaces is its name with curl + php?

I'm using this code for uploading a file to my server, using HTTP POST: 我正在使用此代码将文件上传到我的服务器,使用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. 当$ filepath不包含空格时,此代码有效。 However, it is possible that it might. 但是,有可能。 When I test a path with spaces, I get the curl error "failed creating formpost data". 当我用空格测试路径时,我得到卷曲错误“创建formpost数据失败”。

The curl manual does not tell me what to do, all it gives me are unix filenames without spaces. 卷曲手册没有告诉我该怎么做,它给我的是没有空格的unix文件名。 I tried following http://curl.haxx.se/mail/archive-2006-01/0079.html like so, but it didn't help me either: 我试过像http://curl.haxx.se/mail/archive-2006-01/0079.html这样,但它也没有帮助我:

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

Anyone have an idea? 有人有想法吗?

Current versions of cURL (and PHP-cURL) handle spaces in filenames with no problems. 当前版本的cURL(和PHP-cURL)处理文件名中的空格没有问题。 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. 我可以在PHP 5.2.13和Linux上的libcurl / 7.19.4下确认文件名中的空格完全没问题。

The error message you're getting is the same one as when PHP/cURL can't find a file. 您获得的错误消息与PHP / cURL无法找到文件时的错误消息相同。 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. 可能是文件名中空格不足或过度转义的问题,但可能不是cURL本身的问题。

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). 我建议在代码的其他地方查找问题或生成问题的硬编码示例(其中$ filename直接在PHP中设置而不是从其他地方读取)。

I faced the same problem and I couldn't upgrade CURL on the system. 我遇到了同样的问题,我无法在系统上升级CURL。

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. 所以我编写了一个简单的转义函数,它迭代字符串以替换+符号的空格字符,这是用空格表示URL的传统方式。

For my surprise, it worked. 令我惊讶的是,它奏效了。 :) :)

C++ code: C ++代码:

#include <algorithm>
#include <string>

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

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. 甚至在Windows上工作。 I'm pretty sure it will work on *NIX systems 我很确定它可以在* NIX系统上运行

Put the target file in a variable, such as 'while read', and pass the variable in the curl the command. 将目标文件放在变量中,例如“while read”,并在curl命令中传递变量。 Be sure the variable is in double quotes and curl will handle it. 确保变量是双引号,curl将处理它。

For example: 例如:

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

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM