2016-08-05 8 views
0

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

+0

Können Sie uns zur Verfügung stellen, den Code für die Funktion 'move_uploaded_file();'? – technico

Antwort

0

Ich denke, dass $file_tmp_name könnte für das, was move_uploaded_file erwartet falsch. move_uploaded_file erwartet, dass die $tmp_name die eindeutige Zeichenfolge ist, die PHP jeder hochgeladenen Datei zuweist. Ich weiß nicht genau, welche Struktur $this->request->data['file'] haben wird, aber das $_FILES Array hat Array-Schlüssel wie 'name', 'size', 'tmp_name' und 'type'. Sie müssen den 'tmp_name' hier in move_uploaded_file gefunden verwenden. Dies könnte in $this->request->data vorhanden sein, und wenn es so ist, wäre es besser, die 'tmp_name' aus diesem Array zu verwenden.

http://php.net/manual/en/function.move-uploaded-file.php

http://php.net/manual/en/features.file-upload.php