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;

$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.

 
hutman replied on at Permalink Reply
hutman
I believe import returns a number (the fileID) but addFileToSet should be passed an object, so you need to add another line in there to get the file object. Like this:

$newFileID = $fi->import($filePathImg.$imageName);
$newFile = File::getByID($newFileID);
$fs = FileSet::getByID(11);   
$fsf = $fs->addFileToSet($newFile);
$newPage->setAttribute('image_handler',$newFileID);


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');
belhmidi replied on at Permalink Reply
Always doesn't work.

I have always the problem in this line :

$fs = FileSet::getByID(11);

Even I changed
$fs = FileSet::getByName('Fileset Name');


What can I do?

:-(
hutman replied on at Permalink Reply
hutman
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.
belhmidi replied on at Permalink Reply
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

:-)
hutman replied on at Permalink Reply
hutman
I'm not exactly sure what you mean but the use statement at the top should be Concrete\Core\File\Set
belhmidi replied on at Permalink Reply
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 ?
hutman replied on at Permalink Reply
hutman
Ah yes, of course. It's Dashboard -> Files -> File Sets. You can add/edit/view all of your File Sets there.
belhmidi replied on at Permalink Reply 1 Attachment
That's good, I created one under name "portfolio". and I changed the line for this

$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
hutman replied on at Permalink Reply
hutman
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.
belhmidi replied on at Permalink Reply
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();
hutman replied on at Permalink Reply
hutman
I am 100% certain that this line ->$l->write('filePath = '.$fs); is causing your error.
belhmidi replied on at Permalink Reply 1 Attachment
hhhhhh Why you told this.

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();
.
hutman replied on at Permalink Reply
hutman
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.

@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?
belhmidi replied on at Permalink Reply
I try you code and I dont have error, thank you :-), it's very good but the file doesn't imported.
hutman replied on at Permalink Reply
hutman
The file doesn't show up in the File Manager in the Dashboard, or it doesn't show up in the File Set?
belhmidi replied on at Permalink Reply 1 Attachment
The file doesn't show up in the File Manager in the Dashboard, but I have anything in File Set
hutman replied on at Permalink Best Answer Reply
hutman
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();
belhmidi replied on at Permalink Reply
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.
belhmidi replied on at Permalink Reply
I'm happy, great work. Now I can see my photos.

just I add this line in your code:

...
@file_put_contents($filePathImg.$imageName, $image);
$fi = new FileImporter();
...


thank you very much for your assistance :-)
belhmidi replied on at Permalink Reply 1 Attachment
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.
hutman replied on at Permalink Reply
hutman
What type of attribute is the image_handler? Assuming it's an image type you would need to change your setAttribute line to this:

$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.
belhmidi replied on at Permalink Reply
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
@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());
  }
hutman replied on at Permalink Reply
hutman
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.
belhmidi replied on at Permalink Reply
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.
hutman replied on at Permalink Reply
hutman
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?
belhmidi replied on at Permalink Reply
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.
belhmidi replied on at Permalink Reply
for your information, I use Version - 5.7.5.6