This is My File Uploader in .phtml file :
<input type="file" id="imageUploader" name="imageUploader">
This my PHP file code where form post.
<?php
namespace VendorName\ExtensionName\Service;
use Magento\Catalog\Model\Product;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem\Io\File;
class ImportImageService
{
protected $directoryList;
protected $file;
public function __construct(
DirectoryList $directoryList,
File $file
) {
$this->directoryList = $directoryList;
$this->file = $file;
}
public function execute()
{
$tmpDir = $this->getMediaDirTmpDir();
/** create folder if it is not exists */
$this->file->checkAndCreateFolder($tmpDir);
/** @var string $newFileName */
//$imageUrl='file:///home/chakko/Downloads/mywatch.jpg';
$imageUrl=$_FILES['imageUploader']['name'];
$newFileName = $tmpDir . baseName($imageUrl);
/** read file from URL and copy it to the new destination */
$result = $this->file->read($imageUrl, $newFileName);
if ($result) {
/** add saved file to the $product gallery */
$product->addImageToMediaGallery($newFileName, $imageType, true, $visible);
}
return $result;
}
/**
* Media directory name for the temporary file storage
* pub/media/tmp
*
* @return string
*/
protected function getMediaDirTmpDir()
{
return $this->directoryList->getPath(DirectoryList::MEDIA) . DIRECTORY_SEPARATOR . 'tmp';
}
}
The Problem is when I give manually path of the image like $imageUrl='file:///home/chakko/Downloads/mywatch.jpg';
is work good.
But when use fileuploader
like this $imageUrl=$_FILES['imageUploader']['name'];
it will not read the file .
Can any will help me i will be appriciate him
Ref: https://www.atwix.com/magento-2/import-product-image-from-url/