Can i display/open/preview base64 pdf without store in Laravel - php

Currently I am storing file into storage folder then show that file into browser and when close that file then remove that file from storage but I want directly show into browser and also can be download without store. This is working but storing file.
$fileFromAPI = $apiResponse->file;
$bin = base64_decode($fileFromAPI, true);
$path = config('pdf_paths.file') . $id . "/";
if (!is_dir($path)) {
$oldmask = umask(0);
Storage::makeDirectory($path, 0777, true);
umask($oldmask);
}
Storage::put($path . 'file_' . $fid . '.pdf', $bin);
$pdfread = 'storage/' . $path . 'file_' . $fid . '.pdf';
return response()->file($pdfread)->deleteFileAfterSend(true);

I found solution just set header and return it.
$fileFromAPI = $apiResponse->file;
$bin = base64_decode($fileFromAPI, true);
return response($bin)
->header('Content-Type', 'application/pdf');

Related

How to check Image name in folder and rename it?

I have a csv file with image name and Prefix for image and I want to add prefix in that image name and rename it and move it to another directory.
<?php
$fullpath = '/var/www/html/john/Source/01-00228.jpg';
$additional = '3D_Perception_';
while (file_exists($fullpath)) {
$info = pathinfo($fullpath);
$fullpath = $info['dirname'] . '/' . $additional
. $info['filename']
. '.' . $info['extension'];
echo $fullpath ;
}
?>
Here Image file is store in Source Directory I want to rename it with some prefix and move it other directory like Destination
Please help me out to find solution for this.
<?php
$source_directory = '/var/www/html/john/Source/';
$dest_directory = '/var/www/html/john/Source/'; // Assuming you'll change it
$filename = '01-00228.jpg';
$prefix = '3D_Perception_';
$file = $source_directory . $filename;
$dest_file = $dest_directory . $prefix . $filename;
if (file_exists($file)) {
if (copy($file, $dest_file)) {
unlink($file); // Deleting source file.
var_dump(pathinfo($dest_file)); // Dump dest file infos, replace it with wathever you want to do.
} else {
// if copy doesn't work, do something.
}
}
?>
Try this, and take care about my comments. ;)

file_get_contents() cannot read PHP tags <?php ?>

I am using cakePHP and got stuck with this riddle.
Time ago i made function that inserts custom routes dynamically in file using file_get_contents() and string replace functions.
$filename = "301routes.php";
$path = SERVER_PUBLIC_PATH . 'app' . DS . 'config';
$add_data = "Router::connect('".$old."', array('controller'=>'index_router', 'action'=>'redirect_301','".$new."'));\n";
$file_data = file_get_contents($path . DS . $filename);
$file_data = str_replace("<?php\n","<?php\n".$add_data,$file_data);
file_put_contents($path . DS . $filename, $file_data);
this worked, but now i tried to put exactly the same function in other project and its working only when there is no PHP tag. Problem is with file_get_contents() function becouse it reads file well when content is:
// routes here
Router::connect('/articles/category/en/test', array('controller'=>'index_router', 'action'=>'redirect_301','/articles/category/en/test-old'));
but when i change beginning like this
<?php
Router::connect('/articles/category/en/test', array('controller'=>'index_router', 'action'=>'redirect_301','/articles/category/en/test-old'));
?>
file_get_contents() returns only part of file:
string(154) "'index_router', 'action'=>'redirect_301','/articles/category/en/test-old')); ?>"
Can someone help me with this?
To run php codes and assign to $file_data variable,
ob_start();
$file_data = file_get_contents($path . DS . $filename);
ob_end_flush();
Try: Create new file 301routes.php in config directory then read write permission of file.
$filename = "301routes.php";
$path = __DIR__;
$add_data = "Router::connect('".$old."', array('controller'=>'index_router', 'action'=>'redirect_301','".$new."'));\n";
$file_data = file_get_contents($path . DS . $filename);
$file_data = str_replace("<?php\n","<?php\n".$add_data,$file_data);
file_put_contents($path . DS . $filename, $file_data);

php check file name exist, rename the file

How do I check if file name exists, rename the file?
for example, I upload a image 1086_002.jpg if the file exists, rename the file as 1086_0021.jpg and save, if 1086_0021.jpg is exist, rename 1086_00211.jpg and save , if 1086_00211.jpg is exist, rename 1086_002111.jpg and save...
Here is my code, it only can do if 1086_002.jpg exist, rename the file as 1086_0021.jpg, maybe should do a foreach, but how?
//$fullpath = 'images/1086_002.jpg';
if(file_exists($fullpath)) {
$newpieces = explode(".", $fullpath);
$frontpath = str_replace('.'.end($newpieces),'',$fullpath);
$newpath = $frontpath.'1.'.end($newpieces);
}
file_put_contents($newpath, file_get_contents($_POST['upload']));
Try something like:
$fullpath = 'images/1086_002.jpg';
$additional = '1';
while (file_exists($fullpath)) {
$info = pathinfo($fullpath);
$fullpath = $info['dirname'] . '/'
. $info['filename'] . $additional
. '.' . $info['extension'];
}
Why not just append a timestamp onto the filename? Then you won't have to worry about arbitrarily long filenames for files which have been uploaded many times.
I hope this helps
$fullPath = "images/1086_002.jpg" ;
$fileInfo = pathinfo($fullPath);
list($prifix, $surfix) = explode("_",$fileInfo['filename']);
$x = intval($surfix);
$newFile = $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $prifix. "_" . str_pad($x, 2,"0",STR_PAD_LEFT) . $fileInfo['extension'];
while(file_exists($newFile)) {
$x++;
$newFile = $fileInfo['dirname'] . DIRECTORY_SEPARATOR . $prifix. "_" . str_pad($x, 2,"0",STR_PAD_LEFT) . $fileInfo['extension'];
}
file_put_contents($newFile, file_get_contents($_POST['upload']));
I hope this Helps
Thanks
:)
I feel this would be better. It will help keep track of how many times a file with the same name was uploaded. It works in the same way like Windows OS renames files if it finds one with the same name.
How it works: If the media directory has a file named 002.jpg and you try to upload a file with the same name, it will be saved as 002(1).jpg Another attempt to upload the same file will save the new file as 002(2).jpg
Hope it helps.
$uploaded_filename_with_ext = $_FILES['uploaded_image']['name'];
$fullpath = 'media/' . $uploaded_filename_with_ext;
$file_info = pathinfo($fullpath);
$uploaded_filename = $file_info['filename'];
$count = 1;
while (file_exists($fullpath)) {
$info = pathinfo($fullpath);
$fullpath = $info['dirname'] . '/' . $uploaded_filename
. '(' . $count++ . ')'
. '.' . $info['extension'];
}
$image->save($fullpath);
You can change your if statement to a while loop:
$newpath = $fullpath;
while(file_exists($newpath)) {
$newpieces = explode(".", $fullpath);
$frontpath = str_replace('.'.end($newpieces),'',$fullpath);
$newpath = $frontpath.'1.'.end($newpieces);
}
file_put_contents($newpath, file_get_contents($_POST['upload']));

saving blob to image files in php

I have an image in my database saved as a large BLOB. and now I want to retrieve it and save it to my file system.
for this purpose I have done the following, but the images that were saved to my file system were corrupted ones.
Approach1:
$basedir = 'images/';
$imagename = strtolower(preg_replace('/([^\w\d\-_]+)/', '-', $row->name));
$filename = $basedir . $imagename . '_' . $row->id. '.jpg';
$file_content = base64_decode($row->image_data);
return file_put_contents($filename, $file_content);
Approach2:
$basedir = 'images/';
$imagename = strtolower(preg_replace('/([^\w\d\-_]+)/', '-', $row->name));
$filename = $basedir . $imagename . '_' . $row->id . '.jpg';
fopen($filename,'w');
if($fh = fopen("{$filename}", "wb")) {
fwrite($fh, base64_decode($row->image_data));
fclose($fh) ;
}
Please help!!!
This should do the trick if the BLOB isn't encoded:
file_put_contents('filename.jpg', $row->image_data);

permissions on a new folder in php

My code looks like this:
do
{
$rand = rand(1,10000000);
$name_of_file_clear = $rand ;
$name_of_file = $rand . '.JPG' ;
$name_of_file_t = $rand . '_t.JPG' ; // for thubnsdffafasf
$this_directory_path = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
$images_directory_path = $_SERVER['DOCUMENT_ROOT'] . $this_directory_path . 'img/' . $name_of_file_clear . '/';
$whole_path = $images_directory_path.$name_of_file;
} while (file_exists($whole_path));
mkdir($images_directory_path, 777);
chmod($images_directory_path, 777);
move_uploaded_file($temp_file_name, $whole_path);
The problem is, when I try uploading file, the file is not uploaded. I think the problem is something with the permissions. Help me please, I am new to php.
You're specifying them in decimal. Try specifying them in octal instead.
mkdir($images_directory_path, 0777);

Categories