I use the following code to upload images.
I madethe images file mandatory, so save() is invalidated if there is no
images.
I choose to store them in a directory named after the object's id.
I dunno on what to extend the explanation, please tell.
You'll find the file in $this->data['MyObject']['image']['tmp_name']
Also make sure your form contains the attribute:
enctype="multipart/form-data"
olivvv
function edit($id = null)
{
$img = '';
if (isset($this->data['Applicationimage']['image']['tmp_name']))
$img = $this->data['Applicationimage']['image']['tmp_name'];
if (empty($this->data)){
if ($id) $this->data = $this->Applicationimage->read(null, $id);
$this->set('applicationArray',$this->Applicationimage->Application->generateList());
}else{
if (!$id) if (($img == '') ||
(!$this->_isValidImage($img)))
$this->Applicationimage->invalidate('Applicationimage/image');
if ($id) if ($img != '') if (!$this->_isValidImage($img))
$this->Applicationimage->invalidate('Applicationimage/image');
$this->cleanUpFields();
if ($this->Applicationimage->save($this->data)){
if ($img != ''){
if ($id) $this->_uploadfile($img,
$this->imgpath.DS.'application'.DS.$id.'.jpg');
if (!$id) $this->_uploadfile($img,
$this->imgpath.DS.'application'.DS.$this->Applicationimage->getLastInsertID().'.jpg');
}
$this->flash('Votre image d\'application a
été modifiée.','/applicationimages');
}else{
$this->set('applicationArray',$this->Applicationimage->Application->generateList());
if ($id) if ($img != '') if
(!$this->_isValidImage($img)) $this->set('imageerror', true);
if (!$id) if ($img == '') $this->set('imageerror', true);
}
}
}
################ functions in app_controller : ##################
function _selectedArray($data, $key='') {
$array = array();
if(!empty($data)) {
if ($key != ''){
foreach($data as $var) {
$array[$var[$key]] = $var[$key];
}
} else {
foreach($data as $var) {
$array[$var] = $var;
}
}
}
return $array;
}
function _isValidImage($tmp_name){
if (is_uploaded_file($tmp_name)) if
(@getimagesize($tmp_name)) return true;
else return false;
}
function _uploadFile($tmp_name, $filename){
if (is_uploaded_file($tmp_name)){
if(file_exists($filename)) unlink($filename);
move_uploaded_file( $tmp_name , $filename);
return true;
}
else return false;
}
################## view file: ##########
<h1>Ajouter une image d'application:</h1>
<form action="<?php echo
$html->url('/applicationimages/edit/'.$html->tagValue('Applicationimage/id'));
?>" enctype="multipart/form-data" method="post">
<?php //echo $html->formTag('/applicationimages/add')
?>
<p>
<?php echo $html->tagErrorMsg('Applicationimage/name', 'Entrez le
nom.'); ?>
Nom: <?php echo $html->input('Applicationimage/name',
array('size'=>'40')); ?>
</p>
<p>
<?php echo $html->tagErrorMsg('Applicationimage/image', 'Entrez
l\'image.'); ?>
<?php if(isset($imageerror)) echo '<div class="error">Choisissez une
image.</div>'?>
Image: <?php echo $html->file('Applicationimage/image'); ?>
</p>
<?php if(!empty($this->data['Applicationimage']['id'])){ ?>
<img src="/phpThumb/phpThumb.php?src=/cakeimg/application/<?php echo
$this->data['Applicationimage']['id'] ?>.jpg&w=200" width="200"/>
<?php } ?>
<p>
Application:
<?php echo $html->tagErrorMsg('Applicationimage/application_id',
'Choisissez une application.'); ?>
<?php echo $html->selectTag('Applicationimage/application_id',
$applicationArray, null, null) ; ?>
</p>
<p>
<?php echo $html->hidden('Applicationimage/id') ?>
<?php echo $html->submit('Save') ?>
</p>
</form>
[EMAIL PROTECTED] wrote:
> Okay I am integrating a Multiple File upload Flash applet into CakePHP.
> It is configurable to direct the uploaded files to a CakePHP url.
> Now, here is the line of code normally used by the script to get at the
> files uploaoded.
>
> move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadfile)
>
> Is there a pretty way to do this same thing in CakePHP? I am pretty
> new to this POST upload of files so just looking to get some more
> information on the CakePHP way to do it since I couldn't really find
> much on this Group about it.
>
>
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---