Uploading image with url address in Codeigniter

2019-08-08 18:32发布

问题:

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

回答1:

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.



回答2:

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