How to save images from list image url in csv - php

I want to save images from url to my directory and its image name after downloaded into a csv.
I write the following code which store images. but its not working as I want.
in below code $img is variable which fetch url from csv.
suppose I have 1000+ products in csv and every products have image url like
www.example.com/images/image1.jpg
so I want to download this image to my local directory/server directory and store its image name after download to csv file so I can add only image name into database tables.
set_time_limit(1000);
while($url = $response->fetch()) {
$my_image = file_get_contents($img);
$my_file = fopen('/csvfiles/','w+');
fwrite($my_file,$my_image);
fclose($my_file);
}
The some solution I found on other similar questions is
$file = file_get_contents($img);
file_put_contents("/csvfiles/Tmpfile.zip", fopen($img, 'r'));
But it is not applicable in my condition or not working.
Any help would be appreciated.
Edit New -
As marked as duplicate I want to add some more info here -
As I have all products info in csv formats and while importing the products I also want to download images from its url. And it is not possible to download 1000+ product image manually. I checked the above solution but it is not applicable in this situation. As in above solution they download image from only particular page and rename it directly. so it is different scenario here.
New Code - I also tried this code still not working
$content = file_get_contents("http://www.google.co.in/intl/en_com/images/srpr/logo1w.png");
$fp = fopen("/csvfiles/image.jpg", "w");
fwrite($fp, $content);
fclose($fp);

Try with the following code.
set_time_limit(1000);
while ($url = $response->fetch()) {
$my_image = file_get_contents($img);
// check stricly that you have content of image
if ($my_image !== false) {
// download the image and store in the database.
$my_file = fopen('/csvfiles/', 'w+');
fwrite($my_file, $my_image);
fclose($my_file);
}
}

You need a filename as well, not only a directory:
// download the inage from the internet:
$my_image = file_get_contents($url);
// get filename from url:
$filename = basename($url);
$path = '/csvfiles/' . $filename;
// save image to that file:
$my_file = fopen($path,'w+');
fwrite($my_file, $my_image);
fclose($my_file);
// save $filename to DB

Hi below code works for me .. hope it will help someone..
Thanks #Gavriel and #Alankar
if(!empty($img)){
$my_image = file_get_contents($img);
file_get_contents(filename)
$filename = $data[0] . ".jpg";
echo $filename. "\n";
if(!file_exists($filename))
{
$my_file = fopen($filename,'w+');
file_put_contents($filename, $my_image);
echo $filename . "\n";
}
}
$image = "/uploads/" . $filename;
echo $image . "\n";
Here $img store url of image. which is fetch from csv file.
Here data[0] is csv column which is used to store image name uniquely . so we can use file_exists(); otherwise whenever I run my script again and again it store images again and store it by random temp name.

Related

how to download users twitter images from API with php

i am trying to download twitter users images that sign up in my website such that i can save them in my file to insert into my database later on for use in my website.
this is my TWITTER API:
$user_pic = $user_info->profile_image_url_https;
i tried this and it works but i want it to download the image to my base file which is profilePictures/:
$twitterimage = file_get_contents($user_pic);
$fp = fopen('path_to_filename.png', 'w');
fwrite($fp, $twitterimage);
fclose($fp);
now it is being downloading but how can i download the twitter images and save them in my base file "profilePictures/"?
i found the solution, all i had to do is this
$user_pic= str_replace('_normal','',$user_pic);
$Imgurl = $user_pic;
$target_path = "profilePictures/";
$filename = basename($Imgurl);
$saveLoc = $target_path . $filename;
file_put_contents($saveLoc, file_get_contents($Imgurl));
and then save the $saveLoc variable into my database

Image is not showing while copying from one folder to another

I am trying to copy an image from one folder to another using PHP.
Image is moving fine, but when I am opening the image, the image is not showing.
Any ideas?
Thank you.
Below is my code.
if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'image_copy') {
$image = $_POST['image'];
//name of file to be copied
$img = $image;
//read the file
$fp = fopen('files/post/' . $img, 'r') or die("Could not contact $img3");
$page_contents = "";
$new_text = fread($fp, 100);
$page_contents = $new_text;
//This moves you from the current directory user to the images directory in the new user's directory
chdir("files/profile");
//name of your new file
$newfile = $image;
//create new file and write what you read from old file into it
$fd = fopen($newfile, 'w');
fwrite($fd, $page_contents);
//close the file
fclose($fd);
exit;
}
I have made it some other way using file get content and put content .
$image = $_POST['image'];
$dir_to_save = "files/profile/";
if (!is_dir($dir_to_save)) {
mkdir($dir_to_save);
}
file_put_contents($dir_to_save . $image, file_get_contents('files/post/' . $image));
But i am unable to do in move method of php.

Cannot convert blob to image

I have a database where images are saved as blob. Now, I want to convert this blob data into image files.
So I have made this code, but its not giving any result
$sql_icon = "SELECT $off_table.icon,$off_table.id
FROM $off_table,$cnt_table,$ctg_table
WHERE $off_table.id = $cnt_table.id
AND $off_table.id = $ctg_table.id
AND ".$cond_api."
AND ".$cond_nt."
AND ".$cond_cnt."
AND ".$cond_ctg;
$result_icon = mysql_query($sql_icon);
while($row = mysql_fetch_array($result_icon))
{
$image = $row["icon"];
$id = $row["id"];
$file = fopen("./image/".$id.".jpg","w");
fwrite($file, base64_decode($image));
fclose($file);
}
The issue is, I am getting files but no extensions and if I rename them to ".jpg" extension, then its displaying "no preview available"
NOTE:
The images are saved into database by this method
$sql = "INSERT INTO aw_offers_v2
(
id,name,description,payout_type,
payout,expiration_date,creation_date,preview_url,
tracking_url,categories,countries,countries_short,
api_key,network_id, icon,icon_size)
VALUES
('$id','$name','$description','$payout_type',
'$payout','$expiration_time','$creation_time','$preview_url',
'$tracking_url','$categories','$countries','$countries_short',
'$api','$api_url','".mysql_real_escape_string(file_get_contents($cover_image_src))."','".strlen(file_get_contents($cover_image_src))."')";
If I write this code
echo '<img src="data:image/jpeg;base64,'. base64_encode($image).'" />';
then I can see the images properly. That means the images are successfully saved.
But I can't store them in folder as files.
By looking at your code, you don't base64_encode your image.
So you can just do:
fwrite($file, $image);
You only need base64_encode when you display the image putting the data inside the src attribute of <img tag.
Also note that you can simplify this:
$file = fopen(...);
fwrite($file, $image);
fclose($file);
with a single line:
file_put_contents("./image/{$id}.jpg", $image);

how to display uploaded image on a website without saving it in a database using php

I'm working in a content based image retrieval php project.The user should upload an image to retrieve images similar to it . I need to display the query image without saving it in database. I tried two different ways to do that but it didn't work. The image appear as image icon.
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, $fileSize);
$content = addslashes($content);
I tried this:
echo '<img src="data:image/jpeg;base64,' . base64_encode( $content ) . '" height=200 width=200 />';
and this:
header("content-type: image/jpeg");
echo $content;
take a look at this jquery plugin. See Documentation section and it's a PHP implementation example. Basically, the plugin upload the image, save temporally on server file system and then send back so client to be rendered. Once it's on your file system you can do whatever you want with it (make the query, delete it for example). I think it's a good aproach, it was to me!!.
Hope This Helps!!

dynamicaly creating file ,file path not working on server machine

I am saving canvas to a file. Code creates a png file in the upload folder. It is working correctly on local machine but when i try to run this on the server, I am not able to find the file in the upload folder. am i giving wrong path ?
After file creation i am printing alert, so i get file creation alert but file is not just created in the upload folder .
if ( isset($_POST["image"]) && !empty($_POST["image"]) ) {
// get the image data
$data = $_POST['image'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
//Image name
$filename ="image". md5(uniqid()) . '.png';
$file ='../upload/'.$filename;
// decode the image data and save it to file
file_put_contents($file,$data);
}
make sure directory '../upload' exists or create it first

Categories