【问题标题】:PHP Curl upload file with file browse button带有文件浏览按钮的 PHP Curl 上传文件
【发布时间】:2012-10-03 00:55:04
【问题描述】:

我想将我的文件远程上传到接受上传的服务器 使用 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); ]]

防止将文件上传到临时路径中的中间服务器(托管此脚本),只是从客户端直接到远程服务器

有什么解决办法吗

感谢大家的帮助。

【问题讨论】:

    标签: php file-upload curl


    【解决方案1】:

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

    【讨论】:

    • 感谢 aravind.udayashankara 但我不想使用 $post_params['file'] = '@'.'demo/testfile.txt';我想以这种方式使用它 $post_params['file'] = $variable_holding_filePath_from_the_browse button;
    • @Mas 让我试一试,但是你的问题到底是什么@我会尽快更新这个答案
    • 只是为了从文件类型输入中获取完整的文件路径,然后将文件上传到远程文件主机,而无需先将其上传到我的服务器
    猜你喜欢
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2014-04-08
    • 2014-06-01
    相关资源
    最近更新 更多