1

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. ...

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
heron
  • 3,611
  • 25
  • 80
  • 148
  • Parse the submitted HTML, extract image URLs, download the images, store them, update the image URLs in the HTML. Done. – hakre Oct 10 '11 at 09:48
  • possible duplicate of [Best methods to parse HTML with PHP](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html-with-php), [save image from php url using php](http://stackoverflow.com/questions/724391/save-image-from-php-url-using-php) – hakre Oct 10 '11 at 09:49
  • it's simple for you. but how to do that? – heron Oct 10 '11 at 09:49
  • I added you some duplicate questions, you should use the search before posting a new, combined question. – hakre Oct 10 '11 at 09:50

1 Answers1

8

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');

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.

Praveen kalal
  • 2,148
  • 4
  • 19
  • 33