自动从网络服务器发布文件

时间:2012-07-04 08:11:50

标签: php file post

我正在尝试创建一个脚本,通过POST方法将文件从我的服务器发送到另一台服务器。 HTML可能如下所示:

<form action="https://anotherserver/receive.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" />
</form>

通过这部分,我可以将文件从我的计算机发送到另一台服务器。但是如果条件为真,脚本应该从我的网络服务器发送文件。例如,我在我的网站上创建了一个文件,我点击“确定”,文件自动发送到外部网络服务器。 这样的方法有可能吗?非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

此代码会将文件发布到另一台服务器:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, 'https://anotherserver/receive.php');
    curl_setopt($ch, CURLOPT_POST, true);
    $post = array(
        "file"=>"/path/to/myfile.jpg",
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
?>

改编自:http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html

相关问题