从URL将图像保存到服务器 [英] Save image to server from a URL

查看:159
本文介绍了从URL将图像保存到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 CKEditor 构建了一个小型内容管理系统.用户可以从另一个网站粘贴图像 URL .当用户提交内容时,是否可以获取所有图像的URL,将所有这些图像保存到服务器,然后用我的服务器的URL替换另一台服务器的URL?

I've built a mini-content management system with CKEditor. The user has the ability to paste an image URL from another website. Is there a way to to get all image URLs when the user submits the content, save all these images to the server, and replace another server's URL with URL of my server?

例如,用户编写了这样的内容:

For example, the user wrote something like this:

<img src="somews.com/img1.jpg"/>Lorem Ipsum is simply dummy text of the printing and typesetting industry. ...

在提交过程中, PHP 会将图像从somews.com/img1.jpg保存到服务器,然后进行转换它的URL到myserver.com/photos/img1.jpg并将<img src="somews.com/img1.jpg"/>替换为..可能吗?

During the submit process PHP would save the image from somews.com/img1.jpg to the server, converts its URL to myserver.com/photos/img1.jpg and replaces <img src="somews.com/img1.jpg"/> with .. Is that possible?

推荐答案

如果您在服务器上启用了PHP5和HTTP流包装器,则将其复制到本地文件非常简单:

If you have PHP5 and the HTTP stream wrapper enabled on your server, it's incredibly simple to copy it to a local file:

copy('http://somedomain.com/file.jpeg','/tmp/file.jpeg');

copy('http://somedomain.com/file.jpeg', '/tmp/file.jpeg');

这将照顾所有需要的流水线等.如果您需要提供一些HTTP参数,则可以提供第三个流上下文"参数.

This will take care of any pipelining etc. that's needed. If you need to provide some HTTP parameters there is a third 'stream context' parameter you can provide.

这篇关于从URL将图像保存到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆