PHP Curl上传文件,带文件浏览按钮

时间:2012-10-03 00:55:04

标签: php file-upload curl

我想将我的文件远程上传到接受上传的服务器 使用curl,但没有每次使用“@”符号键入完整的文件路径 我可以制作一个浏览按钮来选择文件然后继续卷曲上传

这是我的代码::

$post = array("file_box"=>"@/path/to/myfile.jpg");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://remote-site");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$output = curl_exec($ch);
curl_close($ch);

return $output;

只想通过浏览按钮更改“@ / path / to / myfile.jpg”,将其值传递给php变量

我想更改此[[$ post = array(“file_box”=>“@ / path / to / myfile.jpg”); ]

到类似的东西

[[$ post = array(“file_box”=>“@”。$ variable_contains_file_path_from_browse_button); ]

防止文件直接从客户端直接上传到中间服务器(托管此脚本)到客户端到远程服务器

是否有任何解决方案

感谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

SO中有几个问题可以解决这个问题

这里有一些链接

uploading files using curl

Curl php file upload

谷歌搜索策略也是http://bit.ly/PMUf2b

但是,由于您要求从前端上传浏览按钮,这里是完整的设置以及导师链接

你可以关注这里有前端和php代码的导师

upload using curl 这是php代码

    $request_url = ‘http://www.example.com/test.php’;
    $post_params['name'] = urlencode(’Test User’);
    $post_params['file'] = ‘@’.'demo/testfile.txt’;
    $post_params['submit'] = urlencode(’submit’);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $request_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
    $result = curl_exec($ch);
    curl_close($ch);

这是html代码

<form method=”post” action=”test.php” enctype=”multipart/form-data”>
    <input type=”text” name=”name” value=”Test User” />
    <input type=”file” name=”file” />
    <input type=”submit” name=”submit” value=”submit” />
</form>