List CakePHP File Upload error codes - file

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.

Related

CakePHP 3.0 - Virtual property missing on production server

I have a strange issue with CakePHP 3.0 and virtual properties on our server.
We have a Photo Entity with the following virtual property:
protected function _getPath()
{
[...]
return $path; // array with path for different photo sizes
}
On our development server (Ubuntu, Apache, PHP 5.5.9-1ubuntu4.6) everything works perfectly.
On our production server (Linux, Apache, PHP 5.5.23) the site is working great, except that the virtual properties are missing in the data object.
The photo entity is read from db via Entries Table->contain and echoed like this:
$entry->photos[0]['path']['wide'];
The basic properties of the Photo entity can be read on both servers with e.g.:
$entry->photos[0]['filename'];
In addition I just found out, that a pr() on $entry->photo[0] (or $photo in foreach loop) is different on both servers.
On the server where everything works as expected i get:
App\Model\Entity\Photo Object
(
[_accessible:protected] => Array
(
[user_id] => 1
[entry_id] => 1
[filename] => 1
[org_name] => 1
[description] => 1
[user] => 1
[entry] => 1
)
[_virtual:protected] => Array
(
[0] => path
)
[...]
and on the server where the virtual property is missing i get:
Cake\ORM\Entity Object
(
[_properties:protected] => Array
(
[id] => 37
[user_id] => 1
[entry_id] => 4
[filename] => p19fng7349bb2p6nsac14j51qnu4.jpg
[...]
Any idea why the virtual property on the production environment is missing, and why the objects on both servers are of different types?
Thanks a lot!
Simon

audio upload in cakephp2.4

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.

How do I get data from associated models in CakePHP?

I create a simple blog with cakephp where i have some posts and some comments.
After i bake an application it create a comments folder with the index.ctp and a posts folder with the index.ctp
What i want to do is display them as fallow:
Post1
comment1
comment2
Post2
comment1
comment2
If i place the comments inside the posts/index.ctp i get an error telling me that the $comments is not defined.
How can i do this?
Thanks
edit:
alright, im sorry for the comeback, but it's still a bit unclear. I do have the $hasMany relationship setup, in fact on the page that displays the posts i have a link that points me to the comments. The only thing that i want them to be displayed in the same page as the posts.
i should be able to say <?php echo $comment['Comment']['content']; ?>
Check your controllers.
If you want to display Comment information in a Posts view, you need to be sure that the Posts controller can load the data.
The "CakePHP" way is to define relationships between models. Check your baked models and see if there is something like this:
class Post extends AppModel{
var $hasMany = array( 'Comment' );
}
When the models are associated with each other, your Posts controller will automatically find Post objects and their associated Comment objects.
For instance, this line:
$this->Post->findById( $id );
Will produce something like this:
Array
(
[Post] => Array
(
[id] => 42
[text] => Post1
)
[Comment] => Array
(
[0] => Array
(
[id] => 1
[post_id] => 42
[text] => Comment1
)
[1] => Array
(
[id] => 2
[post_id] => 42
[text] => Comment2
)
)
)
Good documentation at http://book.cakephp.org/
Model Associations
Retreiving Data
EDIT: Adding more info after your comment
As long as the models have the association, CakePHP will pull the data appropriately (unless you set Recursive => false or are using Containable which I assume you aren't).
Check your PostsController controller and see how it's loading the data. I'm guessing it is doing something like the following:
$post = $this->Post->findById( $id );
$this->set( compact( 'post' ) );
or
$this->data = $this->Post->findById( $id );
Check which way it is storing the retrieved data, and then access that variable from the view.
For example, if it is storing the data in a variable named "$post", you would put something like this in your view:
// output the 'text' field of the 'post' object
echo $post[ 'post' ][ 'text' ];
// loop through associated comments
foreach ( $post[ 'comment' ] as $comment ){
//output the 'text' field of a 'comment' object
echo $comment[ 'text' ];
}
By default CakePHP stashes tons of detail in arrays after retrieving data. The trick is to know the hierarchy of the data and fetch it from the array accordingly.

CakePHP removing special characters from this->params

I am using jQuery to pass data to the following URL in my cakephp 1.2 app:
$("#test").load("http://domain.com/controller/action/productID:2001642/questionID:2501322/value:C%2B%2B/questionType:3", function({
$("#test").fadeOut(3000);
});
In the controller when I
debug($this->params['named']);
it returns
Array
(
[productID] => 2001642
[questionID] => 2501322
[value] => C
[questionType] => 3
)
The URL part of $this displays
[url] => Array
(
[url] => deu/productanswers/updateoredit/productID:2001642/questionID:2501322/value:C /questionType:3
)
so that somewhere along the line the C++ or C%2B%2B is getting squished.
Does anyone have a solution or workaround please?
Cheers,
Taff
Although I would be very interested in a cakephp solution, I resorted to using $_SERVER['REQUEST_URI']
Definitely not a sexy solution
$tmp1 = explode('value:',$_SERVER['REQUEST_URI']);
$tmp2 = explode('/',$tmp1[1]);
$prod=$this->params['named']['productID'];
$ques=$this->params['named']['questionID'];
$value=urldecode($tmp2[0]);
Hope this helps someone in the future...

How to use MeioUpload Behavior for uploading any kind of files not only images

I would like to use the MeioUpload Behavior for uploading any kind of documents(I want every extension to be accepted). I've already seen this question , but it didn't work for me, for some strange reason I can only upload image and pdf files for the other types of files I get this error when I attempt to submit the form : "The post could not be saved. Please, try again."
Edit: Well, it looks like I was finally able to upload other kind of files apart from images,I had to write the options 'allowedMime' and 'allowedExt' in camelCase (in the documentation they use the underscore version 'allowed_mime', 'allowed_ext' I don't know why:( ), but I haven't been able to upload .zip files and most importantly I still don't know how to tell the behaviour to accept anything
var $actsAs = array(
'MeioUpload' => array(
'link_referencia' => array(
'dir' => 'files{DS}uploads',
'create_directory' => true,
'allowedMime' => array('application/pdf', 'application/msword', 'application/vnd.ms-powerpoint', 'application/vnd.ms-excel', 'application/rtf', 'application/zip'),
'allowedExt' => array('.pdf', '.doc', '.ppt', '.xls', '.rtf', '.zip'),
'default' => false,
)
)
);
Thanks in advance
It can be use for anything, hence it's called meioUpload and not meioImage
Just set the allowed file extensions and mime types when you initialiave the behavior.
Step 4 has an example http://www.meiocodigo.com/projects/meioupload/

Resources