简体   繁体   English

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

[英]Automatically post file from webserver

I'm trying to make a script that sends a file from my server to another server via POST method. 我正在尝试制作一个脚本,通过POST方法将文件从我的服务器发送到另一台服务器。 The HTML may look like this: HTML可能如下所示:

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

With this part, I can send a file from my computer to another server. 通过这部分,我可以将文件从我的计算机发送到另一台服务器。 But the script should send the file from my webserver, if a condition is true. 但是如果条件为真,脚本应该从我的网络服务器发送文件。 For example, I create a file on my website, I hit "OK" and the file is automatically sent to the external webserver. 例如,我在我的网站上创建了一个文件,我点击“确定”,文件自动发送到外部网络服务器。 Is it possible such a method? 这样的方法有可能吗? Any help is much appreciated. 任何帮助深表感谢。

This code will post a file to another server: 此代码将文件发布到另一台服务器:

<?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);
?>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM