php curl: (26) 无法打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23730283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:53:45  来源:igfitidea点击:

curl: (26) couldn't open file

phpapicurlbox-api

提问by halkujabra

I am getting this error, when I am trying to call a box api through curl.

当我尝试通过 curl 调用 box api 时,出现此错误。

curl: (26) couldn't open file

Can't find why! I am calling this api with a correct file name-

找不到原因!我用正确的文件名调用这个 api-

curl https://upload.view-api.box.com/1/documents \
-H "Authorization: Token YOUR_API_TOKEN" \
-H "Content-type: multipart/form-data" \
-F file=@A_correct_file_name

I have seen all the three already asked questions but 2 of them are unanswered and one is specific to facebook.

我已经看到了所有三个已经问过的问题,但其中两个没有答案,一个是特定于 facebook 的。

cURL error 26 couldn't open file

cURL 错误 26 无法打开文件

Fatal error: Uncaught CurlException: 26: couldn't open file "" thrown in

致命错误:未捕获的 CurlException:26:无法打开文件“”

Getting Fatal Error Uncaught CurlException: 26: couldn't open file

获取致命错误未捕获的 CurlException: 26: 无法打开文件

采纳答案by halkujabra

Sorry guys! My bad. I had not included extension of the file in the file name. After including it, it worked. I am answering this in case someone does the same mistake in future.

对不起大家!我的错。我没有在文件名中包含文件的扩展名。包含它后,它起作用了。我正在回答这个问题,以防将来有人犯同样的错误。

回答by Guilherme Salomé

I was having a similar problem after changing to PHP 5 (I was using the @upload method and it was deprecated, so I had to start using CURLFile), and the solution to my problem was found in this stack.

改用PHP 5后也遇到了类似的问题(我用的是@upload方法,已经弃用了,所以不得不开始使用了CURLFile),我的问题的解决方法是在这个stack中找到的。

Solution:curl upload won't work with relative paths, use the full path instead

解决方案:curl 上传不适用于相对路径,请改用完整路径

回答by Luis Cruz

I was having this problem this morning but I solved with this

我今天早上遇到了这个问题,但我解决了这个问题

fileUpload=@\"file, with comma .txt\"

So you have to put some double quotes around the file name if you have commas in the file name ( \" )

因此,如果文件名中包含逗号( \" ),则必须在文件名周围加上一些双引号

回答by Edmund Lee

For my case, using relative path didn't work. But changing it to a absolute path fixes it.

就我而言,使用相对路径不起作用。但是将其更改为绝对路径可以修复它。

This failed

这失败了

curl -i -X POST -H "Content-Type: multipart/form-data" \
    -F "file=@~/Downloads/xxx.csv" http://localhost:6708/upload

This worked

这有效

curl -i -X POST -H "Content-Type: multipart/form-data" \
    -F "file=@/Users/myself/Downloads/xxx.csv" http://localhost:6708/upload

回答by Kevin

I had a similar problem with relative paths like @Guilherme did too. I was running my bash script with:

我在相对路径方面也有类似的问题,比如@Guilherme。我正在运行我的 bash 脚本:

bash test/script.sh

However, my file was in the same directory level as the bash script and not the testdirectory (where I was calling bash from), so the script could not find my file.

但是,我的文件与 bash 脚本位于同一目录级别,而不是test目录(我从中调用 bash 的位置),因此脚本找不到我的文件。

Solution for me:
1. cd into /test and run bash script.sh from there
2. Use absolute paths

我的解决方案:
1. cd 进入 /test 并从那里运行 bash script.sh
2. 使用绝对路径

回答by Gapmeister66

Luis Cruz is correct: quotes are required around the filename, but I needed to use the 'file' keyword, not 'fileUpload', as is it shown in the question.

Luis Cruz 是正确的:文件名周围需要引号,但我需要使用“file”关键字,而不是“fileUpload”,如问题所示。

curl -X GET \
-H 'Content-Type: multipart/form-data' \
-F file=@"/path/to/file.ext" \ 
'http://host:port/path/to/dir'