ShoeLace1291 Posted July 28, 2011 Share Posted July 28, 2011 I have a script that loops through an array to upload files with CodeIgniter. I use a foreach loop to avoid having to write the code out twice. In CI, I get an error message saying that the variable "o"(letter o) is undefined. The only thing that I can assume is that it's from the first letter o in the first array value called "original_file". I don't know what's causing the script to name the variable "o". Here's my code: $files = array('original_file', 'step_screenshot'); $attachment_errors = array(); $upload_errors = array(); foreach($files as $file){ $this->upload->initialize($config); if($this->upload->do_upload($file)){ $$file = $this->upload->data(); $attachment_info = array( 'file_name' => $$file['file_name'], 'file_width' => $$file['file_width'], 'file_height' => $$file['file_height'], 'file_size' => $$file['file_size'], 'author_id' => $member['id'] ); if($this->attachment->create($attachment_info)){ $$file['insert_id'] = $this->attachment->insert_id; } else { $attachment_errors[] = 'The file '.$file.' did not attach properly.'; } } else { $upload_errors[] = 'The file '.$file.' did not upload properly.'; } } The error only occurs on lines where i call the "$$file" variable. Quote Link to comment https://forums.phpfreaks.com/topic/243084-whats-wrong-with-my-variables/ Share on other sites More sharing options...
Muddy_Funster Posted July 28, 2011 Share Posted July 28, 2011 I have never come across a variable prefix using $$ - where did you get this idea from? Quote Link to comment https://forums.phpfreaks.com/topic/243084-whats-wrong-with-my-variables/#findComment-1248414 Share on other sites More sharing options...
ShoeLace1291 Posted July 28, 2011 Author Share Posted July 28, 2011 http://www.php.net/manual/en/language.variables.variable.php They're called variable-variables. I think the problem has more to do with how i'm iterating through my array. Quote Link to comment https://forums.phpfreaks.com/topic/243084-whats-wrong-with-my-variables/#findComment-1248415 Share on other sites More sharing options...
kikesv Posted July 28, 2011 Share Posted July 28, 2011 first of all sorry about my English, The problem will be structure of $$file, try to print th content : $$file = $this->upload->data(); print_r($$file); $attachment_info = array( 'file_name' => $$file['file_name'], 'file_width' => $$file['file_width'], 'file_height' => $$file['file_height'], 'file_size' => $$file['file_size'], 'author_id' => $member['id'] ); if($this->attachment->create($attachment_info)){ // IF $$file it's not array, you can do this. $$file['insert_id'] = $this->attachment->insert_id; } Quote Link to comment https://forums.phpfreaks.com/topic/243084-whats-wrong-with-my-variables/#findComment-1248418 Share on other sites More sharing options...
Muddy_Funster Posted July 28, 2011 Share Posted July 28, 2011 nice, I never knew about those So, having established my ignorance please indulge me a minute - you are assigning a variable variable array variable to $$file (which is attached to $file wich is declared as the contents of $this->upload->initialize($config); which may or may not also be an array? is that right or am I getting the totaly wrong end of the stick? Quote Link to comment https://forums.phpfreaks.com/topic/243084-whats-wrong-with-my-variables/#findComment-1248423 Share on other sites More sharing options...
ShoeLace1291 Posted July 28, 2011 Author Share Posted July 28, 2011 Yeah, you're right lol. Quote Link to comment https://forums.phpfreaks.com/topic/243084-whats-wrong-with-my-variables/#findComment-1248425 Share on other sites More sharing options...
Muddy_Funster Posted July 28, 2011 Share Posted July 28, 2011 OK, glad we got that cleaerd up From what I have read, I think you might want to change the $$file to ${$file} to evidence that it is an array. Quote Link to comment https://forums.phpfreaks.com/topic/243084-whats-wrong-with-my-variables/#findComment-1248430 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.