Ich bin neu in CakePHP, ich erstelle eine Anwendung mit diesem Framework jetzt habe ich ein kleines Problem leiden, nicht Bild hochladen aber Bildname in der Datenbank speichern. unter meinem Code:Kein Bild auf CakePHP hochladen 3
public function save()
{
$shop = TableRegistry::get('shop');
$query = $shop->query();
//
$product = $this->request->data('prductname');
$domain = $this->request->data('domainname');
$phone = $this->request->data('phone');
if (!empty($this->request->data))
{
$file = $this->request->data['file'];
}
$filename = $file;
$file_tmp_name = $filename;
$dir = WWW_ROOT.'img'.DS.'uploads';
$allowed = array('png','jpg','jpeg');
if (!in_array(substr(strrchr($filename,'.'),1),$allowed))
{
throw new InternalErrorException("Error Processing Request", 1);
}
elseif(is_uploaded_file($file_tmp_name))
{
$filename = Text::uuid().'-'.$filename;
$fileDB = TableRegistry::get('shop');
$entity = $fileDB->newEntity();
$entity->filename = $filename;
$fileDB->save($entity);
move_uploaded_file($file_tmp_name, $dir.DS.$filename);
}
$query->insert([
'prductname','domainname','phone','file'
])->values([
'prductname'=>$product,
'domainname'=>$domain,
'phone'=>$phone,
'file' => $file
])->execute();
if ($query)
{
$this->Flash->success('This information has been saved.');
$this->redirect(['controller'=>'shop','action'=>'index']);
}
}
Form:
<?php
echo $this->Form->create(null,['url'=>['controller'=>'Shop', 'action'=>'save']],['enctype' => 'multipart/form-data']);
?>
<div class="form-group">
<label for="product">Product Name</label>
<input type="text" class="form-control" name="prductname" placeholder="Name">
</div>
<div class="form-group">
<label for="domain">Domain Name</label>
<input type="text" name="domainname" class="form-control" placeholder="Domain Name">
</div>
<div class="form-group">
<label for="Phone">Phone</label>
<input type="text" name="phone" class="form-control" placeholder="Phone">
</div>
<div class="form-group">
<label for="InputFile">File input</label>
<input type="file" name="file">
</div>
<button type="submit" class="btn btn-default">Save</button>
<?php
echo $this->Form->end();
?>
nicht angezeigt alle Fehler, Bildnamen DB Speichern, aber nicht ersetzen Bild Ordner Ziel.
Dank
Können Sie uns zur Verfügung stellen, den Code für die Funktion 'move_uploaded_file();'? – technico