audio upload in cakephp2.4 - cakephp

I want to upload audio files (type mp3,acc,wav) I am using cakephp 2.4.1 stable and php5 stable. I have tried mime_content_type,finfo_file to check if uploaded file is audio file with either mp3,acc or wav type. But I get this
`error mime_content_type(059.piya basanti re... [piya basanti][2000].mp3): failed to open stream: No such file or directory [APP/Controller/AdminController.php, line 86]`
My app dir and webroot dir are permited with 0777.
Here is my view.ctp code :
<?= $this->Form->create('Homepage',array('type'=>'file'));
echo $this->Form->input('audio_1',array('type'=>'file'));
echo $this->Form->submit('Submit');
echo $this->Form->end(); ?>
my Controller code
public function saveaudio() {
if($this->request->is('post')) {
$this->loadModel('Homepage');
//this is line 86// $file = mime_content_type($this->request->data['Homepage']['audio_1']['name']);
pr($file);exit;
}
and here is my data
Array
(
[Homepage] => Array
(
[audio_1] => Array
(
[name] => 059.piya basanti re... [piya basanti][2000].mp3
[type] =>
[tmp_name] =>
[error] => 1
[size] => 0
)
)
)
My question is :
How I can check type of file? First I want to check its mime type then I want to save it
Why I am not getting tmp_name for uploaded file?
Why I am getting error for mime_content_type?
Can anyone explain me? It would be helpfull to me

Look at the data, the upload was discarded as there's an error. 1 equals UPLOAD_ERR_INI_SIZE, and the cause for this is:
The uploaded file exceeds the upload_max_filesize directive in php.ini.
See http://php.net/manual/features.file-upload.errors.php
So you have to increase that value, and most probably also post_max_size, see http://php.net/manual/features.file-upload.common-pitfalls.php for more information.
And once the uploading works, you'll have to use the correct keys, it's audio_1, not audio_, also you'll then have to use tmp_name, ie this:
$this->request->data['Homepage']['audio_']['name']
should be this
$this->request->data['Homepage']['audio_1']['tmp_name']
On a side note, mime_content_type is deprecated in favour of the Fileinfo extension.

Related

CakePHP Email Sending 0 bytes file attachment

I need to attach a recent file to an email. The attachment is there, but it is 0 bytes. The path is correct
I have the following code, in part:
$file = WWW_ROOT . $handle->file_dst_pathname;
/* $file = /var/www/app/webroot/files/uploads/children/0000000220.pdf which is correct */
$attachment_name = 'Progress Note for ' . $parent['Child']['child_name'] . '-' . date('mdY') . '.' . $data['extension'];
$Email->attachments(array($attachment_name => $file));
I am using Cakephp 2.4.9
Any help is appreciated.
Greg
according to the docs
$Email->attachments(array(
$attachment_name => array(
'file' => '$file'
)
));
It probably depends on the version of CakePHP you are using (2.x) and probably a version of PHP. I was struggling with the 0 bytes attachments as well. I had to dig in the vendor email file. Basically it turned out, that the _readFile() fn was messing things up with CakePHP File Reader.
So what worked for me was to specify the filename, specify the full path and attach the data of the file in base64. Then everything worked. All other options from documentation didnt work. I was running PHP 5.4.45 (old one).
'filename' = array(
'file' => '/full/path/to/the/file/on/disc.pdf',
'data' => chunk_split(base64_encode(file_get_contents('/full/path/to/the/file/on/disc.pdf')))
);

Cakephp 2.3.x Sending Files and forcing the download of a mp4 file

I'm using cakephp 2.3.1
I want to force download a mp4 file per http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file
In my 'view' I have the following code which is correctly searching for the filename, and finding the file name, and displaying the download link:
<?php $filename = APP . 'webroot/files/' . $dance['Dance']['id'] . '.mp4';
if (file_exists($filename)) {
echo $this->Html->link('DOWNLOAD', array('controller' => 'dances', 'action' => 'sendFile', $dance['Dance']['id']));
} else {
echo 'Coming soon: available April 16th';
}
?>
When the user clicks on the link I want to force the download of the mp4 file. In my controller I have the following code that doesn't work:
public function sendFile($id) {
$file = $this->Attachment->getFile($id); //Note: I do not understand the 'Attachment' and the 'getFile($id)'
$this->response->file($file['webroot/files/'], array('download' => true, 'name' => 'Dance'));
//Return reponse object to prevent controller from trying to render a view
return $this->response;
}
I don't understand 'Attachment' and the 'getFile()'
I'm getting the following error:
Error: Call to a member function getFile() on a non-object
What am I doing wrong and is there any other documentation I can be looking at to understand this better?
The line you don't understand is simply part of the example - it assumes the application has a model called Attachment and that it has a method called getFile. Since you don't have an Attachment model (or at least it isn't visible to the controller) you get a "call to member function on a non-object" error. That's not important though: all you need to worry about is providing a full system path to this->response->file(). In your example, you can probably get that by changing that line to:
$this->response->file(WWW_ROOT.'files/'. $id .'.mp4', array('download' => true, 'name' => 'Dance'));
You can get rid of the $this->Attachment->getFile line as it is irrelevant in your case.
Let me know if that helped!
public function downloadfile($id= null) {
$this->response->file(APP.'webroot\files\syllabus'.DS.$id,array('download'=> true, 'name'=>'Syllubus'));
return $this->response;
}
<?php echo $this->Html->link('Syllabus',
array('controller' => 'coursesubjects',
'action'=>'downloadfile',
$courseSubject['CourseSubject']['subject_syllabus']));
?>

Wrong File Type for File Download - CakePHP

In my cakephp web page, I have a button that the user clicks and it performs some maintenance on the server and then presents a download to the user. The file download is testlogs.gzip but when the download option occurs in firefox, it comes up as a file type of html document.
The file downloads and extracts fine, but I want the file type to be correct.
Here is the Media view class for the download:
//$this->autoRender = false;
$this->viewClass = 'Media';
$params = array(
'id' => 'systemlogs',
'name' => 'testsystemlogs',
'download' => true,
'extension' => 'gzip',
// 'mimeType' => 'zip',
'path' => DS . 'home' . DS
);
$this->set($params);
I tried to add in the 'mimeType' => 'zip' but that did not work.
Any help would be great.
Thanks
UPDATE: I tested uploading of the same files and I used firebug to determine the content type. The only file types I care about are of type: application/octet-stream.
So I think I just need to set this type in the Media class settings, not sure how to do that though.
THanks
mimeType has to be specified as an associative array (extension -> type), in your case this would be array('gzip' => 'application/x-gzip'). CakePHP merges this array with the built-in array of known MIME types in CakeResponse class.
However, CakePHP already knows the MIME type of .gz files so changing the extension of your file, if it is an option, might be an even easier solution.

Problem when trying to upload zip/rar files with the milesjohnson's upload plugin

I'm new to cakephp and I have found this milesjohnson's upload plugin ,
and I kind of like it, mostly because it gives me the chance of renaming the file once uploaded.Unfortunately, I can't get it to upload any zip/rar files.
This is the action where I upload the file:
function add() {
if (!empty($this->data)) {
if ($data = $this->Uploader->upload('link_referencia', array('name' => date("dmYhis")))) {
debug($data);
$this->data['Publicacione']['link_referencia']=$data['name'];
}
$this->Publicacione->create();
if ($this->Publicacione->save($this->data)) {
$this->Session->setFlash(__('The publicacione has been saved', true));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The publicacione could not be saved. Please, try again.', true));
}
}
$users = $this->Publicacione->User->find('list');
$this->set(compact('users'));
}
And this is the error I get everytime I attempt to upload any zip/rar file :
EDIT
Full insert query:
INSERT INTO `publicaciones` (`vigencia`, `tipo`, `titulo`, `descripcion`, `fecha_publicacion`, `fecha_caducidad`, `link_referencia`, `modified`, `created`) VALUES (1, 'c', 'there\'s nothing you can\'t do', '
fsdfsdfsdf
', '2011-06-07', '2011-06-30', Array, '2011-06-07 16:47:23', '2011-06-07 16:47:23')
Does anyone have any ideas on what the problem might be?
Thanks in advance.
i think there is nothing with the code what you have written but you should look at in to the plugin and find out whether it gives permission you to upload zip or not.
there will be conditions in your pluggin that you can upload only some kind of files like jpg,png,txt something like that.
I hope that helps you.
Regards,
Archit
Are you totally sure your File input on the form has type=>file specified, and same for your form?
Also I would look at other uploaders - MeioUpload can allow renaming and so on and is a bit more up to date. There is also the Cuploadify plugin (uploadify for cake) you can find on github.
Make sure your form is set to multipart/form-data like so:
<?php echo $this->Form->create('File', array('enctype' => 'multipart/form-data')); ?>
The following code will upload a file:
if ($this->request->is('post')) {
if ( $this->data['File']['file']['error'] <= 0 && $this->data['File']['file']['size'] <= 8388608 ) { // Check for no errors and that File size is around 8mb.
$folder = new Folder (ROOT . DS . 'app' . DS . 'filestorage' . DS, true); // create folder in /app/filestorage/
$path = $folder->path . $this->data['File']['file']['name']; // Set path to newly created folder + uploaded file name.
$tmpUrl = new File ( $this->data['File']['file']['tmp_name'] ); // Create temporary file object
if ($tmpUrl->copy($path , true) ) { // If copying file to path is successful,
$this->Session->setFlash(__('File uploaded succesfully!'));
}
}

List CakePHP File Upload error codes

Anyway, can anyone give me the error code definitions for when files are uploaded in cakePHP.
So my $this-data contains something like this,
Array
(
[file] => Array
(
[name] => cake.jpg
[type] => image/jpeg
[tmp_name] => /tmp/hp1083.tmp
[error] => 1
[size] => 24530
)
)
What does [error] = 1 indicate?
While you're at it can you list the break down of all the numbers, then maybe it'll be easier for others to find it the future
Thanks!
File upload has nothing to do with CakePHP.
$this->data contains what $_FILE contains, it is a HTTP/PHP specific array.
Documentation, $_FILE, and the error codes.

Resources