什么是此链接中的'目标脚本'使用curl上传文件?

时间:2014-03-05 05:39:04

标签: php http curl

This is the past question我在谷歌时发现了。第一个答案很简单,我不明白。

整个脚本在这里,

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/your-destination-script.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setpopt($ch, CURLOPT_POSTFIELDS, array(
        'file' => '@/..../file.jpg',    // you'll have to change the name, here, I suppose
        // some other fields ?
));
$result = curl_exec($ch);
curl_close($ch);

这是我不明白的,

curl_setopt($ch, CURLOPT_URL, "http://www.example.com/your-destination-script.php");

这是定义目标网址的地方。但是我应该在那里写什么来从这里上传文件,

curl_setpopt($ch, CURLOPT_POSTFIELDS, array(
        'file' => '@/..../file.jpg',    // you'll have to change the name, here, I suppose
        // some other fields ?
));

请有人解释我或请尝试。我是PHP的新手。

1 个答案:

答案 0 :(得分:1)

答案很简单。目标脚本(比如说,这里是一个PHP文件)是使用$_POST数组使用file_get_contents()函数获取文件内容的脚本(或者如果是JSON对象,它使用{{1}对其进行解码功能)

因此,基本上,这是将图像发送到上传的脚本。可以是任何网站上的任何脚本(如果网站允许,可以像这样上传)。有时站点需要登录ID或令牌ID,这也是使用json_decode()传递的,如果登录ID或令牌ID是真实的,则在PHP脚本中检查。只是一项安全措施:)

明白了? :)

通常,目标脚本是用于连接到站点的API等的脚本:)