I have a form that can upload an image to my website.
I have thousand images to upload in a folder (test images). I create a symfony Command to import and copy my images to the destination folder.
$url = '/tmp/testimg.jpg';
$photo = new Photo();
$photo->setName('test name');
$photo->setFile(new UploadedFile($url, basename($url)));
$photo->upload();
When i execute the commande i have:
[Symfony\Component\HttpFoundation\File\Exception\FileException]
The file "test name" was not uploaded due to an unknown error.
Function Photo::upload:
public function upload()
{
if (null === $this->file) {
return;
}
$this->file->move(
"/home/julien/work/mysite/src/MyProject/PhotoBundle/Entity/../../../../web/uploads/photos",
$this->name
);
$this->file = null;
}
This looks like the move method crashes. Using absolute paths might be a problem here. You could try this as well:
__DIR__ . '/../../../../web/uploads/photos'
Also make sure that you have proper access to that folder.
I'd also attach a debugger to this just to be sure. You can easily set up a debug session using XDebug and step through your lines. It's always good to step into each function of your code and see where it breaks.
If your code still fails I'd suggest you implement this example and see if it works:
http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html
Related
I have some encrypted responses that I convert to a Zip file in my Laravel application. The function below downloads the API response, saves it as a Zip file, and then extracts it while I read the folder's contents. In my local environment, it works well. However, the Zip file is not getting saved to the storage folder on the live server. No error is being shown, only an empty JSON response. Please, what could be the cause?
public function downloadZipAndExtract($publication_id, $client_id)
{
/* We need to make the API call first */
$url = $this->lp_store."clients/$client_id/publications/$publication_id/file";
$file = makeSecureAPICall($url, 'raw');
// Get file path. If file already exist, just return
$path = public_path('storage/'.$publication_id);
if (!File::isDirectory($path)) {
Storage::put($publication_id.'.zip', $file);
// Zip the content
$localArchivePath = storage_path('app/'.$publication_id.'.zip');
$zip = new ZipArchive();
if (!$zip->open($localArchivePath)) {
abort(500, 'Problems experienced while reading file.');
}
// make directory with the publication_id
// then extract everything to the directory
Storage::makeDirectory($publication_id);
$zip->extractTo(storage_path('app/public/'.$publication_id));
// Delete the zip file after extracting
Storage::delete($publication_id.'.zip');
}
return;
}
First thing I'd check is if the storage file is created and if it isn't created, create it. Then I'd look at your file permissions and make sure that the the groups and users permissions are correct and that you aren't persisting file permissions on creation. I've had many instances where the process that's creating files(or trying) is not in the proper group and there is a sticky permission on the file structure.
Laravel Version: 8.35.1
PHP Version: 8.0.0
Description:
I'm uploading an image with laravel using this code:
$product_image = $request->file('product_image');
$product_image_extension = $product_image->extension();
$product_image_name = time() . '.' . $product_image_extension;
$product_image->storeAs('/media/product_images', $product_image_name);
$model->product_image = $product_image_name;
It works fine, the file is uploaded to storage/app/media/product_images/.
Then, I run the command
php artisan storage: link
to create the symlink in public.
The Command Execute Like this:
Local NTFS volumes are required to complete the operation.
The [E:\Complete Programming Bootcamp\laravel Work\ecom-project\cyber_shopping\public\storage] link
has been connected to [E:\Complete Programming Bootcamp\laravel Work\ecom-
project\cyber_shopping\storage\app/public].
The links have been created.
I am Using This Code To Display Image:
{{asset('storage/media/product_images/' . $list->product_image)}}
But Image is not displaying on the frontend.
Also, The Storage Folder Is not created in the public folder.
PLz, Help Me.
Thanks
Step 1:: Store Image
$path = ‘’;
if( $request->has('product_image') ) {
$path = $request->file('product_image')->store('media/product_images');
}
$model->product_image = $path;
Step 2:: Check Store File Path
The File Will Be Store In Path::
————————————————————————————————
Storage/app/public/media/product_images/
Step 3:: Link Storage In Public Folder
Run The Storage Link Command and remove storage link folder from the public if already exist
php artisan storage:link
Step 4:: Create Global Function To Access Images Main Controller.php File Create Global Storage Image Getting Function Like This
public static function getUrl($path)
{
$url = "";
if( !empty($path) && Storage::disk('public')->exists($path) )
$url = Storage::url($path);
return $url;
}
Step 5:: Use Function In Assest To Display Image
<img src="{{ getUrl($list->product_image) }}" />
According to https://github.com/photoncms/cms/issues/8, you are trying to symlink on fat32 drive on which it does not work. Try to test it on NTFS drive
I have tried to setup an upload script in Laravel and have followed the instructions in the docs.
I created a Symlink using the Laravel script and it looks like the following
storage -> /Users/username/Sites/switch/storage/app/public
The problem arrives when I go to upload the image and then get result of the image url in return. As you can see to match the symlink I set the folder to be public below.
$path = $request->file('manufacturer_image_name')->store('public');
echo asset($path);
and this returns
http://127.0.0.1:8000/public/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
the problem is this doesn't work and throws a 404 but if I manually change the url from "public" to "storage" it will find the image.
http://127.0.0.1:8000/storage/XxIX7L75cLZ7cf2xzejc3E6STrcjfeeu3AQcSKz1.png
Shouldn't
echo asset($path);
be returning a url containing storage instead of public?
assett($path) is for generating a URL for assets that are just in the public folder, things like the Mix generated CSS and JS files. If you user Laravel Storage to save the file, you also have to use Laravel storage to generate the file URL.
Storage::url('file.jpg');
Well, there are a lot of ways to do that, pick anyone which fits you best.
// using storage_path helper
storage_path('public/' . $filename);
// you could make a double-check with File::exist() method
$path = storage_path('public/' . $filename);
if (!File::exists($path)) {
abort(404);
}
// using asset helper
asset('storage/your_folder/image.png');
// using url helper
url('storage/your_folder/image.png');
// using Storage facade
Storage::url($photoLink)
Here is the simplest and exact thing for your issue
if(!empty($request->file('manufacturer_image_name'))){
$path = storage_path('public/image/');
$image_path = Storage::disk('public')->put('manufacturer_image_name', $request->file('manufacturer_image_name'));
//Assuming you have a model called Manufacturer and created $manufacturer = new Manufacturer()
$manufacturer->manufacturer_image_name = isset($image_path) ? "storage/".$image_path : "";
}
Thanks for the help, I discovered this answer the fits nearly perfectly what I am after. Laravel: Storage not putting file inside public folder
This was what I ended up with.
if($request->file('manufacturer_image_name')){
$path = Storage::disk('public')->put('logo', $request->file('manufacturer_image_name'));
echo $path;
}
$path now returns "logo/filename.ext" instead of "public/ or storage/" so I can store this directly in the db.
Currently I have a project that is running Laravel 5.8 and I am trying to use a form that allows user's to upload their own images to the site and store it in the public folder. When trying to use the Storage facade with the 'put' method, a path gets returned but the images do not actually get stored.
if ($request->hasfile('images')) {
foreach ($request->file('images') as $file) {
$path = Storage::disk('public')->put('images', $file);
echo $path;
}
}
This is the code I am trying to use. The path gets echo'd as it should and I have not made any changes to the filesystem config. In the form, the input field images allows for submitting multiple files and the form does hasenctype="multipart/form-data". The $file variable also contains an instance of Illuminate\Http\UploadedFile.
The previous code I used which did work was:
if ($request->hasfile('images')) {
foreach ($request->file('images') as $file) {
$file->move(public_path('images'), $file->getClientOriginalName());
}
}
I would be okay with using my previous code if Laravel can give it a unique file name on upload but what would be causing my code with the Storage facade to not save the images properly? Does the put function just not work like that or is there something I am overlooking?
Edit:
So I realise the images were in fact saving correctly as they should be. I was looking inside the public folder rather than the storage folder which is why my second code example 'worked' but not the first. I realise when using the Storage facade I need to make use of Symbolic linking if I want to access these files on the web.
You need to also make sure you have created the symbolic link on your Ubuntu server or Windows development machine to the storage folder.
Windows you can use : mklink /j /path/to/laravel/public/youfolder /path/to/laravel/storage/youfolder
Ubuntu: ln -s /path/to/laravel/public/youfolder /path/to/laravel/storage/youfolder
To check & set you can also use use php artisan storage:link
Hope this helps
if ($request->hasfile('images')) {
foreach ($request->file('images') as $file) {
$path = Storage::disk('public')->put('images', $file);
$new_file_name = time() . "_" . uniqid() . "_" . $file->getClientOriginalName();
$path = Storage::disk('public')->put($new_file_name, file_get_contents($file));
echo $path;
}
}
I'm trying to extract zip file to some folder in storage_path (I'm using laravel). But I don't know why they comes with .(dot)files when it extracted, also I want to extract them without creating another folder. Here's the code I'm using:
lines inside some public function:
$src = storage_path('application');
$dst = storage_path('tmp/'.time());
$zipfile = $request->file('splashicon');
$zipfile->move($dst, 'splashicon');
$this->splashIcon($dst.'/splashicon', $dst);
splashIcon function:
public function splashIcon($src, $dst)
{
$zip = new ZipArchive();
$x = $zip->open($src); // open the zip file to extract
if ($x === true) {
$zip->extractTo($dst.'/www'); // place in the directory with same name
$zip->close();
unlink($src); //Deleting the Zipped file
}
}
The current result is:
1494618313/www/name.files/thecontentsofzipfile
I have no idea where .(dot) and files came from, please explain what was happened to them.
And by the way, the correct result should be something like this:
1494618313/www/thecontentsofzipfile
Thank you,
This is a laravel convention.
The . means it's a subfolder/subfile within your directory.
I'm not really sure how to get around it.
You might look into Zipper, which is an extraction helper for Laravel. I believe it has an exact match for extraction instead of using the typical Laravel syntax.
https://github.com/Chumper/Zipper