upload a file to server without using a form?

2019-01-15 16:47发布

We imagine that form;

<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>

I want to upload files to this server without using the form which is above.

I tried this with php curl but I could not.

I want it because I have very large number of files to upload. And this should be automatic with cron jobs.

1条回答
看我几分像从前
2楼-- · 2019-01-15 17:18

This is an example of file uploading with cURL you could start with:

$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));
}

The string '@/path/to/file' has a special meaning because it starts with an @; the string that directly follows it should contain the path of a file you wish to upload.

查看更多
登录 后发表回答