Table of Contents
回复内容:
Home Backend Development PHP Tutorial 静态文件服务器A和web应用服务器B分开,怎么样在B服务器上传的图片,上传到静态文件服务器应用是PHP写的?

静态文件服务器A和web应用服务器B分开,怎么样在B服务器上传的图片,上传到静态文件服务器应用是PHP写的?

Jun 06, 2016 pm 04:44 PM
ftp php

用户上传图片到B服务器,B服务器PHP收到之后,怎样复制到A服务器,大网站是怎么做的,用ftp?

回复内容:

FTP这种太落后了,不要用。文件传输的开源程序很多,可以去找找,rsync也可以。
如果是我的话,我随手就写了一个upload_server和upload_client,基于swoole扩展。
github.com/swoole/uploa 有以下种方式。
1. 如果A和B都为同一个局域网内的服务器,可以使用局域网网络磁盘映射,将A的某个磁盘目录映射到B的某个文件夹,那么,在B上传文件时,只需要复制一份到这个映射目录就可以了,系统会直接传送的A服务器的目录里,这个速度因为是局域网内所以是可以忽略速度影响的。

2.利用服务器rsync的同步工具。在B架设rsync的服务端,设定需要同步的文件夹,在A设定rsync的客户端,设定同步的来源服务器和对应的文件夹。当B的上传文件夹变动时,rsync会自动同步一份到A的客户目录。

3.当不在同一个局域网内的时候,可以在A部署一个底层脚本,用来接收post文件,可以自己设定一个密钥。当B有文件上传时,B服务器用php的curl直接重新post一份到A的接口,然后在A的接口设定逻辑存放到相应的目录。这个原理类似于各大云存储的S3逻辑。

当然,如果文件大小不是太小,B服务器还可以利用各种中转程序,例如先存到mysql,或者nosql的存储里,然后在A服务器上自动去抓取下来。 如果题主的网站不需要考虑低版本浏览器兼容性,可以采用html5的xhr2对象直接跨域上传静态文件服务器,很方便的(不逃 实时性要求不高的话一般都是跑脚本,ftp的形式,定时传。
实时性要求高的话还得看具体需求,有不同的形式和方案。 居然有人想到ftp,啧啧,再不济也是rsync啊
给你一个思路,这种模式太常见了,解决方案也很简单
1. 我们都知道上传这个操作如果在php层做的话,必定经过一个 upload_tmp_dir 配置,如你所说,上传在B上,是用户上传,符合第一条(php层)
2. 既然有tmp或者nfs,那么你其实在post表单后就拿到了一个类似 /tmp/aedg343sdsf 类似的文件,好拿到文件的思路有了
3.再说说平时我们的做法,move_uploaded_file 到B的一个配置的(nginx,apache,php都可以配置)静态文件专门存放的目录
4.这里就有思路了,既然B拿到了文件,那么就是模拟move_uploaded_file 为post到A服务器的一个接口,无非就是一个中转
5.4中说到了A接口,我们的业务有这样的需求,你可以百度下s3(我们内部类似),看看文档,他们的接口定义,无非标准的post加上一些source或者appkey等安全问题,但是你不涉及到裸露接口调用,所以可以简单来实现接口 局域网或广域网都可以这样做,原因和 @周大哥 的相同
系统为centos 7
yum -y install sshfs
echo "postgres" | sshfs postgres@IP:/opt/data /opt/data -o allow_other -ocache=no -o password_stdin
简单来就是ssh的mount
后面用sersync2定向同步 如果数据量大、用户多的话,是不是只能使用ftp上传呢 vfs
以二进制的形式进行传输,读文件也以二进制进行解码 其实你可以使用静态资源云存储,比如七牛,阿里云,使用它们的接口就能把资源上传到它们的服务器,接口十分简单,费用非常便宜,比自建资源服务器便宜很多。
基本就是,
1、图片上传到Web目录
2、使用它们API或者使用FTP上传到云存储的服务器或自己的服务器
3、在Web服务器中删除用户所上传的文件
比较麻烦,但管用。步骤可以自己再优化下。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

See all articles