0

I use CakePHP-Upload plugin, now need to use the upload without form, following this example: Programmatic File Retrieval without a Form

All my upload are stored in the associated model, called the Attachment. So when I save the article, at the same time save the images in Attachmen model.

Plugin documentation suggests something like this:

<?php
  $this->Article->set(array('file' => $image_path)); // ?????
  $this->Article->save();
?>

I have a larger collection of images on the server, previously uploaded via the Joomla CMS, now I have a migration of data to a custom CMS built on CakePHP framework. I need to take all these pictures and upload again, through this plugin.

How to properly use this plugin for my needs, I try to put the path to a local picture, but uploading is not working ???

EDIT

$image_path = WWW_ROOT . 'img' . DS . 'attachment' . DS . '59'.DS.'hpim4799.jpg';
debug($image_path);
'C:\xampp\htdocs\portal\webroot\img\attachment\59\hpim4799.jpg'

Do not save the record and not create new images. Note, uploading through HTML form works well, I use windows 7 and xampp server.

EDIT 2 / Solution

Image path must be valid url like 'http://localhost/portal/img/attachment/59/hpim4799.jpg'

 $image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';

Thank you.

Salines
  • 5,674
  • 3
  • 25
  • 50
  • You are saying that you need to upload all images again. Why? The upload-plugin stores uploads in a table. If you can find out what path it is, and what name, you can build some code to generate the database-rows, and copy the files manual? – Bob Jan 17 '15 at 16:41
  • No, I need to generate new thumbnails in different size from the original image that is already on the server. I have saved the file names in the database, the original images are located in one folder. The plan is to make a loop of all the record, find related images and generate new images that will be placed in folders by id number of records. – Salines Jan 17 '15 at 17:53
  • Ah okay... I will check its documentation, but can't promise I've the answer ;) – Bob Jan 17 '15 at 18:33
  • Does the file `C:\xampp\htdocs\portal\webroot\img\attachment\59\hpim4799.jpg` really exists. If the upload isn't working it should not exist? – Bob Jan 17 '15 at 19:53
  • Yes, there is a file. Yes, there is a file. Behavior only work with remote files. – Salines Jan 17 '15 at 20:28

2 Answers2

1

Can you tell me what the content of the variable $image_url is? The doucmentation says you can add a file via the form, or you can do it programmatically. If you do that, use the $image_url as path to the image. If you got an associated model like Attachment you shoud use:

<?php
  $this->Article->set(array('Attachment.file' => $image_path)); // path + file to file
  $this->Article->save();
?>
Bob
  • 873
  • 1
  • 8
  • 21
  • Before I put the question, I tried it that way. It seems to me that the plugin has a bug. – Salines Jan 17 '15 at 19:03
  • I doubt that... Are you sure that the variable `$image_url` is right? – Bob Jan 17 '15 at 19:05
  • Can you debug the `$image_url`, your programatically code, and the loading of the behavior (`$actsas`)? – Bob Jan 17 '15 at 19:06
0

After hours of searching for solutions and detailed inspection of the Behavior code I finally found it.

Because Behavior uses php FILTER_VALIDATE_URL Filter, path must be a valid URL. Here is an example:

<?php
  $image_path = Router::url('/', true) . 'img/attachment/59/hpim4799.jpg';
  // return http://localhost/portal/img/attachment/59/hpim4799.jpg

  $this->Article->Attachment->set(array('file' => $image_path)); 
  $this->Article->Attachment->save();
?>
Salines
  • 5,674
  • 3
  • 25
  • 50
  • Can you please tell me what this piece of code actually do? I did not found any explanation in the documentation. Can i pass any valid image-url as $image_path? for example a google image .jpg-link? Or has the image data to be located on the same server local? – q0re Jan 26 '15 at 13:03
  • Simple upload any remote file. – Salines Jan 26 '15 at 15:22