PHP文件上传(通过命令行上的Curl)

标签 php curl

我在使用 PHP 将文件上传到我的服务器时遇到了噩梦,但是当使用这个简单的 HTML 表单时,它似乎可以工作:

<html><body>
<form enctype="multipart/form-data" action="uploads.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

然后 PHP 是:

<?php
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
?>

是否有任何可能的方法可以使用命令行或 shell 脚本中的 curl 通过此技术发送文件?即:

curl -f "@/path/to/my/file;type=text/html" http://mywebserver.com/uploads.php

那条特定的行给我:“curl: (22) Failed to connect to::1: Permission denied” 虽然网站上没有密码等。我认为这是可能的,但我的语法错误?

最佳答案

我认为您希望参数是大写的 F(对于 Form)。小写的 f 表示失败,并返回错误代码 22。说真的,它在手册中!

-f, --fail

(HTTP) Fail silently (no output at all) on server errors. This is mostly done to better enable scripts etc to better deal with failed attempts. In normal cases when an HTTP server fails to deliver a document, it returns an HTML document stating so (which often also describes why and more). This flag will prevent curl from outputting that and return error 22.

This method is not fail-safe and there are occasions where non-successful response codes will slip through, especially when authentication is involved (response codes 401 and 407).

-F, --form

(HTTP) This lets curl emulate a filled-in form in which a user has pressed the submit button. This causes curl to POST data using the Content-Type multipart/form-data according to RFC 2388. This enables uploading of binary files etc. To force the 'content' part to be a file, prefix the file name with an @ sign. To just get the content part from a file, prefix the file name with the symbol <. The difference between @ and < is then that @ makes a file get attached in the post as a file upload, while the < makes a text field and just get the contents for that text field from a file.

此外,您还没有命名您的文件变量。我想你想要:

curl -F "uploadedfile=@/path/to/my/file" http://mywebserver.com/uploads.php

关于PHP文件上传(通过命令行上的Curl),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16307819/

相关文章:

php - 确保我在 Laravel 5 中获得了正确的外键语法

php - 使用来自 3 个表的查询填充表单字段

curl - 如何使用curl获取与使用Chrome完全相同的GET请求?

c++ - 使用 curl 的 mobotix 相机

java - Glassfish REST API

php - 使用php获取具有相同id的多个数据

javascript - PHP 警报不显示

javascript - 如何摆脱这个 Facebook 像素警告?

curl ca-cert 在 Mac 上安装 Meteor 时出错

macos - 是否可以将 curl 设置为在进度条之前显示文件名?