When I store an image in Laravel by doing:
$path = $request->file('myImage')->store('public/src/');
It returns the full path, but how do I get only the filename it was given?
This is an example of the returned path:
public/src/ltX4COwEmvxVqX4Lol81qfJZuPTrQO6S2jsicuyp.png
Here, you can try this one.
$fileNameWithExt = $request->file('myImage')->getClientOriginalName();
$fileNameWithExt = str_replace(" ", "_", $fileNameWithExt);
$filename = pathinfo($fileNameWithExt, PATHINFO_FILENAME);
$filename = preg_replace("/[^a-zA-Z0-9\s]/", "", $filename);
$filename = urlencode($filename);
$extension = $request->file('myImage')->getClientOriginalExtension();
$fileNameToStore = $filename.'_'.time().'.'.$extension;
$path = $request->file('myImage')->storeAs('public/src/',$fileNameToStore);
return $fileNameToStore;
You will get your stored filename in $fileNameToStore.
Also, all the spaces will be replaced with "_" and you will get your stored filename with current time attached with it, which will help you differentiate between two files with the same name.
Since $path returns the full path to the saved file, it contains its generated name.
You have just to parse this string :
$extension = explode('/', $path);
$filename = end($extension)
which will give you ltX4COwEmvxVqX4Lol81qfJZuPTrQO6S2jsicuyp.png
In Laravel, the store() method generates the name dynamically .. so you can't get it from the store() method.
But you can use storeAs() method. Basically the store() method is calling the storeAs() method. So:
$path = $request->file('myImage')->store('public/src');
What Laravel is doing is calling ->storeAs('public/src', $request->file('myImage')->hashName()); .. you see the hashName() method? that is what generates the name.
So you can call hashName() first and know your name before the storing happens .. here is an example:
$uploadFile = $request->file('myImage');
$file_name = $uploadFile->hashName();
$path = $uploadFile->storeAs('public/src', $file_name);
Now you have $file_name and $path.
See:
https://laravel.com/docs/6.x/filesystem#file-uploads .. Specifying A File Name
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Http/UploadedFile.php#L33
https://github.com/laravel/framework/blob/6.x/src/Illuminate/Http/FileHelpers.php#L42
Related
in laravel controller i am trying to give each video file i uploading should be rename as
**myfile.mp4**
and save in public folder.
but my present code makes random number for my files but i need to give name as myfile
my controller
$input['file_id'] = time() . '.' . $request->file_id->getClientOriginalExtension();
$folder1 = public_path('/public');
$path1 = $folder1 . $input['file_id']; // path 1
$request->file_id->move($folder1, $input['file_id']);
What you want is :
$input['file_id'] = 'myfile.'.$request->file_id->getClientOriginalExtension();
But you cannot give the same name 'myfile' to all your videos because each recording will be overwritten by the previous one, you need unique names.
For this you can do, for example:
$input['file_id'] = 'myfile'.time().'.'.$request->file_id->getClientOriginalExtension();
// here, time() represents the time at which the video was saved in your '/public' file
or
$input['file_id'] = 'myfile'.date().'.'.$request->file_id->getClientOriginalExtension();
// here, date() represents the date the video was saved in your '/public' file
I am currently trying to display the sharepoint thumbnail in a picturebox when i click a button on the form. What appears to be happening is the file is locked and will not let me replace the file or anything. I even created a counter so the file name is always different. When I run the first time everything works, after that I believe it cant write over the file. Am I doing something wrong is there a better method??
$User=GET-ADUser $UserName –properties thumbnailphoto
$Filename='C:\Support\Export'+$Counterz+'.jpg'
#$img = $Filename.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
[System.Io.File]::WriteAllBytes($Filename, $User.Thumbnailphoto)
$Picture = (get-item ($Filename))
$img = [System.Drawing.Image]::Fromfile($Picture)
$pictureBox.Width = $img.Size.Width
$pictureBox.Height = $img.Size.Height
$pictureBox.Image = $img
$picturebox.dispose($Filename)
Remove-Item $Filename
You should be able to do this without creating a temporary file.
Just create $img like:
$img = [System.Drawing.Image]::FromStream([System.IO.MemoryStream]::new($User.thumbnailPhoto))
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
Don't forget to remove the form from memory after closing with $form.Dispose()
If you insist on using a temporary file, then be aware that the $img object keeps a reference to the file untill it is disposed of.
Something like:
# get a temporary file name
$Filename = [System.IO.Path]::GetTempFileName()
[System.IO.File]::WriteAllBytes($Filename, $User.thumbnailPhoto)
# get an Image object using the data from the temporary file
$img = [System.Drawing.Image]::FromFile($Filename)
$pictureBox.Width = $img.Width
$pictureBox.Height = $img.Height
$pictureBox.Image = $img
$form.Controls.Add($pictureBox)
$form.ShowDialog()
# here, when all is done and the form is no longer needed, you can
# get rid of the $img object that still has a reference to the
# temporary file and then delete that file.
$img.Dispose()
Remove-Item $Filename
# clean up the form aswell
$form.Dispose()
I want to store my image upload path in table.Currently it returns
/storage/images/user/business_card/1524811791.jpg"
I want to store full access URL for example abc.com/storage/images/user/business_card/1524811791.jpg
How can i do this ?
Image Upload Code :
if(Input::file('profile_picture'))
{
$profilepic = Input::file('profile_picture');
$filename = time() . '.' . $profilepic->getClientOriginalExtension();
$request->file('profile_picture')->storeAs('public/images/user/profile', $filename);
$accessUrl = Storage::url('images/user/profile'). '/' . $filename;
$url = Storage::put('public/user/profile/', $filename);
$saveUserInformation->profile_picture = $accessUrl;
$saveUserInformation->save();
}
First of all it is bad practice. Imagine a problem when your domain changes, then you need to make huge work in database. Therefore the relative path is better.
But If you realy need absolute path you can take domain:
{{ Request::root() }}
Or
{{ Request::server ("SERVER_NAME") }}
and add it before storage path.
Good luck!
$saveUserInformation->profile_picture = URL::to('/').$accessUrl;
My code to upload image like this :
$file = $file->move($path, $fileName);
The code works
But I want to change it using Storage::put like this reference :
https://laravel.com/docs/5.6/filesystem#storing-files
I try like this :
Storage::put($fileName, $path);
It does not works
I'm confused, where I must put $file on the code
How can I solve this problem?
Update :
$file = file of image
$path = storage_path('/app/public/product/')
$fileName = chelsea.jpg
So I want to save the file with name chelsea.jpg on the /app/public/product/
Easy Method
$path = $request->file('avatar')->storeAs(
'avatars', $request->user()->id
);
This will automatically store the files in your default configuration.
This is another example
Storage::put($fileName, $path);
Hope this helps
I can change my code to save the uploaded image in the public dir but not when I want to their uploaded image in a folder as their company's name. For example of what works:
/public/company_img/<filename>.jpg
If the user's company name is Foo, I want this when they save save their uploaded image:
/public/company_img/foo/<filename>.jpg
This is in my controller:
$image = Input::file('company_logo');
$filename = $image->getClientOriginalName();
$path = public_path('company_img/' . Auth::user()->company_name . '/' . $filename);
// I am saying to create the dir if it's not there.
File::exists($path) or File::makeDirectory($path); // this seems to be the issue
// saving the file
Image::make($image->getRealPath())->resize('280', '200')->save($path);
Just looking at that you can easily see what it's doing. My logs shows nothing and the browser goes blank after I hit the update button. Any ideas
File::exists($path) or File::makeDirectory($path);
This line does not make sense, as you check if a file exists and if not you want to attempt to create a folder ( in your $path variable you saved a path to a file not to a directory )
I would do something like that:
// directory name relative to public_path()
$dir = public_path("company_img/username"); // set your own directory name there
$filename = "test.jpg"; // get your own filename here
$path = $dir."/".$filename;
// check if $folder is a directory
if( ! \File::isDirectory($dir) ) {
// Params:
// $dir = name of new directory
//
// 493 = $mode of mkdir() function that is used file File::makeDirectory (493 is used by default in \File::makeDirectory
//
// true -> this says, that folders are created recursively here! Example:
// you want to create a directory in company_img/username and the folder company_img does not
// exist. This function will fail without setting the 3rd param to true
// http://php.net/mkdir is used by this function
\File::makeDirectory($dir, 493, true);
}
// now save your image to your $path
But i really can't say your behaviour has something to do with that... Without error messages, we can only guess.