【问题标题】:Upload images to remote servers. PHP将图像上传到远程服务器。 PHP
【发布时间】:2011-01-23 17:55:34
【问题描述】:

我做图片托管,但我有问题..

我有 3 台服务器。

首先 - 站点/脚本

图像的任意两个服务器。

如何将图像从“一个”服务器(脚本)上传到第二个和第三个服务器?

<?php
    if (isset($_POST['upload']))
    {
        $blacklist = array('.php', '.phtml', '.php3', '.php4', '.php5');
        foreach ($blacklist as $item)
        {
            if(preg_match('#' . $item . '\$#i', $_FILES['file']['name']))
            {
                echo "We do not allow uploading PHP files\n";
                exit;
            }
        }

        $uploadDir  = PROJECT_ROOT . 'upload/'; // 1ST SERVER (THIS SERVER)
        $uploadFile = $uploadDir . basename($_FILES['file']['name']);

        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile))
        {
            echo "File is valid, and was successfully uploaded.\n";
        }
        else
        {
            echo "File uploading failed.\n";
        }
    }    
?>
<form name="upload" method="post" enctype="multipart/form-data">
    Select the file to upload: <input type="file" name="file"/>
    <input type="submit" name="upload" value="upload"/>
</form>

【问题讨论】:

  • 你想在这里实现什么?您是尝试在三台服务器之间同步图像,还是?
  • @middaparka,我做图像托管。当您加载图像时,应将其倒在 3 台服务器中的任何一台上。 3台服务器需要卸载通道。以后会有更多的服务器。
  • 我不确定您所说的“卸载频道”是什么意思。也就是说,如果您尝试在所有三台服务器上复制图像,我会很想使用rsync 之类的东西。

标签: php image upload


【解决方案1】:

您可以使用 ftp。 PHP 有相当简单的方法来做到这一点。检查此链接。

http://www.php.net/manual/en/book.ftp.php

【讨论】:

    【解决方案2】:

    如果您已经在其他服务器上运行了 HTTP 服务器,请使用 cURL。用法:

    1. 致电curl_init
    2. 致电curl_setopt
    3. 致电curl_exec

    可以使用curl_setopt 配置HTTP 请求。尤其令人感兴趣的是选项 CURLOPT_URL、CURLOPT_POST 和 CURLOPT_POSTFIELDS。

    【讨论】:

    • 也就是说,你必须在另外两台服务器上提供托管脚本,并且第一台使用Curl来发送图片?
    • @Isis 我以为你已经有了。如果不这样做,请使用 dqhendricks 建议的 FTP 或其他服务器已经提供的其他协议。
    【解决方案3】:

    您可以使用 Zend_Http 客户端通过 HTTP 将文件上传到其他服务器,就像 HTML 上传表单一样。您可以在“文件上传”部分找到您需要的所有信息:http://www.zendframework.com/manual/en/zend.http.client.advanced.html

    在开始之前,您还应该阅读以下内容:

    http://www.zendframework.com/manual/en/zend.http.client.html

    基本上你需要的代码是:

    require_once('Zend/Http/Client.php');
    $client = new Zend_Http_Client("http://serverurl/path");
    $client->setFileUpload(...);
    $client->request();
    

    【讨论】:

      猜你喜欢
      • 2014-12-01
      • 2016-03-29
      • 1970-01-01
      • 2016-03-16
      • 1970-01-01
      • 2013-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多