开发者

Upload images to remote servers. PHP

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 uploa开发者_如何学运维d: <input type="file" name="file"/>
    <input type="submit" name="upload" value="upload"/>
</form>


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

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


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.


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜