Jump to content

Uploadable files/ pdf attachment in csv import


Recommended Posts

Hi PrestaShop forum

 

We have got a new PrestaShop 1.6 and would like to use the csv import function. We managed to import the products with pictures and text and we also set the uploadable files to "1", but how do we attach a data sheet in pdf file as the uploadable file?

 

With the url it is "http://localhost/prestashop/img/x/x/x/x.jpg", but I can't find any examples with a pdf data sheet. 

 

Does anyone know about this?

 

Best regards

Morten, Oxford IT

Link to comment
Share on other sites

  • 1 month later...
  • 2 weeks later...

I had problems trying to attach a size chat, first as a .pdf file and then as a hyperlink, but both unsuccessful. On reading up about csv files it seems they only do straight text, but I'm happy to be corrected if anyone can work out a way of doing it.

 

Kaye

Link to comment
Share on other sites

  • 2 weeks later...

Hi all,

 

I suggest you take a look at this documentation to get guidance and examples of what you are looking for:

 

http://www.prestashopmanager.com/useful-articles/about-prestashop/get-the-sample-of-prestashop-import-csv-file/

 

http://www.prestashopmanager.com/useful-articles/about-prestashop/supported-formats/

 

Hope this helps. 

 

Cheers!

 

Maggie

Link to comment
Share on other sites

  • 3 months later...
  • 4 weeks later...

Hi PrestaShop forum

 

We have got a new PrestaShop 1.6 and would like to use the csv import function. We managed to import the products with pictures and text and we also set the uploadable files to "1", but how do we attach a data sheet in pdf file as the uploadable file?

Hello,

 

The "uploadable file" column in csv file is not to attach a file but to allow visitor to upload a file in front-end.

 

I had to import a hundred of files with pdf attached and I managed to do it with a few changes:

 

1/ First, back up your files ad database, to avoid to delete attached files by mistake.

 

2/ in classes/attachment.php

at the end of file, just before the very last }, add this:

	public function addAttachment($autodate = true, $nullValues = false)
	{
	
		$return = parent::add($autodate, true);
		return $return;
	}
	
	public static function addAttachmentImport($filename, $name, $description)
	{
			$attachment = new Attachment();
			
			$languages = Language::getLanguages();
			foreach ($languages as $language)
			$attachment->name[$language['id_lang']] = strval($name);
			$attachment->description[$language['id_lang']] = $description;

			$attachment->file = sha1($filename);
			$attachment->file_name = $filename;
			
			$path_file = _PS_DOWNLOAD_DIR_.$filename;
			$attachment->file_size = filesize($path_file);

			$finfo = finfo_open(FILEINFO_MIME_TYPE);
			$attachment->mime = finfo_file($finfo, $path_file); 

			$attachment->addAttachment();

			return (int)$attachment->id;		
	}
	
	
	public static function addAttchmentProductImport($id_product, $id_attachment)
	{
		return Db::getInstance()->execute('
			INSERT INTO `'._DB_PREFIX_.'product_attachment` (`id_attachment`, `id_product`)
			VALUES ('.(int)$id_attachment.', '.(int)$id_product.')
		');
	}

2/ in controller/admin/AdminImportController.php

search for

case $this->entities[$this->l('Products')]:

below, in the available_fields filst,

search for

'delete_existing_images'=> array('label' => $this->l('Delete existing images (0 = No, 1 = Yes)')),

just below, add this

'delete_existing_attachments' => array('label' => $this->l('Delete existing attachments (0 = No, 1 = Yes)')),
'attachment' => array('label' => $this->l('attachment')),

Then in the same file, search for

Feature::cleanPositions();

just below, add this

				// Attachment files import
				
				if (isset($product->delete_existing_attachments))
					if ((bool)$product->delete_existing_attachments)
				
				if (isset($attachments['attachment']) && !empty($attachments['attachment']))
					
						$attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
						$attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
						$attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';

						if(!empty($attachment_filename))
						{
							$id_attachment = (int)Attachment::addAttachmentImport($attachment_filename, $attachment_name, $attachment_description);

							Attachment::addAttchmentProductImport($product->id, $id_attachment);
						}
					
					}

3/ Place your attached files in folder /download

Remember filenames must not contain spaces.

 

4/ In your csv file

You can add several attached files per product.

For each file, you must specify the filename, then you can specify a name and a description. Name and description are optional.

To separate filename, name and description, use "|" as a separator.

 

Add those two columns:

- Attachment (filename|name|description)

- Delete existing attachment (0 = No, 1 = Yes)

 

Example for column "Attachment" (here your separator value is %):

filename1.pdf|Name of my first pdf|Description of my first pdf%filename2.pdf%filename3.pdf

 

I hope it works for you!

post-949465-0-20344700-1425600986_thumb.png

  • Thanks 1
Link to comment
Share on other sites

  • 3 months later...

Hello vlester,

 

I tried your solution but i thins there are some problems.

 

In this script where did you set variables $attachements or $tab_attachment?

 

Maybe there should be a for each..

// Attachment files import
				
if (isset($product->delete_existing_attachments))
	if ((bool)$product->delete_existing_attachments)
		if (isset($attachments['attachment']) && !empty($attachments['attachment']))
			$attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
			$attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
			$attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';

			if(!empty($attachment_filename))
			{
				$id_attachment = (int)Attachment::addAttachmentImport($attachment_filename, $attachment_name, $attachment_description);
				Attachment::addAttchmentProductImport($product->id, $id_attachment);
			}
		}

Could you help me?

 

Thankyou

Link to comment
Share on other sites

Yes, sorry here the whole thing:

                // Attachment files import
                
                //delete existing attachments if "delete_existing_attachments" is set to 1
                if (isset($product->delete_existing_attachments))
                    if ((bool)$product->delete_existing_attachments)
                        $product->deleteAttachments();
                
                
                $attachments = get_object_vars($product);

                if (isset($attachments['attachment']) && !empty($attachments['attachment']))
                    foreach (explode($this->multiple_value_separator, $attachments['attachment']) as $single_attachment)
                    {
                    
                        $tab_attachment = explode('|', $single_attachment);
                        $attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
                        $attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
                        $attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';

                        if(!empty($attachment_filename))
                        {
                            $id_attachment = (int)Attachment::addAttachmentImport($attachment_filename, $attachment_name, $attachment_description);

                            Attachment::addAttchmentProductImport($product->id, $id_attachment);
                            Product::updateCacheAttachment($product->id);
                        }
                    
                    }



Edited by vlester (see edit history)
Link to comment
Share on other sites

  • 1 month later...

Hello Vlester,


 


I tried your solution but it does not work for me.


Can you please upload the 2 files AdminImportController.php and attachment.php.


I'm working on prestashop 1.6.0.14 I made all the changes that you posted above but when I'm importing the csv nothing happens in the attachment tab (not even in the database for ps_attachement)


I don't have controller/admin/AdminImportController.php  i have controllers/admin/AdminImportController.php


 


Can you help me?


Link to comment
Share on other sites

  • 2 weeks later...

 

Yes, sorry here the whole thing:

                // Attachment files import
                
                //delete existing attachments if "delete_existing_attachments" is set to 1
                if (isset($product->delete_existing_attachments))
                    if ((bool)$product->delete_existing_attachments)
                        $product->deleteAttachments();
                
                
                $attachments = get_object_vars($product);

                if (isset($attachments['attachment']) && !empty($attachments['attachment']))
                    foreach (explode($this->multiple_value_separator, $attachments['attachment']) as $single_attachment)
                    {
                    
                        $tab_attachment = explode('|', $single_attachment);
                        $attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
                        $attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
                        $attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';

                        if(!empty($attachment_filename))
                        {
                            $id_attachment = (int)Attachment::addAttachmentImport($attachment_filename, $attachment_name, $attachment_description);

                            Attachment::addAttchmentProductImport($product->id, $id_attachment);
                            Product::updateCacheAttachment($product->id);
                        }
                    
                    }

Hello Vlester,

 

I tried your solution but it does not work for me.

Can you please upload the 2 files AdminImportController.php and attachment.php.

I'm working on prestashop 1.6.0.14 I made all the changes that you posted above but when I'm importing the csv nothing happens in the attachment tab (not even in the database for ps_attachement)

I don't have controller/admin/AdminImportController.php  i have controllers/admin/AdminImportController.php

 

Can you help me?

 

Link to comment
Share on other sites

Hello,

I can't post my import file cause I changed it a lot for other reasons so it won't be a big help for you.

 

Where are your pdf files? The cell in the "attachment" column must contain the absolute path.

 

If it's too boring to add the path in each cell, you can add it automatically in php file, like this:

Below this line

$attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';

add

$absolute_path_to_pdf_repertory = "absolute/path/to/your/pdf_folder/";
$attachment_filename = $absolute_path_to_pdf_repertory.$attachment_filename;

I hope this helps.

Edited by vlester (see edit history)
Link to comment
Share on other sites

  • 2 months later...

 

Hi Valorosu, did you get your attachment upload working? I'm having problems with Prestashop 1.6.1.1 and the above code. Import seems to run fine but no attachment of the pdf-file with the product and nothing happens in DB ps_attachment, just like you said. I would really like to get this working.

 

Link to comment
Share on other sites

Hi Vlester,

 

Are you using Prestashop 1.6.1.1 at the moment and do you know if your code is still working? I can't seem to get the attachment upload to work. I have checked the code I copied from above multiple times and can't find any errors. Besides that I have tried different paths to the attachments in my csv-file. When I place an absolute url there I see from an error in my server logs that the absolute path is added twice (so once from me in the CSV and once in the code you provided). So I assume that if I put the file in the folder /download and just put in the filename in the CSV it should work fine. But not so....

 

I really hope you can help me out.

Link to comment
Share on other sites

Hello,

I made some change to work with the last Prestashop Version.

 

So in the AdminImportController.php, add those two lines in the  fields list for products import (case $this->entities[$this->l('Products')])

'delete_existing_attachments' => array('label' => $this->l('Delete existing attachments (0 = No, 1 = Yes)')),
'attachment' => array('label' => $this->l('attachment')),

Then at the end of the function productImport(), after those lines:

else {
                        StockAvailable::setQuantity((int)$product->id, 0, (int)$product->quantity, (int)$this->context->shop->id);
                    }
                };

add this:

 // Attachment files import
                
        //delete existing attachments if "delete_existing_attachments" is set to 1
                if (isset($product->delete_existing_attachments))
                    if ((bool)$product->delete_existing_attachments)
                        $product->deleteAttachments();
                
                
                $attachments = get_object_vars($product);

                if (isset($attachments['attachment']) && !empty($attachments['attachment']))
                    foreach (explode($this->multiple_value_separator, $attachments['attachment']) as $single_attachment)
                    {
                    
                        $tab_attachment = explode('|', $single_attachment);
                        $attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
                        $attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
                        $attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';

                        if(!empty($attachment_filename))
                        {
                            $id_attachment = (int)Attachment::addAttachmentImport($attachment_filename, $attachment_name, $attachment_description);

                            Attachment::addAttchmentProductImport($product->id, $id_attachment);
                            Product::updateCacheAttachment($product->id);
                        }
                    
                    }



In the attachment class file (classes/Attachment.php)

add at the end those two functions:

	    public static function addAttachmentImport($filename, $name, $description)
    {
            $attachment = new Attachment();
            
            $languages = Language::getLanguages();
            foreach ($languages as $language)
            $attachment->name[$language['id_lang']] = strval($name);
            $attachment->description[$language['id_lang']] = $description;

            $attachment->file = sha1($filename);
            $attachment->file_name = $filename;
            
            $path_file = _PS_DOWNLOAD_DIR_.$filename;
            $attachment->file_size = filesize($path_file);

            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $attachment->mime = finfo_file($finfo, $path_file);

            $attachment->addAttachment();

            return (int)$attachment->id;        
    }
    
    
    public static function addAttchmentProductImport($id_product, $id_attachment)
    {
        return Db::getInstance()->execute('
            INSERT INTO `'._DB_PREFIX_.'product_attachment` (`id_attachment`, `id_product`)
            VALUES ('.(int)$id_attachment.', '.(int)$id_product.')
        ');

    }

Now don't forget to delete the cache file: /cache/class_index.php

 

You must put your files in the /Download folder.

 

In your cvs file, the "attachment" column could contain this:
myfile1.doc|Name of my file

 

Or if you want to attach several files to one product:

myfile1.doc|Name of my file%myfile2.pdf| Name of my file

 

 

 

My code is not pretty but it works, I've just tested it.

 

Good luck

post-949465-0-01383000-1443631340_thumb.png

post-949465-0-60033500-1443631341_thumb.png

Edited by vlester (see edit history)
Link to comment
Share on other sites

Hi Vlester, wow you saved my day! So happy that it works now. For other people who are reading up I have just a minor correction to the code you provided. In the function below in the line for $path_file I crossed out the $filename and replaced it with $uniqid.

 

 

public static function addAttachmentImport($uniqid, $name, $description)
    {
            $attachment = new Attachment();
            
            $languages
= Language::getLanguages();
            foreach ($languages as $language)
            $attachment->name[$language['id_lang']] = strval($name);
            $attachment->description[$language['id_lang']] = $description;

            $attachment->file = $uniqid;
            $attachment->file_name = $uniqid;
            
            $path_file
= _PS_DOWNLOAD_DIR_.$filename$uniqid;
            $attachment->file_size = filesize($path_file);


            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $attachment->mime = finfo_file($finfo, $path_file);

            $attachment->addAttachment();

            return (int)$attachment->id;        
    
}

Link to comment
Share on other sites

  • 2 months later...

Hi,

 

Your solution is exactly what were looking for, i have implemented it this morning, everything was going well unit i checked the Front Office.

 

The downloads are on the product page (Bravo!) and have the correct title. (Bravo!) When you click to download it downloads a file that is something like:

 

85abc36bd15f91d5330e5caab4025d52c03075a4 (No Bravo!)

 

Which, if you are like me it is OK as i opened it with adobe acrobat which opens the correct PDF file. However, i'm not sure this is great for my customers. 

 

The problem is the following:

 

a) the downloaded file is not titled anything relevant, such as what the uploaded file was called. 

 

b ) there is no file extension so they cant open it unless they somehow realize they have to rename the file with .pdf at the end. 

 

any help would be appreciated.

 

Thanks

Edited by metalines (see edit history)
Link to comment
Share on other sites

I must have got something wrong here:

public function addAttachment($autodate = true, $nullValues = false)
    {
    
        $return = parent::add($autodate, true);
        return $return;
    }
    public static function addAttachmentImport($filename, $name, $description)
    {
            $attachment = new Attachment();
            
            $languages = Language::getLanguages();
            foreach ($languages as $language)
            $attachment->name[$language['id_lang']] = strval($name);
            $attachment->description[$language['id_lang']] = $description;

            $attachment->file = $filename;
            $attachment->file_name = $filename;
            
            $path_file = _PS_DOWNLOAD_DIR_.$filename;
            $attachment->file_size = filesize($path_file);


            $finfo = finfo_open(FILEINFO_MIME_TYPE);
            $attachment->mime = finfo_file($finfo, $path_file);

            $attachment->addAttachment();

            return (int)$attachment->id;        
    }

    
    
    public static function addAttchmentProductImport($id_product, $id_attachment)
    {
        return Db::getInstance()->execute('
            INSERT INTO `'._DB_PREFIX_.'product_attachment` (`id_attachment`, `id_product`)
            VALUES ('.(int)$id_attachment.', '.(int)$id_product.')
        ');
    }
// Import Attached files
                
                //delete existing attachments if "delete_existing_attachments" is set to 1
                if (isset($product->delete_existing_attachments))
                    if ((bool)$product->delete_existing_attachments)
                        $product->deleteAttachments();
                
                $attachments = get_object_vars($product);

                if (isset($attachments['attachment']) && !empty($attachments['attachment']))
                    foreach (explode($this->multiple_value_separator, $attachments['attachment']) as $single_attachment)
                    {
                    
                        $tab_attachment = explode('|', $single_attachment);
                        $attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
                        
                        // Create unique filename and copy original file in another file with this name
                        $filename = sha1(microtime());
                        $old_path=_PS_DOWNLOAD_DIR_.$attachment_filename;
                        $new_path=_PS_DOWNLOAD_DIR_.$filename;
                        if (!copy($old_path, $new_path)) {
                            echo "Copy of ".$old_path."to".$new_path." failed.\n";
                        }
                      
                        $attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
                        $attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';

                        if(!empty($attachment_filename))
                        {

                        $id_attachment = (int)Attachment::addAttachmentImport($filename, $attachment_name, $attachment_description);

                        Attachment::addAttchmentProductImport($product->id, $id_attachment);
                        Product::updateCacheAttachment($product->id);
                            
                        }

                    }
Link to comment
Share on other sites

Hi,
 
I have read the post to upload attachments files in CSV.
I have tried to implement but it does not working with me.
 
I have this error : 
 
FastCGI: server "/usr/lib/cgi-bin/php5-fcgi" stderr: PHP message: PHP Parse error:  syntax error, unexpected '$uniqid' (T_VARIABLE) in /classes/Attachment.php on line 216
 
Maybe, someone can send me both files ? Attachments + AdminImportController ?
Prestashop version : 1.6.1.1
 
Many thanks!
Edited by comivi (see edit history)
Link to comment
Share on other sites

  • 1 month later...

Hi,

 

I did manage to get this working, eventually, due to my limited knowledge. It was a great success, but unfortunately it was short lived. HAHA!

 

Everything looked great, CSV imported with multiple attachments per product. Was fantastic news. So i imported the first stage in my product catalog, around 300 products.

 

At first it all looked perfect, until i noticed a product without its attachments. And then after investigating further there was a number of products that didn't have there attachments.

 

Naturally, I started by investigation whether i had the file names slightly wrong on the CSV or if the file was actually present in the downloads folder. As it turns out, there was nothing wrong here, all files present and file names matched accordingly. I then noticed that it had successfully imported for example product 1-9 perfect, 10 with no attachments, 11-13 perfect, 14 no attachment, 15-30 perfect, 31 no attachment etc. it goes on like this with seemingly no pattern. 

 

I have tried many different things to try and identify the problem but with no success, i cannot find the fault. Further to this, strangely , if you throw all the unsuccessful lines together in a CSV and import again they all work (with the odd line deciding not to work!) Further proving that there's nothing wrong with the CSV or the files but proving that the is an intermittent problem here. 

 

It like saying "if you import the same CSV over and over, but changing the order of products in the CSV, it is completely random which lines decide not to work!

 

Any thoughts, help, advice would be fantastic!

 

Thanks in advance!

 

Mike

Link to comment
Share on other sites

Hello,

 

 

I suggest you to first import all your data without the attachments column, but with a "product reference".

And then, import only "attachments" and "product reference" columns  and select  "use reference key" in import parameters.

You also can use id_product instead of product reference and then  select "force ids" in  import parameters.

Doing that might make your cvs file more digestible for Prestashop.

 

 

Hope that helps.

 

Vicky

Link to comment
Share on other sites

Is there a reason that, if you add an attachment through the back office the file name is a sha1 hash number but when i import attachments through CSV they have the actual file name and extension?

 

Also, if product 1 and product 2 have the exact same attachment, and i set the 'delete existing attachment' column to '1', is it possible that when the product 2 is imported it deletes the attachment on product 1?

 

There is also something else, i have noticed, if i navigate to attachment tab in BO, each attachment has only one assigned product.

 

Mike

Link to comment
Share on other sites

  • 1 month later...

Hello,

 

I finally succeed to modify prestashop to import attachments along with products in a csv, the problem now is that I get a huge list of attachments in the attachment list of the product attachment tab.

 

Now, before going on with other products, I need to fix this. How can import products just "adding" an attachment from an the existing attahcmentlist without creating a new one every time I import a new product?

 

I hope it's clear :)

 

Thank you very much

Link to comment
Share on other sites

Hi,

I am not sure I understand well. You're supposed to import products with their attachments in one step. if there is no attachment selected in the attachment tab of the product, it means the import didn't work correctly.

Link to comment
Share on other sites

Hi,

 

probably I'm not writing properly :) I imported succesfully hundreds of products with the same attachment (a datasheet), but looking in the product tab now I get hunderds of "available attachments", while I should have just one.

 

So, altough the modifiy explained in this post did the job (I have an attachment downloadable for each of the product), the right procedure to import massive quantity of products with attachment, should be to load the attachment one time (as done manually) and then add that attachment for each product from the same loaded one. 

 

Please check the images , Hope it's clear now.

 

Thank you very much

 

attachment_tab_screenshot.jpg

csv_screenshot.jpg

Link to comment
Share on other sites

OK, for anyone that had my same problem, I solved. Now I can import hundreds of products adding the same existing attachment or loading a new one for each.

 

In AdminImportCotroller.php:

...

'attachment' => array('label' => $this->l('Attachment (filename, name, description)')
), 
'attachment_id_nums' => array('label' => $this->l('Existing Attachment IDs')
),
...
if (isset($product->attachment) && !empty($product->attachment)) 
{
$tab_attachment =explode(',',$product->attachment);
$attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
$attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
$attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';
 
if(!empty($attachment_filename) && !empty($attachment_name))
{
Attachment::addAttachmentImport($attachment_filename, $attachment_name, $attachment_description, $product->id);
}                  
}
 
if (isset($product->attachment_id_nums) && !empty($product->attachment_id_nums))
{
$tab_attachment_id_nums =explode(',',$product->attachment_id_nums);
if (!Attachment::attachToProduct($product->id, $tab_attachment_id_nums))
$this->errors[] = Tools::displayError('An error occurred while saving product attachments.');
}
 
 
Peace
Link to comment
Share on other sites

  • 4 months later...
  • 2 months later...

Hi Silvioscavone,

 

Thanks for everyone who helped on this it has made everything so easy!

 

Silvioscavone, thanks for your improvement, I'm just implementing it in my site now and I would like to ask something about it...

 

My current csv looks like this:

 

field separator ;

multiple value separator @

attachment file name.pdf | attachment name

 

product-id;attachment-1.pdf|[email protected]|[email protected]|attachment-3;delete-existing-attachments;

 

With your improvement should it now look as follows:

 

product-id;attachment-1.pdf|[email protected]|[email protected]|attachment-3;attachment-id-1@attachment-id-2@attachment-id-3;delete-existing-attachments;

 

or

 

product-id;attachment-1.pdf|attachment-1;attachment-id-1;attachment-2.pdf|attachment-2;attachment-id-2;attachment-3.pdf|attachment-3;attachment-id-3;delete-existing-attachments;

 

I hope what I am asking has been asked clearly and thanks in advance!

 

Mike

Edited by metalines (see edit history)
Link to comment
Share on other sites

  • 1 month later...

Thanks for the great mod!

 

I can import attachments now, and it shows up correctly... however one problem, when I download, the file is actually a text file, with the following:

<br />
<b>Warning</b>:  filesize(): stat failed for /home/public_html/download/37e9d4939a884a82befa6645001c91f63035a48e in <b>/home/public_html/controllers/front/AttachmentController.php</b> on line <b>44</b><br />
<br />
<b>Warning</b>:  readfile(/home/public_html/download/37e9d4939a884a82befa6645001c91f63035a48e): failed to open stream: No such file or directory in <b>/home/public_html/controllers/front/AttachmentController.php</b> on line <b>47</b><br />

And I looked at the code (line 44 of the attachment controller):

 header('Content-Length: '.filesize(_PS_DOWNLOAD_DIR_.$a->file));

$a->file is the sha1 checksum, I think it should be $a->filename.  However I manually added an attachment, and it worked without changing line 44.

I also checked the database and did a var_dump($a), and the $a->file indeed is the checksum.  So why it works?

 

var_dump of working file (manually added attachment):

 ["file"]=>
  string(40) "f80c541a817aaec6db4d9c8460cc5be47054293d"
  ["file_name"]=>
  string(9) "model.pdf"
  ["file_size"]=>
  string(5) "41935"
  ["name"]=>
  string(4) "Test"
  ["mime"]=>
  string(15) "application/pdf"
  ["description"]=>
  string(4) "test"
  ["position"]=>.....
.....

and this is the var_dump of the bad attachment (imported):

 ["file"]=>
  string(40) "4a4aed26dea405fb231b7c98a75b290bf320e14b"
  ["file_name"]=>
  string(9) "product.pdf"
  ["file_size"]=>
  string(5) "172314"
  ["name"]=>
  string(4) "product.pdf"
  ["mime"]=>
  string(15) "application/pdf"
  ["description"]=>
  string(4) ""
  ["position"]=>...
...
 

This is the response header of the manually added attachment (works):

Connection:Keep-Alive
Content-Disposition:attachment; filename="model.pdf"
Content-Encoding:gzip
Content-Transfer-Encoding:binary
Content-Type:application/pdf
Date:Tue, 29 Nov 2016 10:51:25 GMT
Keep-Alive:timeout=1, max=100
Server:Apache
Transfer-Encoding:chunked
Vary:Accept-Encoding,User-Agent
X-Powered-By:PHP/5.6.28

And this is the not working one (imported):

Connection:Keep-Alive
Content-Disposition:attachment; filename="product.pdf"
Content-Encoding:gzip
Content-Length:236
Content-Transfer-Encoding:binary
Content-Type:application/pdf
Date:Tue, 29 Nov 2016 10:51:44 GMT
Keep-Alive:timeout=1, max=100
Server:Apache
Vary:Accept-Encoding,User-Agent
X-Powered-By:PHP/5.6.28

The content length should be 179.18k

Edited by brisk5181 (see edit history)
Link to comment
Share on other sites

Hi,

 

Thank you everyone, this thread has made everything work so well, but i need one last little bit of help  :) .

 

I have got this improvement now working for one attachment but for the life of me i cant get it to import multiple attachments.

 

I cant understand what this explode is for? Can anyone point me in the right direction here. As far as i can tell in the column following the attachments it should just be the attachment id numbers separated by the desired multiple value separator. So whats the explode for?

 

                    $tab_attachment_id_nums =explode('|',$product->attachment_id_nums);
                    if (!Attachment::attachToProduct($product->id, $tab_attachment_id_nums))
                    $this->errors[] = Tools::displayError('An error occurred while saving product attachments.');
                }
 
 
my sample csv: 1533;attachment-manual.pdf|attachment [email protected]|attachment-manual-1;835@836
 
When i import it throws up the following error:
 
 
[PrestaShopException]

Property Attachment->name length (39) must be between 0 and 32
at line 954 in file classes/ObjectModel.php

 

 

Link to comment
Share on other sites

  • 2 months later...

Hello, I am using version 1.6 of prestashop and I am doing a backoffice module where I want the user to insert more than a pdf or doc file, and to save it in configuration so that later I can fetch those files to send to the Client by email, I already have my file type field but it still does not save anywhere. Someone can help me? Thank you.

Hello, I am using version 1.6 of prestashop and I am doing a backoffice module where I want the user to insert more than a pdf or doc file, and to save it in configuration so that later I can fetch those files to send to the Client by email, I already have my file type field but it still does not save anywhere. Someone can help me? Thank you.

Link to comment
Share on other sites

  • 3 weeks later...

I'm using PS 1.6.1.1 and I can't make it working...

It gives a 500 error when trying to import the files, I believe the problem is in the Attachment.php class, but I'm not so expert to solve the problem.

Does someone has this small but very interesting module working in this version on PS?

If so, is it possible to have some advices?

Thank you.

Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...
  • 1 month later...

 

Yes, sorry here the whole thing:

                // Attachment files import
                
                //delete existing attachments if "delete_existing_attachments" is set to 1
                if (isset($product->delete_existing_attachments))
                    if ((bool)$product->delete_existing_attachments)
                        $product->deleteAttachments();
                
                
                $attachments = get_object_vars($product);

                if (isset($attachments['attachment']) && !empty($attachments['attachment']))
                    foreach (explode($this->multiple_value_separator, $attachments['attachment']) as $single_attachment)
                    {
                    
                        $tab_attachment = explode('|', $single_attachment);
                        $attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
                        $attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
                        $attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';

                        if(!empty($attachment_filename))
                        {
                            $id_attachment = (int)Attachment::addAttachmentImport($attachment_filename, $attachment_name, $attachment_description);

                            Attachment::addAttchmentProductImport($product->id, $id_attachment);
                            Product::updateCacheAttachment($product->id);
                        }
                    
                    }



Link to comment
Share on other sites

 

 

Yes, sorry here the whole thing:

                // Attachment files import
                
                //delete existing attachments if "delete_existing_attachments" is set to 1
                if (isset($product->delete_existing_attachments))
                    if ((bool)$product->delete_existing_attachments)
                        $product->deleteAttachments();
                
                
                $attachments = get_object_vars($product);

                if (isset($attachments['attachment']) && !empty($attachments['attachment']))
                    foreach (explode($this->multiple_value_separator, $attachments['attachment']) as $single_attachment)
                    {
                    
                        $tab_attachment = explode('|', $single_attachment);
                        $attachment_filename = isset($tab_attachment[0]) ? $tab_attachment[0] : '';
                        $attachment_name = isset($tab_attachment[1]) ? trim($tab_attachment[1]) : $attachment_filename ;
                        $attachment_description = isset($tab_attachment[2]) ? trim($tab_attachment[2]) : '';

                        if(!empty($attachment_filename))
                        {
                            $id_attachment = (int)Attachment::addAttachmentImport($attachment_filename, $attachment_name, $attachment_description);

                            Attachment::addAttchmentProductImport($product->id, $id_attachment);
                            Product::updateCacheAttachment($product->id);
                        }
                    
                    }



 

 

Can i upload multipe files for one product? i need for every product upload 6 files. what path on ftp i need to upload this code? Thanks for your help!

Edited by roolz (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...