1

CodeIgniter upload library only supports uploading image file from PC.

I am trying to make a code that a user can upload image with image url address.

http://example.com/photo/jake2910.jpg

I simply send this image address to server and upload the photo.

Is there a way to do that? (trust me I tried different codes already for few days.)

james
  • 225
  • 6
  • 20
  • Best thing you can try here is php's file_get_contents() and file_put_contents(). Load your image into memory and write it on server. – zaw May 29 '12 at 08:01

2 Answers2

7

There is no need in CI's upload library at all. You can simply download the file with this code:

$url = 'http://example.com/photo/jake2910.jpg';
/* Extract the filename */
$filename = substr($url, strrpos($url, '/') + 1);
/* Save file wherever you want */
file_put_contents('upload/'.$filename, file_get_contents($url));

But be very cautious about what files you are trying to save. Remember that even images can be stuffed with vulnerable code.

CodingHamster
  • 352
  • 2
  • 12
  • It copies image address and file name but failed to copy image data. it shows me that copied image is 0 byte. – james May 29 '12 at 09:48
  • Tested the code once again. It works. I can suggest that there are some restrictions on the webserver preventing hotlinking or something. Can you please provide the url you're trying to save? – CodingHamster May 29 '12 at 11:05
3

Try this

copy('http://example.com/photo/jake2910.jpg', '/mylocation/jake2910.jpg');

for more details you can check this similar post

Copy Image from Remote Server Over HTTP

Community
  • 1
  • 1
pinaldesai
  • 1,835
  • 2
  • 13
  • 22