php 网站的独立文件服务器

发布于 2024-11-30 13:29:44 字数 224 浏览 2 评论 0原文

我有两个网站服务器。一台服务器将有 php 代码和数据库,另一台速度较低的服务器仅用于存储文件。我需要以这样的方式实现这些:通过网站上传的文件必须存储在另一台服务器上,然后也可以从那里下载。

谁能建议我实现这一目标的最佳方法。我知道文件可以在通过网站上传后通过 php 的 FTP 功能传输到另一台服务器,但这似乎不是正确的方法。

或者两个服务器可仅用于静态媒体内容(例如图像)。

谢谢

I have two servers for a website. One server will have php code and database and another less speed server to store files only. I need to implement these in a way that file uploaded through the website must store at another server and also then can be downloaded from there.

Can anyone suggest me best way to achieve that. I know the files can be transferred to another server by FTP functions of php just after uploading through website but doesn't seems a correct way.

Or two server can be used for static media content like images only.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

痴意少年 2024-12-07 13:29:44

最好的想法是将所有文件,包括“存储服务器”上的网站文件。基本上,您要做的就是安装“共享文件夹”,这意味着您需要的网站文件和其他文件。 (大多数时候,存储服务器上只有一个 /var/www-local/ 文件夹,您可以将其挂载到网络服务器上的 /var/www/ 中)。

确保使用 NFS 挂载它,方法是将其添加到 Web 服务器上的 /etc/fstab 文件中。
有关 NFS 的更多信息

这种想法的优点是,当您想要要进行扩展,可以通过放置软件负载平衡器(例如 HAProxy)、添加任意数量的网络服务器来轻松实现这一点,并且您将同步数据。

The best idea is to just have ALL the files, including the websites files on the "storage server". Basically what you do is mount the "shared folder", this means the websites files and other files you will be needing. (Most of the times you just have a /var/www-local/ folder on the storage server, which you mount in /var/www/ on the webserver).

Make sure you mount it using NFS by adding it in the /etc/fstab file on the webserver.
(More info on NFS)

The advantage of this idea is that when you want to expand, this is easily possible by putting a software loadbalancer (like HAProxy), adding as much webservers as you like and you will have your data in sync.

夜夜流光相皎洁 2024-12-07 13:29:44

我使用了名为 Gluster 的东西,它允许这样的事情。我使用它的情况更多的是用于负载平衡而不是替代内容分发。

此外,像 Stack Overflow 这样的网站还使用内容分发网络服务来获取其网站上的某些信息。像这样的解决方案实际上可能比购买/设置/维护全新服务器更具成本效益。

I've used something called Gluster which allows for things like this. The situation I use it for is more for load balancing than for alternate content distribution.

Also, sites like Stack Overflow use Content Distribution Network services for certain pieces of information on their site. A solution like this might actually be more cost effective than buying/setting up/maintaining a whole new server.

鹿童谣 2024-12-07 13:29:44

也许您可以将上传目录挂载到您的网络服务器。
检查 Linux NFS

May be you can mount the upload directory to your web server.
Check Linux NFS

剩一世无双 2024-12-07 13:29:44

不知道这是否是最好的方法,但为什么不简单地让文件服务器有一个单独的子域呢?这样它就可以处理所有文件下载细节,并且您可以从主服务器通过 FTP 或 SFTP 连接到它。

基本上,您可以使用以下过程:

  1. 将您的子域指向辅助服务器。我在此处找到了一些信息。
  2. 在您的主服务器上有一个上传表单,用于处理和验证文件。
  3. 当文件被认为可以接受时,通过 FTP 或 SFTP 将其发送到其他服务器。如果您无法使用 PHP 工具来实现此目的,phpseclib 可能会有所帮助。您可能希望使此步骤成为多线程。

Don't know if this is the best way, but why not simply have the file server a separate subdomain? That way it can handle all of the file download minutia and you can connect to it via FTP or SFTP from the main server.

Basically, here is a process you could use:

  1. Point your subdomain at the secondary server. I found some information on that here.
  2. Have an upload form on your main server which processes and validates the file.
  3. When the file is deemed acceptable, send it to the other server via FTP or SFTP. If you can't get the PHP tools working for this, phpseclib might help. You may want to make this step multi-threaded.
烟花肆意 2024-12-07 13:29:44

通过使用 url 包装,您可以使用默认的 move_uploaded_file(),如果您的 ftp 服务器接受此连接类型。或者,您可以使用 PHP 的 ftp 函数,尤其是 ftp_put(),将文件上传到服务器。

对于内容交付,您需要有一个数据库或其他方式来获取内容分发服务器上的原始 url,并将该 url 放入 html 参数中:

<img src="http://cdn1.example.com/images/68263483.png" />
<a href="http://cdn2.example.com/files/9872345.pdf">Download PDF</a>

处理上传文件的示例代码将是 或

<?php
// ...
$uploads_dir = 'images';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, 
            "ftp://user:[email protected]/$uploads_dir/$name");

        // save url in your database for later retrieval ...
    }
}

ftp_put():

<?php
// ...
$ftpCon = ftp_connect('cdn1.example.com') 
    or die('Could not connect to ftp server');
$uploads_dir = 'images';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        ftp_put($ftpCon, "$uploads_dir/$name", $tmp_name)
            or die("could not upload $name to ftp server");
        // save url in your database for later retrieval ...
    }
}

By using an url wrapper, you can use your default move_uploaded_file(), if your ftp server accepts this connection type. Alternatively, you can use PHP's ftp functions, especially ftp_put(), to upload the file to the server.

For content delivery, you need to have a database or other means to get the original url on the content distribution server and put the url in the html arguments:

<img src="http://cdn1.example.com/images/68263483.png" />
<a href="http://cdn2.example.com/files/9872345.pdf">Download PDF</a>

An example code to handle an uploaded files would be

<?php
// ...
$uploads_dir = 'images';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        move_uploaded_file($tmp_name, 
            "ftp://user:[email protected]/$uploads_dir/$name");

        // save url in your database for later retrieval ...
    }
}

or with ftp_put():

<?php
// ...
$ftpCon = ftp_connect('cdn1.example.com') 
    or die('Could not connect to ftp server');
$uploads_dir = 'images';
foreach ($_FILES["pictures"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
        ftp_put($ftpCon, "$uploads_dir/$name", $tmp_name)
            or die("could not upload $name to ftp server");
        // save url in your database for later retrieval ...
    }
}
苹果你个爱泡泡 2024-12-07 13:29:44

如果您的应用程序需要对文件服务器进行密集读/写,那么我认为将其分开是一个坏主意。也许您可以将 NFS 镜像到您的主 Web 服务器以减少延迟!

使用 nginx 处理所有对静态文件的请求(例如:css、图像、javascript)

if your application requires intensive read/write to the file server, then I would think it's a bad idea to separate it. maybe you can mirror the NFS to your main web server to reduce latency!

Use nginx to handle all request to static files (eg: css, images, javascript)

迎风吟唱 2024-12-07 13:29:44
  1. 在文件服务器上创建一个共享到您要下载和上传文件的目录。如果您打算将文件保存在单独的文件夹中,则需要两个共享。

  2. 使用 PHP 代码库映射 Web 服务器上的文件夹。确保您使用登录名连接到具有读写访问权限的共享。

  3. 映射共享后,在网站上创建一个到共享文件夹的虚拟目录(这一切都取决于您的网络服务器)。

  4. 检查 PHP 如何处理访问网站中共享驱动器上的文件,根据您的网络服务器,它可能与您通常处理文件的方式不同。

我喜欢这种方法,因为服务器操作系统负责将文件传输到文件服务器或从文件服务器传输文件。 PHP 只是告诉它它需要哪些文件。

  1. Create a share on the file server to tbe directory where you want to download and upload files. Two shares if you plan to keep tyem in separate folders.

  2. Map the folders on the web server with the PHP code Base. Make sure you use a login to conect to the share(s) that will have read and write access.

  3. Once you have mapped the share(s), create a virtual directory on the website to the shared folder (this all depends on your web server).

  4. Check how PHP handles accessing files on a shared drive in a website, depending on your webserver it may be different than how you normally handle files.

I like this approach because the server OS handles transfering the files to and from the file server. PHP just handles telling it which files it needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文