images are saving like tmp file in public folder - laravel

hi m trying to add data in db but images saves like tmp file, m storing these pics in ecommmerce\public\images\backend_images\category_images and these real pics are not saving instead their real name in db they are saving in folder like this: https://ibb.co/CHXTm3j .. any solution . here is my code:
store function:
$category = new Category;
$category->category_name = $request->category_name;
$category->category_description = $request->category_description;
$category->category_slug = $request->category_slug;
$path = $request->file('category_image');
$image = $path->getClientOriginalName();
$path->move(public_path('images/backend_images/category_images'));
$category->category_image = $image;
$category->save();

Since you are missing the parameter for filename there. Your function to move the original file should be:
$path->move(public_path('images/backend_images/category_images'), $image);
If you do not provide that parameter, the default value would be temp name of the file rather than it's original name.

Related

Recursive deleting files from storage Laravel

I have a need to be able to delete files from storage on laravel.
My idea is to loop through the files based on the folder name (course id) using this code:
$rootPath = public_path() .'/storage/' . $course_id . '/';
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($rootPath),
\RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir() === FALSE){
// Get real and relative path for current file
$filePath = $file->getRealPath();
//HERE I WANT TO DELETE THE ACTUAL FILE - BUT I CAN'T GET THE FILE NAME
//$name and $file are not carrying the filenames and hence I cant delete
the file
}
}
and then I will delete each file using: Storage::delete($file);
My issue is that my iteration is not showing the filenames within the directory and I have no idea what I am doing incorrectly.
You could just iterate over the directories and use
$success = Storage::cleanDirectory($directory);
to remove all files and directories in the specified directory.

Store results of split function in variables

I have files in a directory like below:
first-file-name
2nd-file-name
3rd-file-name
.
.
n-file-name
I need to store each portion of file name in a separate variable because I want to insert these values in separate columns of table.
For this, I used the below script to get the each portion of a file name:
$var1=$item.BaseName.Split("-",3)[0]---------first
$var2=$item.BaseName.Split("-",3)[1]---------file
$var3=$item.BaseName.Split("-",3)[2]---------name
and can save these values in a variable. But the question is how can I do this for all files, if I use foreach loop then the variable values will be overwritten???
foreach(item in $items)
{
$var1=$item.BaseName.Split("-",3)[0]---------first
$var2=$item.BaseName.Split("-",3)[1]---------file
$var3=$item.BaseName.Split("-",3)[2]---------name
}
Here, in $items I got the file path using get-childitem.
I would create a PsCustomObject with the three parts:
$parts = $items | ForEach-Object {
[PsCustomObject]#{
FirstPart = $item.BaseName.Split("-",3)[0]
SecondPart = $item.BaseName.Split("-",3)[1]
ThirdPart = $item.BaseName.Split("-",3)[2]
}
}
now $parts is an array of these objects so you can access them using e. g.
$parts[0].FirstPart

Data in loop is repeated

I am trying to work something out. I have the following
$fileString = "";
if (Input::hasFile('filePath')) {
$files = Input::file('filePath');
dd($files);
foreach($files as $file) {
$file->move(public_path('uploads'), $file->getClientOriginalName());
$fileString .= public_path('uploads') . '/' . $file->getClientOriginalName();
$uploadDoc = new ReportingDocUpload();
$uploadDoc->filename = $file->getClientOriginalName();
$uploadDoc->mime = $file->getClientOriginalExtension();
$uploadDoc->filepath = $fileString;
$uploadDoc->documentId = $dsReportingDoc->id;
$uploadDoc->save();
}
}
For each uploaded file, it should create a new row in the database. The dd I have displays something like the following if I upload 3 documents
array:3 [▼
0 => UploadedFile {#29 ▶}
1 => UploadedFile {#30 ▶}
2 => UploadedFile {#31 ▶}
]
So this all looks good. So, I have a loop which should loop each UploadedFile and save it to the database.
At the moment, this kind of works. A new row is added for each uploaded document, and everything besides one thing looks correct.
The thing that is wrong is the filePath. For the first row, this is fine. The second row in the database contains the filePath for both the first file and the second file. And the third row shows the filePath for all three rows. This should only contain the filePath for the current file.
I have looked over it, and not sure what I am missing, but for some reason this is occuring.
Am I missing something obvious here?
Thanks
Because you are setting the $fileString="" outside the loop, every time it goes in the loop, it concatenates to the exists.
Example
$fileString = ""
Loop 1:
$fileString .= a
Loop 2:
$fileString .= b
$fileString = ab
If you clear the the value in each loop, the result is what you need,
Example:
Loop 1:
$fileString = ""
$fileString = a
Loop 2:
$fileString = ""
$fileString = b
If you use the = instead of the .=, you might not need to assign it to an empty string inside the loop
Move $fileString = ""; inside the foreach loop or change $fileString .= public_path('uploads') . '/' . $file->getClientOriginalName(); to $fileString = public_path('uploads') . '/' . $file->getClientOriginalName();

steps to import images with a product

What are the steps to import a product with images. I mean - the images, precisely.
So I have this code now:
protected function _getImages($product)
{
$importImagesDirectory = Mage::getBaseDir()."/".Mage::getStoreConfig('xmlimport/product/import_images_dir');
if(file_exists($importImagesDirectory))
{
$addImage['sku'] = (string)$product['PK_Produkt'];
$addImage['_media_image'] = $importImagesDirectory . $this->_getPresentationAttributeValue($product, '#Art="Bild" and #Rang="1"');
$addImage['_media_attribute_id'] = Mage::getSingleton('catalog/product')->getResource()->getAttribute('media_gallery')->getAttributeId();
$addImage['_media_is_disabled'] = 0;
$addImage['_media_position'] = 1;
// the following 4 lines are just acquiring string values - the name of the picture
$addImage['_media_lable'] = $this->_getPresentationAttributeValue($product, '#Art="Bild" and #Rang="1"');
$addImage['image'] = $this->_getPresentationAttributeValue($product, '#Art="Bild" and #Rang="1"');;
$addImage['small_image'] = $this->_getPresentationAttributeValue($product, '#Art="Bild" and #Rang="1"');;
$addImage['thumbnail'] = $this->_getPresentationAttributeValue($product, '#Art="Bild" and #Rang="1"');;
}
else Mage::log('Error! Image with name: ' . $importImagesDirectory . ' does not exist! ', null, 'error_image.log', true);
return $addImage;
}
So this array is being merged with the array containing the product information, that is being imported. Now, I was said that the function should also move the images from my media/import/ folder to somewhere catalog/product/ folder, because Magento looks up for images in that folder. Anf if I just import the information, but don't move the images - then no effect (which actually happens right now).
So I am asking, how does that work, is there a function from the API to make this "movement", is that the correct procedure?
As per this answer you need to use addImageToMediaGallery.
You need to have an instance of a catalog/product object. That should be easy $product = Mage::getModel('catalog/product'); and then you need to load its data from the DB for example $product->load($id);.
$product->addImageToMediaGallery($filePath, $imageType, false);
Magento stores product files in media/catalog/product/X/Y/XYfilename.ext where X and Y are the first letters of the filename. So Magento creates two levels of subfolders based on the first two letters in the filename. This logic is not present directly in addImageToMediaGallery, it's in Mage_Catalog_Model_Product_Attribute_Backend_Media::addImage().
magento_root: app/code/core/Mage/Catalog/Model/Product/Attribute/Backend/Media.php

Can't get image out with appended numbering - CodeIgniter

its a silly question but I'm really bad in escaping quotes :/.
I'm using the table class to generate a table of products, and in that table each row has an image to be displayed for that product. The images are stored using their product_id as a name with an appended "_x" because there's more than one image per product, so an example of an image name is 193_1.
This is how I'm generating my rows for the table:
$table_row = array();
foreach ($r->result() as $p)
{
$table_row = NULL;
$table_row[] = "<img src='http://localhost/CI/photos/$p->product_id\"1\".jpg' height='150' width='150'/>";
$table_row[] = $p->product_id;
$table_row[] = $p->title;
$table_row[] = $p->description;
$table_row[] = $p->price;
$table_row[] = $p->stock;
$this->table->add_row($table_row);
}
But the image won't show, even when I've escaped the '1'. I only want to display the first image as that is the main one, so that's why I've hard-coded the number. Could someone please help? I've tried multiple ways i.e.
"<img src='http://localhost/CI/photos/$p->product_id '1' .jpg' height='150' width='150'/>"
"<img src='http://localhost/CodeIgniter/photos/<?php echo $p->product_id; ?>1.jpg' height='150' width='150'/>";
But the image still doesn't show. The table is being generated in my Controller which is a PHP file so I don't really think I need to use php tags
"<img src='http://localhost/CodeIgniter/images/" . $product->product_id . "_1.jpg' height='100' width='100'/>";

Resources