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
Related
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']));
simple question. I tried to save a file to storage from a form post. Code:
$path = $request->file('image')->store('image');
But the $path returns as /tmp/phpCCTWsV
How to fix this?
When you submit a form, all files will save into tmp folder. Then you must save them to storage. Ex:
$disk = Storage::disk('local');
$path = '/app/public/images/';
$name = time().'.'.$image->getClientOriginalExtension();
$disk->put(str_replace('/app', '', $path) . $name, $request->file('image'));
$path .= $name; // This file's saved in `/storage/app/app/public/images/` folder
I cant save a image using the storage method.
If I do: $file->move(public_path().'/', $file->getClientOriginalName());
The image will get saved and I can open it in finder.
But if I save the image using:
$extension = $file->guessExtension();
$filename = 'thumnail_'.$id.'.'.$extension;
if ($file) {
$s3 = Storage::disk('public')->put($filename, $file);
}
It will also get saved to the folder but then I cant open it because the file is corrupted
And to be clear "file" is sent to my model from a controller: $file = $request->file('images');
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.
So I'm receiving an image in base64 encoding. I want to decode the image and upload it in a specific directory. The encoded file is an image and I'm using $file = base64_decode($base64_string); to decode it. Uploading it via file_put_contents('/uploads/images/', $file); always results in failed to open stream: No such file or directory error.
So, how do I take the decoded string, and upload it on a specific path?
Thanks.
Your path is wrong.
/uploads/images
is checking for a folder in the / directory called uploads. You should specify the full path to the folder:
/var/www/vhosts/MYSITE/uploads/images
I had the same problem and finally solved using this:
<?php
$file = base64_decode($base);
$photo = imagecreatefromstring($file);
//create a random name
$RandomStr = md5(microtime());
$name = substr($RandomStr, 0, 3);
$name .= date('Y-m-d');
$name .= '.jpg';
//assign name and upload your image
imagejpeg($photo, 'images/'.$name, 100);