tags:

views:

323

answers:

1

Hi,

i am getting the error as

  Array to string conversion [CORE/cake/libs/file.php, line 96]
      $path = array(
"name" => "23_50_11[1].gif",
"type" => "image/gif",
"tmp_name" => "/tmp/phpbBWxAT",
"error" => 0,
"size" => 25230
  )

 $create    = false
 $mode  = 493

     dirname - [internal], line ??
  File::__construct() - CORE/cake/libs/file.php, line 96
    FormsController::add() - APP/controllers/forms_controller.php, line 528
   Object::dispatchMethod() - CORE/cake/libs/object.php, line 117
     Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 245
  Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 211
  require - APP/webroot/index.php, line 88
 [main] - CORE/index.php, line 61

Notice (8): Array to string conversion [CORE/cake/libs/file.php, line 97]


   $path    = array(
"name" => "23_50_11[1].gif",
"type" => "image/gif",
"tmp_name" => "/tmp/phpbBWxAT",
"error" => 0,
"size" => 25230
   )
$create = false
  $mode = 493

 is_dir - [internal], line ??
 File::__construct() - CORE/cake/libs/file.php, line 97
 FormsController::add() - APP/controllers/forms_controller.php, line 528
 Object::dispatchMethod() - CORE/cake/libs/object.php, line 117
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 245
 Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 211
 require - APP/webroot/index.php, line 88
  [main] - CORE/index.php, line 61

Warning (2): basename() expects parameter 1 to be string, array given [CORE/cake/libs/file.php, line 98]

The code that i have used is,

 <h1>Form Fill</h1>
 <?=$form->create('Form',array('type'=>'file'));?>
  <?=$form->input('date',array('label'=>'Publication Date '));?>
  <?=$form->input('headline');?>
 <?=$form->input('content');?>
 <?=$form->input('image',array('type'=>'file'));?>
 <?=$form->end('Submit');?>

in the Formscontroller function add($formid) { echo "Imaage ".$this->data['Form']['image'];// shows me the Imaage Array

      if ($this->data['Form']['image']) {

   $file = new File($this->data['Form']['image']);// this is not working 

   //$ext = $file->ext();

   //echo "Extension ".$ext;
   echo "Fiel ".$file;


   $date = $this->data['Form']['date'];
   $filename = $date['year'].'-'.$date['month'].'-'.$date['day'].'-form-image.'.'gif';

   $data = $file->read();

   echo "Data ".$data;
   $file->close();

   $file = new File(WWW_ROOT.'/img/'.$filename,true);
   $file->write($data);
   $file->close();
   }


     }

Instance for File is not working?? Please help me . actually i m trying to upload the image FIle and i m using Ubuntu linux machine ..

+2  A: 

To recap:

$this->data['Form']['image']; // shows me the Imaage Array

$file = new File($this->data['Form']['image']); // this is not working

Error: Array to string conversion

The problem:

new File() expects a string as argument, but you're giving it an array.

Solution:

Supply a string as argument for new File(), preferably the path of the file:

$file = new File($this->data['Form']['image']['tmp_name']);


To be a little more detailed and help you fix these kinds of problems yourself next time (hopefully), it's not actually File that expects a string argument. If you look at the trace again:

dirname - [internal], line ??
File::__construct() - CORE/cake/libs/file.php, line 96
FormsController::add() - APP/controllers/forms_controller.php, line 528
...

This says the problem actually occurred in dirname, which is a PHP internal function. It was called on line 96 in File::__construct, which in turn was called from FormsController::add etc. pp.

Since PHP is a dynamic language it doesn't usually crap out completely when you supply arguments of the wrong type. Rather it'll try to make the best out of it and actually try to convert an array to a string, and just notify you that it did so with Notice (8): Array to string conversion. Since the result of an Array -> String conversion is literally the string "Array", your program will usually crap out later, since now you're trying to do things like is_dir("Array"), which is nonsense.

As you can see the program actually struggles on, with quite a few notices, and basename() on line 98 even complaining a little harder with a Warning:

94: function __construct($path, $create = false, $mode = 0755) {
95:    parent::__construct();
96:    $this->Folder =& new Folder(dirname($path), $create, $mode);
97:    if (!is_dir($path)) {
98:        $this->name = basename($path);

Warning (2): basename() expects parameter 1 to be string, array given [CORE/cake/libs/file.php, line 98]

Now, it would be good style for File to check if the parameter actually is a string or not and fail gracefully, but it doesn't bother. Because as a developer you should read the documentation to see what the function expects. If it's not on the website, look directly in the file /cake/libs/file.php:

/**
 * Constructor
 *
 * @param string $path Path to file
 * @param boolean $create Create file if it does not exist (if true)
 * @param integer $mode Mode to apply to the folder holding the file
 * @access private
 */
 function __construct($path, $create = false, $mode = 0755) {
     ...
deceze