开发者

Image hosting with multiple servers

开发者 https://www.devze.com 2023-03-26 10:18 出处:网络
I\'m developing an image host and wish to have images uploaded to a separate server from my web content, eg: http://i1.mysite.com instead of http://mysite.com/uploads. But I\'m having some trouble fig

I'm developing an image host and wish to have images uploaded to a separate server from my web content, eg: http://i1.mysite.com instead of http://mysite.com/uploads. But I'm having some trouble figuring out how to do that.

Say I have this form:

<form action="http://mysite.com/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
    <input type="file" name="image" id="file_upload" />
    <input type="submit" value="Upload" id="upload_submit" />
</form>

That will send an image file to /upload, where I can validate the file and save it, but that will be on the same server as the website is hosted, rather than a dedicated storage server. How can I achieve what I want without having the images uploaded on the same server as my web site?

I could always do:

<form action="http://i1.mys开发者_如何学Goite.com/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
    <input type="file" name="image" id="file_upload" />
    <input type="submit" value="Upload" id="upload_submit" />
</form>

which would send the image file to another server, but then when the image upload is complete I'd be redirected to http:/i1.mysite.com/upload.

Anyone have any experience with this and can recommend a course of action? Thank you!


Don't upload to the image server. Such content-specific servers should be optimized for serving up content, and not have to deal with consuming content.

Let the upload form send to your main site's server. You can then use other protocols to transfer the uploaded file(s) to the image servers. rsynch, scp, etc... This way you have all your "control" code in one location, and don't have to worry about synching databases and whatnot between multiple servers - all the data is kept on your main server, and the image servers just passively spit out image data.


I would recommend decoupling these two ideas. First, upload the image to your servers and in a separate process (perhaps a scheduled cron) move the images to other server. You likely do not want the user waiting for two uploads to finish.


Like the others have said, what you're trying to do is not optimal. If you really want to continue to do this, I'd suggest having the form submit to a PHP script which then processes it and places the file where it needs to be and then saves whatever information to the database that is necessary. You'll need to evaluate the best protocol for the data transfer from one server to the other. You'll probably end up using Curl, which you can learn about here and here as well as the curl docs


You could upload the image to your image host, and have it redirect back to your website afterward. One way to do this would be to add hidden "success" and "failure" URL inputs to the form:

<form action="http://i1.mysite.com/upload" method="post" accept-charset="utf-8" enctype="multipart/form-data">
    <input type="file" name="image" id="file_upload" />
    <input type="submit" value="Upload" id="upload_submit" />
    <input type="hidden" name="success" value="http://mysite.com/success" />
    <input type="hidden" name="failure" value="http://mysite.com/failure" />
</form>

The upload script on your image host would then redirect to the supplied URL after a successful upload:

<?php
    .. handle uploaded file ..

    if ($success) {
        header ('Location: ' . $_REQUEST ['success']) ;
    }
    else {
        header ('Location: ' . $_REQUEST ['error'] . '?message=' . $message) ;
    }

?>
0

精彩评论

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