【问题标题】:upload a file to server without using a form?不使用表单将文件上传到服务器?
【发布时间】:2011-08-27 13:34:43
【问题描述】:

我们想象那种形式;

<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 作业应该是自动的。

【问题讨论】:

标签: php html file-upload curl http-headers


【解决方案1】:

这是一个使用 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'具有特殊含义,因为它以@开头;紧随其后的字符串应包含您要上传的文件的路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2016-09-07
    • 2023-03-06
    相关资源
    最近更新 更多