Upload images to remote servers. PHP

若如初见. 提交于 2019-12-07 17:02:29

问题


I do the image hosting and I have a problem..

I have 3 servers.

First - Site/script

Any two servers for images.

How I Can upload image from "one" server (script) to second and third servers?

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

回答1:


You could use ftp. PHP has fairly easy way to do this. Check this link.

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




回答2:


If you already have HTTP servers runing on the other servers, use cURL. Usage:

  1. Call curl_init
  2. Call curl_setopt
  3. Call curl_exec

The HTTP request can be configured using curl_setopt. Especially of interest are the options CURLOPT_URL, CURLOPT_POST and CURLOPT_POSTFIELDS.




回答3:


You can use Zend_Http Client to upload the files to other servers per HTTP the same way an HTML Upload Form would do it. You can find all the information you need here in the Section "File Uploads": http://www.zendframework.com/manual/en/zend.http.client.advanced.html

For getting started you should read also this:

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

Basically the code you need is:

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


来源:https://stackoverflow.com/questions/4775376/upload-images-to-remote-servers-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!