Upload Image/File from URL

Hello. I have an action on Edit Resource where i randomly download an image from web and upload it to the field but its not working.. what i am doing wrong. Here is what i am trying..
filePath = UrlUploadedFile::createFromUrl($imageLink)->store('posts/featured_images');
// $file = UrlUploadedFile::createFromUrl($imageLink);

// $this->record->featured_image = $filePath;
// $this->record->featured_image = $file;
$this->record->featured_image = TemporaryUploadedFile::createFromLivewire($filePath);
$this->record->save();

$this->refreshFormData([
'featured_image',
]);

class UrlUploadedFile extends UploadedFile
{
public static function createFromUrl(string $url, string $originalName = '', string $mimeType = null, int $error = null, bool $test = false): self
{
if (! $stream = @fopen($url, 'r')) {
throw new Exception('Can\'t open file from url ' . $url . '.');
}

$tempFile = tempnam(sys_get_temp_dir(), 'url-file-');

file_put_contents($tempFile, $stream);

return new static($tempFile, $originalName, $mimeType, $error, $test);
}
}
filePath = UrlUploadedFile::createFromUrl($imageLink)->store('posts/featured_images');
// $file = UrlUploadedFile::createFromUrl($imageLink);

// $this->record->featured_image = $filePath;
// $this->record->featured_image = $file;
$this->record->featured_image = TemporaryUploadedFile::createFromLivewire($filePath);
$this->record->save();

$this->refreshFormData([
'featured_image',
]);

class UrlUploadedFile extends UploadedFile
{
public static function createFromUrl(string $url, string $originalName = '', string $mimeType = null, int $error = null, bool $test = false): self
{
if (! $stream = @fopen($url, 'r')) {
throw new Exception('Can\'t open file from url ' . $url . '.');
}

$tempFile = tempnam(sys_get_temp_dir(), 'url-file-');

file_put_contents($tempFile, $stream);

return new static($tempFile, $originalName, $mimeType, $error, $test);
}
}
7 Replies
Mansoor Khan
Mansoor KhanOP2y ago
How can i replace the image state?
Dan Harrin
Dan Harrin2y ago
you need to download the image, save it to the filesystem disk, then save the relative file name in the disk to the database that will allow filament to generate a URL from it you can check the format and location of files by uploading one manually and seeing what the file upload field saves to the db normally
Mansoor Khan
Mansoor KhanOP2y ago
Let me try that!
Mansoor Khan
Mansoor KhanOP2y ago
Its working as expect but i get error: https://flareapp.io/share/dPbq18Em#F64
$filePath = UrlUploadedFile::createFromUrl($imageLink)->store('posts/featured_images', ['disk' => 'public']);
$this->record->featured_image = $filePath; // $filePath is a string: posts/featured_images/abc.jpg
$this->record->save();
$filePath = UrlUploadedFile::createFromUrl($imageLink)->store('posts/featured_images', ['disk' => 'public']);
$this->record->featured_image = $filePath; // $filePath is a string: posts/featured_images/abc.jpg
$this->record->save();
Flare
foreach() argument must be of type array|object, string given - The error occurred at http://mortgagecs.test/admin/pages/20/edit
Mansoor Khan
Mansoor KhanOP2y ago
The file gets uploaded, saved properly to database and when i reload the file also gets previewed. I am not sure why i get this error. Oh I have this line which triggers this error:
$this->refreshFormData([
'featured_image',
]);
$this->refreshFormData([
'featured_image',
]);
How would reload the field/component from action so that it previews newly uploaded file? I have a workaround for now to reload the entire page. But would be nice if i can find a way to refresh the image preview in form.
$this-
>redirect(route('filament.resources.pages.edit', $this->record->id));
$this-
>redirect(route('filament.resources.pages.edit', $this->record->id));
Dan Harrin
Dan Harrin2y ago
instead of $this->refreshFormData(); try $this->data['featured_image'] = [$this->record->featured_image];
Mansoor Khan
Mansoor KhanOP2y ago
Let me try sir! Thanks for the help! Oh this works like a charm. Love you Dan!
Want results from more Discord servers?
Add your server