Context: we sell some magazines and other newspapers on our site, and updating all the cover images manually is a hassle.
I would like the ability to write a url in a field (www.myimageprovider.com/product1) and set a cron job to fetch any new images for the day, for that specific product, and update the entry with the new image.
I'm having a hard time figuring out how to "fake" a file upload from a craft controller/service, as if it was uploaded from the CP form. I have tried something like this:
$file = array();
$url = $entry->getFieldValue('imagefetchurl'); // somegoogleimageurl.com/image.png
$tmpfname = tempnam("/tmp", "UL_IMAGE");
$img = file_get_contents($url);
file_put_contents($tmpfname, $img);
$file['size'] = filesize($tmpfname); // 245 kB
$file['name'] = basename($url); // image.png
$file['tmp_name'] = $tmpfname; // C:\\Windows\\Temp\\UL_XXXX.tmp
$_FILES['assets-upload'] = $file;
$assetOperationResponseModel = craft()->assets->uploadFile(1);
This throws an error
PHP notice
getimagesize(): Read error! - in ...craft\app\services\ImagesService.php(114)
I can see that the image get downloaded, the size and name is correct.
Any pointers and ideas are greatly appreciated
EDIT
After some digging, looks like i have to write my own upload handler for this task. http://www.1-script.com/forums/php/simulate-file-upload-so-that-move-uploaded-file-succeeds-124012-.htm
Basically not possible unless you change the PHP source
I suppose i could override the function, but my instinct tells me that's a bad idea. I will have to go with an alternative approach
move_uploaded_file()
to move the file to its own internal temp storage. If I changemove_uploaded_file()
tocopy()
it works. Maybe if i set some $_POST variables it will work?