Problem to import photos
Permalink
Hello,
In my code I can't import file (image). In the header I declare :
use File;
use Concrete\Core\File\Importer as FileImporter;
use FileSet;
use FileList;
In log error, I have $newFile=11 and $fs is empty, and I dont know why is empty.
The help please.
In my code I can't import file (image). In the header I declare :
use File;
use Concrete\Core\File\Importer as FileImporter;
use FileSet;
use FileList;
$image_upload = $pfl->COL[3]->DATA; if (@$image = file_get_contents($image_upload)) { $basePath = dirname(__FILE__); $img = "/portfolio_image_/"; $filePathImg = $basePath . $img; $imageName = $pfl->COL[0]->DATA.".jpg"; @file_put_contents($img, $image); $fi = new FileImporter(); $newFile = $fi->import($filePathImg.$imageName); $fs = FileSet::getByID(11); $fsf = $fs->addFileToSet($newFile); $newPage->setAttribute('image_handler',$newFile); }
In log error, I have $newFile=11 and $fs is empty, and I dont know why is empty.
The help please.
Always doesn't work.
I have always the problem in this line :
Even I changed
What can I do?
:-(
I have always the problem in this line :
$fs = FileSet::getByID(11);
Even I changed
$fs = FileSet::getByName('Fileset Name');
What can I do?
:-(
Have you already setup a File Set in your Dashboard? If so, what is the name of it? Replace "Fileset Name" with the name of your actual file set and that should work.
Is the Fileset is in files collections ?
if the response is yes I change that what I have, and the system give me this error :
Object of class Concrete\Core\File\Set\Set could not be converted to string
:-)
if the response is yes I change that what I have, and the system give me this error :
Object of class Concrete\Core\File\Set\Set could not be converted to string
:-)
I'm not exactly sure what you mean but the use statement at the top should be Concrete\Core\File\Set
I apologize if I did not express well and IM not very good in english :-)
Just I want to know where is the File Set in my Dashbaord ?
Just I want to know where is the File Set in my Dashbaord ?
Ah yes, of course. It's Dashboard -> Files -> File Sets. You can add/edit/view all of your File Sets there.
That's good, I created one under name "portfolio". and I changed the line for this
And in autometed jobs I have this message:
Object of class Concrete\Core\File\Set\Set could not be converted to string
$fs = FileSet::getByName('portfolio');
And in autometed jobs I have this message:
Object of class Concrete\Core\File\Set\Set could not be converted to string
That's because it's an object and you are trying to write it to your log file as a string. Comment out the log file stuff and I bet that error will go away.
Unfortunately, I dont have comment out the log file because the excution stopped in the error line ($fs = FileSet::getByName('portfolio'); )
@file_put_contents($img, $image); $fi = new FileImporter(); $newFileID = $fi->import($filePathImg.$imageName); $newFile = File::getByID($newFileID); #$fs = FileSet::getByID(11); $fs = FileSet::getByName('portfolio'); #$fsf = $fs->addFileToSet($newFile); #$newPage->setAttribute('image_handler',$newFileID); $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO); $l->write('filePath = '.$fs); $l->close();
I am 100% certain that this line ->$l->write('filePath = '.$fs); is causing your error.
hhhhhh Why you told this.
You know I changed the variable and you can see the attachement.
.
You know I changed the variable and you can see the attachement.
@file_put_contents($img, $image); $fi = new FileImporter(); $newFileID = $fi->import($filePathImg.$imageName); $newFile = File::getByID($newFileID); #$fs = FileSet::getByID(11); #$file_set = Config::get('conversations.attachments_pending_file_set'); #$fs = FileSet::getByName("portfolio"); #$fsf = $fs->addFileToSet($newFile); #$newPage->setAttribute('image_handler',$newFileID); $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO); $l->write('filePath = '.$newFileID ); $l->close();
I'm sorry, I'm having a bit of a hard time understanding exactly what you need but I think if you update your code to look like this, everything should work.
So, the file is imported, which returns the FileID (I believe). Then you use the FileID to get the File object. You then use the File Set Name to get the File Set, add the new file, to the file set and then set the image_handler attribute on your page to the new FileID, is that what you're trying to do?
@file_put_contents($img, $image); $fi = new FileImporter(); $newFileID = $fi->import($filePathImg.$imageName); $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO); $l->write('newFileID = '.$newFileID ); $l->close(); $newFile = File::getByID($newFileID); $fs = FileSet::getByName("portfolio"); $fs->addFileToSet($newFile); $newPage->setAttribute('image_handler', $newFileID);
So, the file is imported, which returns the FileID (I believe). Then you use the FileID to get the File object. You then use the File Set Name to get the File Set, add the new file, to the file set and then set the image_handler attribute on your page to the new FileID, is that what you're trying to do?
I try you code and I dont have error, thank you :-), it's very good but the file doesn't imported.
The file doesn't show up in the File Manager in the Dashboard, or it doesn't show up in the File Set?
The file doesn't show up in the File Manager in the Dashboard, but I have anything in File Set
What do you get if you try this:
$image_upload = $pfl->COL[3]->DATA; if (@$image = file_get_contents($image_upload)) { $basePath = dirname(__FILE__); $img = "/portfolio_image_/"; $filePathImg = $basePath . $img; $imageName = $pfl->COL[0]->DATA.".jpg"; $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO); $l->write('filePath = '.$filePathImg.$imageName); $l->close(); $fi = new FileImporter(); $newFileVersion = $fi->import($filePathImg.$imageName); if (!($newFileVersion instanceof FileVersion)) { $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO); $l->write('fileImportError = '.FileImporter::getErrorMessage($newFileVersion)); $l->close();
Viewing 15 lines of 21 lines. View entire code block.
I can excute the code that you give me. But I have this log message.
27 oct. 2016 21:31:11 Courriels envoyés belhmidi fileImportError = Fichier invalide.
Fichier invalide = invalide file
thank you.
27 oct. 2016 21:31:11 Courriels envoyés belhmidi fileImportError = Fichier invalide.
Fichier invalide = invalide file
thank you.
I'm happy, great work. Now I can see my photos.
just I add this line in your code:
thank you very much for your assistance :-)
just I add this line in your code:
... @file_put_contents($filePathImg.$imageName, $image); $fi = new FileImporter(); ...
thank you very much for your assistance :-)
You know I have my photos in File manager but in the attributes of the page, the image field doesn't receive the image value.
What type of attribute is the image_handler? Assuming it's an image type you would need to change your setAttribute line to this:
Unfortunately if the images already all imported if you run this again they will all import a second time.
$newPage->setAttribute('image_handler',$newFileVersion->getFileID());
Unfortunately if the images already all imported if you run this again they will all import a second time.
When I debug the code, I concluded the system can not be executed because I have like message no such file for $newFileVersion.
this line can import the image file
but this code has not been executed
this line can import the image file
@file_put_contents($filePathImg.$pfl->COL[0]->DATA.".jpg", $image);
but this code has not been executed
$fi = new FileImporter(); $newFileVersion = $fi->import($img); if (!($newFileVersion instanceof FileVersion)) { $l = new GroupLogger(LOG_TYPE_EMAILS, Logger::INFO); $l->write('fileImportError = '.FileImporter::getErrorMessage($newFileVersion)); $l->close(); } else { $fs = FileSet::getByName('portfolio'); $fs->addFileToSet($newFileVersion); $newPage->setAttribute('image_handler',$newFileVersion->getFileID()); }
If you are seeing them in the File Manager that code is getting executed, because the line $newFileVersion = $fi->import($img); is what adds it to the File Manager.
You are right the $newFileVersion = $fi> import ($ img); added the image in the file manager.
And in the report/log I have this message :
fileImportError = The uploaded file is too large. The present value of upload_max_filesize is 30M
Even so my size file is 40kb.
And in the report/log I have this message :
fileImportError = The uploaded file is too large. The present value of upload_max_filesize is 30M
Even so my size file is 40kb.
It's very possible that this @file_put_contents($filePathImg.$attraction->COL[0]->DATA.".jpg", $image); is creating a much bigger file than you think, are you able to check your server and see what the size of the image that is creating is?
I deleted it but always I have the same message.
fileImportError = The uploaded file is too large. The present value of upload_max_filesize is 30M.
fileImportError = The uploaded file is too large. The present value of upload_max_filesize is 30M.
for your information, I use Version - 5.7.5.6
And if your $fs is empty you're not getting a fileSet, might be easier to get it by using $fs = FileSet::getByName('Fileset Name');