I'm currently developing a form to upload a file to the server I'm working on. I can successfully upload a file to one of my app's folder without issue. What I want to do next is to read the file that was uploaded and add its info to a data base. To do this I'm using CakePHP
's file API
, but I can't get the constructor to work. Here is my code
<?php
App::uses('Component', 'Controller');
App::uses( 'String', 'Utility');
App::uses( 'File', 'Utility');
class UploadComponent extends Component{
public $max_files = 1;
public $filePath;
public function upload($data = null){
if( !empty( $data ) ){
if(count ($data) > $this->max_files){
throw new NotFoundException("Error procesando el pedido, el número máximo de archivos aceptados es {$this->max_files}", 1);
}
foreach($data AS $file){
$filename = $file['name'];
$file_tmp_name = $file['tmp_name'];
$dir = WWW_ROOT.'files'.DS.'uploads';
$allowed = array('txt');
if( !in_array( substr( strrchr( $filename, '.'), 1), $allowed)){
throw new NotFoundException("Error procesando el pedido", 1);
}elseif(is_uploaded_file( $file_tmp_name)){
move_uploaded_file($file_tmp_name, $dir.DS.String::uuid().$filename);
$filePath = $dir.DS.String::uuid().$filename;
echo gettype($filePath);
$import = new File($filePath); //this line throws an error
}
}
}
}
}
I'm confused as the API
says that it receives a path in form of a string
, which $filePath
is, but it is saying that it's receiving an array. Any idea on how to make it work?
These are the errors the framework shows:
dirname() expects parameter 1 to be string, array given [CORE\Cake\Utility\File.php, line 87]
is_dir() expects parameter 1 to be a valid path, array given [CORE\Cake\Utility\File.php, line 88]
basename() expects parameter 1 to be string, array given [CORE\Cake\Utility\File.php, line 89]
Thanks in advance.