Uploading image with url address in Codeigniter - php

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

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.

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

Related

How to upload file without form some url to my directory in codeigniter?

I set login with google in my website and google get me some url like so i not have form then how i upload this url image to my directory?
i have no resources from any site for this issue.
Without using CI libaray.Lets try...
$url = 'https://lh3.googleusercontent.com/-MzgndtILI5I/AAAAAAAAAAI/AAAAAAAAAAs/qB7MkGWroIY/photo.jpg';
/* Extract the filename */
$filename = substr($url, strrpos($url, '/') + 1);
/* Save file wherever you want */
file_put_contents('upload/'.$filename, file_get_contents($url));
Also take reference from here...
Saving image from PHP URL and Uploading image with url address in Codeigniter

How to recieve & store an image file from another server, being passed programatically with paramaters?

I am using the API of an image editing website (pixlr.com) for use by members of my site. I open Pixlr.com in an iframe where they can create an image and upon SAVE, pixlr sends the image file by use of parameters.
I want to save these image files (unique for each member) in a folder on my server (or on Amazon's S3 image server), using PHP. How do I receive their parameters ("image") of the image file and store them on my/Amazon's image server?
If the image is sent to your PHP script via POST, then you should be able to do something like this:
$handle = fopen($imageName, "wb");
fwrite($handle, $_POST["image"]);
fclose($handle);
Where $imageName is the absolute path and filename of the image where you want to save it (make sure you Apache user has write permissions to that directory). Depending on the picture's encoding you may need to figure out which extension to save it with (ie .jpg, .bmp, .png, etc).
EDIT:
Looks like they are sending the image via $_FILES. Try this:
move_uploaded_file($_FILES["image"]["tmp_name"], "/home/path/domain.com/upload/". time() .".png");

upload external image and save it on my server

I want be able to upload an image or just paste the URL of an image in order to upload it a sa profile picture for my website users.
the point is i dont wanna store the url but i want to have a copy of that image on my server because if that external image will be lost i dont want to lose it either...
i believe facebook and tumblr etc do so... what the php script or best practice to do that?
thanks!
You can get the contents (bytes) from the image using the PHP function (http://php.net/manual/en/function.file-get-contents.php)
$contents = file_get_contents('http://www.google.com/images/logos/ps_logo2.png');
You can use CURL library as well... Here's an example of how you can downloadImageFromUrl with and save it in a local SaveLocation
function downloadImageFromUrl($imageLinkURL, $saveLocationPath) {
$channel = curl_init();
$curl_setopt($channel, CURLOPT_URL, $imageLinkURL);
$curl_setopt($channel, CURLOPT_POST, 0);
$curl_setopt($channel, CURLOPT_RETURNTRANSFER, 1);
$fileBytes = curl_exec($channel);
curl_close($channel);
$fileWritter = fopen($saveLocationPath, 'w');
fwrite($fileWritter, $fileBytes);
fclose($fileWritter);
}
You can use this as follows:
downloadImageFromUrl("http://www.google.com/images/logos/ps_logo2.png", "/tmp/ps_logo2.png")
You can also get the same name of the image by parsing the URL as well...
I think this is you are looking for: Copy Image from Remote Server Over HTTP
You can use a function called imagecreatefromjpeg or something. It takes a URL path to the image and creates a new image off of that. Have a look at http://php.net/manual/en/function.imagecreatefromjpeg.php
There's different functions for different extensions, though (if you prefer using such). You may need to check for the image extension from the URL and use the appropriate function I suppose.
Handling uploads is covered in this documentation, and if users paste a URL, I'd recommend using file_get_contents to save a copy of the image to your server, and then you can simply store the path to that image, rather than the external image.

how to run sql query after image upload in php when i am using flash uploader for multiple file uploading

I am using library to upload multiple images in php .This library use flash file and a php file . All things are working but problem is that I want to run a query after each image is uploaded to store image detail in database . If i write query in php file after uploading code then It upload images but no error given and no image details are put in database.
Can any one help me that how I add image details in database after uploading image using flash uploader
This is my code
<?
extract($_GET);
$filename = $_FILES['Filedata']['name'];
$temp_name = $_FILES['Filedata']['tmp_name'];
$error = $_FILES['Filedata']['error'];
$size = $_FILES['Filedata']['size'];
/* NOTE: Some server setups might need you to use an absolute path to your "dropbox" folder
(as opposed to the relative one I've used below). Check your server configuration to get
the absolute path to your web directory*/
if(!$error){
copy($temp_name, '../dropbox/'.$filename);
$news_query="insert into tbl_news(img_id,headline,caption,news_catgory_id,shooting_date) Values('0000','News_Headline','News_Caption',
'New_Category','now()')";
mysql_query($news_query) or die(mysql_error());
}
?>
all code working but query data is not updated in database
May be it's a sql error...if your "shooting_date" field is numeric or date, you can't set it's value like this: 'now()', you must strip the quotes.

How to save a snapshot of a SWF to the server with AS3?

I am facing the task of having to upload a snapshot to the server. But I don't want the user to download the image to their computer.
I have explored a few solutions of generating an image serverside with PHP, but they all seem to use a method where the server sends the image to the user.
See for instance: http://mattkenefick.com/blog/2008/11/06/saving-jpegs-with-flash/
I'm wondering if it's possible to save $GLOBALS["HTTP_RAW_POST_DATA"], which in that example contains the ByteArray sent by Flash, to the server as an image file....
Use php code that is along these lines to save the contents of $GLOBALS["HTTP_RAW_POST_DATA"]
// untested code
$imageBytes = $GLOBALS["HTTP_RAW_POST_DATA"]
// in real code you better create a new file for every upload :-)
$file = fopen("uploads/test.jpg", "w");
if(!fwrite($file, $imageBytes)){
return "Error writing to file: $file";
}
fclose($file);

Categories