1

In Propriete entity I have 5 field called image1 image2 image3 image4 image5

I wan't to add these fields in a for loop

I tried this but it doesn't work:

  for($i=0;$i<count($this->request->data['files'])&&$i<5;$i++){
               //... some code
                    $propriete->{'image'.$i+1} = $file['name'];
                }
            }

Can someone help me?

EDIT

This is the code of my loop:

for($i=0; $i<count($this->request->data['files']) && $i<5; $i++){
    $file=$this->request->data['files'][$i];
    $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
    $arr_ext = array('jpg', 'jpeg', 'gif'); //set allowed extensions
    if(in_array($ext, $arr_ext))
    {
        //do the actual uploading of the file. First arg is the tmp name, second arg is
        //where we are putting it
        move_uploaded_file($file['tmp_name'], WWW_ROOT . '/img/' . $file['name']);
        debug($file['name']);
        //prepare the filename for database entry
        $propriete->{'image'.$i+1} = $file['name'];
    }
}
5
  • $file['name'] makes no sense, please show enough code to explain where this variable comes from Commented Apr 11, 2016 at 10:10
  • @RiggsFolly check the edit Commented Apr 11, 2016 at 10:12
  • Can you specify in which line the error is seen? Is it $propriete->{'image'.$i+1} = $file['name']; Are you not getting the filename?Or cannot set the filename in the object property Commented Apr 11, 2016 at 10:14
  • So what is the actual problem but it doesn't work is not much help to us Commented Apr 11, 2016 at 10:16
  • The problem is $prorpete->image1 return null Commented Apr 11, 2016 at 10:17

2 Answers 2

2

Your code should work fine, you're just missing parenthesis which makes your concatenation go haywire

$propriete->{'image'.($i+1)}="test";

This can also be demonstrated by this simple test

$i=2;
echo 'image'.$i+1;  // 1 :)

VS

echo 'image'.($i+1);  //image3
Sign up to request clarification or add additional context in comments.

Comments

0

Put the value of every method in a variable to store the value and then use it.For eg

$values = $this->request->data['files'];

You cannot for loop the data without putting them in a variable first.

If you cannot find the filename the problem should be that you are naming the object property in the wrong manner.You should enclose $i+1 with quotes.

2 Comments

I did that i stor the data from the request in $file
This isn't the right solution but it isn't me who have downvote i don't have the score yet..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.