0

I have custom table with some fields including the image.

Now I want to import the data from CSV to custom table with the image.

Fields are like first_name, last_name, image.

I have done import for the text fields but need some idea regarding image upload.

In the image column I will pass the name of image. When import I need to import the image to the specific folder from given path on the site.

For example, I will upload images by FTP to pub/media/upload_images add the image name in the CSV when I import CSV it will take image from pub/media/upload_images and upload to employee_images folder.

$employeeData = [];
$tableName = $connection->getTableName('employee_data');
foreach ($csvData as $rowData) {
 $employeeData[] = [
 "first_name" => $rowData['first_name'],
 "last_name" => $rowData['last_name'],
 "image" => $rowData['image']
 ];
}
if (count($employeeData)) {
 $connection->insertMultiple($tableName, $employeeData);
 }
asked Jun 9, 2023 at 12:50

1 Answer 1

1

You can use the below code after insert records. To move an image from one location to another location

protected $backendFile
public function __construct(
 \Magento\Framework\Filesystem $backendFile
) {
 $this->mediaDirectory = $filesystem->getDirectoryWrite(\Magento\Framework\Filesystem\DirectoryList::MEDIA);
}
$result = $this->mediaDirectory->copyFile($SourcePath,$DestinationPath);

You can get file path by using this : https://magento.stackexchange.com/a/176917/82670

answered Jun 9, 2023 at 13:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.