how to download users twitter images from API with php - 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

Related

Store blob as a file in S3 with Laravel

I am using a file upload plugin in Vue to upload some image and pdf files through an API and it has an option to create a blob field when uploading the files.
The request sent to Laravel is as follows. I can access the blob from the browser by copying and pasting the URL on the browser.
On the Server side code, I am trying to save the file with the following code but the saved file is some corrupted 64byte file rather than the actual image. How would we store the blob as a normal file in the filesystem?
if ($request->has('files')) {
$files = $request->get('files');
$urls = [];
foreach ($files as $file) {
$filename = 'files/' . $file['name'];
// Upload File to s3
Storage::disk('s3')->put($filename, $file['blob']);
Storage::disk('s3')->setVisibility($filename, 'public');
$url = Storage::disk('s3')->url($filename);
$urls[] = $url;
}
return response()->json(['urls' => $urls]);
}
Try by replacing this:
Storage::disk('s3')->put($filename, $file['blob']);
with this:
Storage::disk('s3')->put($filename, base64_decode($file['blob']));

How can i Download file to local directly from Dropbox API

I m using https://github.com/kunalvarma05/dropbox-php-sdk this library in my laravel project .From this code this "example.txt" file downloaded in my project directory. But i want to download this "example.txt" file with my browser into browser download folder.
$file = $dropbox->download("/example.txt");
$contents = $file->getContents();
$metadata = $file->getMetadata();
file_put_contents(__DIR__ . "/example.txt", $contents);
A little use with Google and found it for you.
$file = $dropbox->download("/my-logo.png");
//File Contents
$contents = $file->getContents();
//Save file contents to disk
file_put_contents(__DIR__ . "/logo.png", $contents);
//Downloaded File Metadata
$metadata = $file->getMetadata();
//Name
$metadata->getName();
link to examples and github:
https://github.com/kunalvarma05/dropbox-php-sdk/wiki/Upload-and-Download-Files

How to save images from list image url in csv

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.

Recovery Symfony2 session

I am a beginner in PHP and am working on a web application developed in Symfony 2, in this application I built a flash module that records the sound of the microphone and save the mp3 file to the connected account folder.
(each user must be connected for it to make recording)
here is my code which is used to save the mp3 file to a folder (recording / user1)
<?php
$songname = $_GET["name"];
$filename = $songname. " - " . date("dmY"). ".mp3";
$file = "recording/" . $filename;
$songname $_GET["name"];
$putdata = fopen("php://input", "r");
$fp = fopen($file, "w");
while ($data = fread($putdata, 1024))
fwrite($fp, $data);
fclose($fp);
fclose($putdata);
echo $filename;
}
?>
My question: If possible, please retrieve the session or the name and user name connected and send the mp3 file directly to each user's folder (folders are created in advance in the recording folder).
thank you

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

Categories