不使用表单如何将文件上传至服务器?

4
我们想象那种形式;
<form action="http://api.blabla.com/huhu.php" method="post" enctype="multipart/form-data">
        <input type="file" name="files[]" />
        <button type="submit">submit</button>
    </form>

我希望能够在不使用上面的表单的情况下将文件上传到该服务器。

我尝试使用php curl,但是失败了。

我之所以想要这样做是因为我有非常多的文件需要上传。并且这应该可以通过cron作业自动完成。


你可以使用file_get_contents。参见:https://dev59.com/I2865IYBdhLWcg3wEaUw - Macbric
1个回答

5
这是一个使用cURL上传文件的示例,您可以开始尝试:
$ch = curl_init('http://api.blabla.com/huhu.php');
curl_setopt_array($ch, array(
    CURLOPT_POSTFIELDS => array(
        'files[]' => '@/path/to/file',
    ),
));
if (false === ($res = curl_exec($ch))) {
    die("Upload failed: " . curl_error($ch));
}

字符串'@/path/to/file'具有特殊含义,因为它以@开头;直接跟在它后面的字符串应该包含您想要上传的文件的路径。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接