I am facing this weird problem here with file upload. So when I upload the file everything goes fine. But when I don't select anyfile in upload and submit the form I get following error:
Notice (8): Array to string conversion [CORE\cake\libs\model\datasources\dbo_source.php, line 749]
Warning (512): SQL Error: 1054: Unknown column 'Array' in 'field list' [CORE\cake\libs\model\datasources\dbo_source.php, line 684]
Query: INSERT INTO `masters` (`candidate_id`, `candidate_name`, `candidate_file_name`) VALUES ('heman', 'He Man', Array)
Now here is what I am doing: in my controller:
The function which makes call to file upload function:
if ( $_FILES['data']['name']['Master'])
{
$this->upload_files();
}
Here is the file upload function:
function upload_files() {
$fcnt = 0;
$vdata = $_FILES['data']['name']['Master'];
echo "Vdata:";
pr($vdata);
foreach ( $vdata as $v )
{
$pro_files[] = array(
'name'=>$_FILES['data']['name']['Master']['candidate_file_name'],
'tmp'=>$_FILES['data']['tmp_name']['Master']['candidate_file_name'],
'type'=>$_FILES['data']['type']['Master']['candidate_file_name'],
);
echo "Pro_files:";
pr($pro_files);
$fcnt++;
}
$cnt = 0;
$allowed = array(
'xls','xlsx','db','mbd',
'txt','pdf','doc','docx',
'log','rtf','wpd','wps',
'csv','dat','pps','ppt',
'pptx','sdf','xml','bmp',
'gif','jpg','png','psd',
'thm','tif','svg','sql',
'html','htm','js','php',
'bin','mim','7z','deb',
'gz','pkg','sit',
'sitx','tar.gz','zip',
'zipx','pdf'
);
foreach ( $pro_files as $file )
{
$fileType = $file['type'];
$fileType2 = explode('/',$fileType);
$fileName = $file['name'];
$path = 'C:\wamp\www' . '\\' . $fileName;
//if ( in_array($fileType2[1],$allowed))
//{
echo "fileName";
pr($fileName);
if ( move_uploaded_file($file["tmp"], $path) )
{
$this->data['Master']['candidate_file_name'] = $fileName;
}
//}
$cnt++;
}
}
Here is how my debug data looks like:
Array
(
[Master] => Array
(
[candidate_id] => heman
[candidate_name] => He Man
[candidate_file_name] => Array
(
[name] => test.txt
[type] => text/plain
[tmp_name] => C:\wamp\tmp\php329D.tmp
[error] => 0
[size] => 0
)
)
)
Here is how vdata looks like:
Array
(
[candidate_file_name] =>
)
here is how profiles looks like
Array
(
[0] => Array
(
[name] =>
[tmp] =>
[type] =>
)
)
filename comes out to be nothing.
here is how the pr($_FILES) looks like:
Array
(
[data] => Array
(
[name] => Array
(
[Master] => Array
(
[candidate_file_name] =>
)
)
[type] => Array
(
[Master] => Array
(
[candidate_file_name] =>
)
)
[tmp_name] => Array
(
[Master] => Array
(
[candidate_file_name] =>
)
)
[error] => Array
(
[Master] => Array
(
[candidate_file_name] => 4
)
)
[size] => Array
(
[Master] => Array
(
[candidate_file_name] => 0
)
)
)
)
I am not able to figure out what the problem might be. Can anyone help? Your help is much appreciated. Thanks,
You are setting the name of the column to the form file name. But because the form field will come back as an array, the database doesn't know what to do with it. This is what you will need to do once you save the file into the location you put it:
$this->request->data['Master']['candidate_file_name'] = '/path/to/where/you/saved/the/file.ext';
If the form field was empty, then you just need to do:
unset($this->request->data['Master']['candidate_file_name']);
UPDATE
The best way to check this is:
if (empty($this->request->data['Master']['candidate_file_name']['name'])) {
unset($this->request->data['Master']['candidate_file_name']);
}