Where to move a file after addUpload

Notice: This thread is very old.
albertosanchezm
Member | 12
+
0
-

Hello,

I've added a addUpload control to a form for uploading images. But after save it, the DB result is like a temp file. How can I move the file to its final destination?

I've tried with:

$values = $form->getValues();
$values->image->move('/www/content/upload');

But it does not work.

Thanks a lot

Oli
Member | 1215
+
0
-

If image is name of your addUpload() field, it should work. Does your folder /www/content/upload exists and has privilegies for writing?

You can also wrap this move method into isOk() unless you have. Just in case.

albertosanchezm
Member | 12
+
0
-

Thanks, you 're right. The problem was the variable name.

But now, I could save the value in the database, but just the path '/www/content/upload’ is in the field. How can I set a valid filename?

Also, I have another problem. I've added the following rule to the form addUpload component
->addRule(Form::IMAGE, ‘Thubnail must be JPEG, PNG or GIF’);

but when I run the page, an exception is raised:

http://snag.gy/JUYJC.jpg

Any idea what is this happening?

Thanks again.

Oli
Member | 1215
+
0
-
  1. I think, that addUpload() return something like name so you probably can call something like this:
$values->image->name;

But im not sure. If it is not work, use dump($values->image); and you'll see what actualy this value return.

  1. You are trying call Form without namespace. You have to use before class
use Nette\Application\UI\Form;

or you have to write Form with namespace

->addRule(Nette\Application\UI\Form::Image, ...);

of course the first solution is better ;-)

albertosanchezm
Member | 12
+
0
-

Thanks a lot. Both things works. But I have a last doubt. Finally, the move command was

$values->image->move(‘content/upload’.$values->image->name);

I have one final doubt, if you allow me. The point is that “/www/content/upload” is the complete web path from the root, and in this way is stored in the DB. Could it be better to set there just relative paths and add the rest of the path with a global var? How can I define a global var to be fetched both from the Presenter and the latte template?