2017-04-12 2 views
0

Ich habe Probleme, PHP dazu zu bringen, den Bildpfad oder das Verzeichnis in die MySQL-Datenbank einzufügen. Anstelle des Pfads und des Namens des Bildes wird nur der Bildname gespeichert. Außerdem benenne ich das Bild so um, dass es dem Titel des Gegenstands entspricht, unter dem es gespeichert ist (z. B. Blaues Auto - Titel, Blaues Auto - Bildname). Anstatt den neuen Bildnamen hochzuladen, wird nur der ursprüngliche Bildname gespeichert.Bildverzeichnis in MySQL einfügen

Dies ist mein Code für das Umbenennen und Hochladen des Bildes etc .:

// The directory that will recieve the uploaded file 
$dir = 'uploads/'; 

//variables for images 
$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$temp = explode(".", $_FILES["picture"]["name"]); 
$extension = end($temp);   

if(isset($_POST['submit'])) { 
    if (strlen($title)>0 && strlen($description)>0) { 
      move_uploaded_file($_FILES['picture']['tmp_name'], $dir . "/" . $title ."." . $extension); 


      // Query database and insert data into item table 
      $itemQry = 'INSERT INTO items (title, picture, startPrice, category, description, quantity, location, sellingFormat, duration, paymentType, postageDetails) 
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; 
      $statement = $conn->prepare($itemQry); 
      $statement->bind_param('sssssssssss', $title, $_FILES['picture']['name'], $startPrice, $category, $description, $quantity, $location, $sellingFormat, $duration, $paymentType, $postageDetails); 
      $statement->execute(); 
+0

* es speichert nur den Bildnamen *, denn das ist es, was Sie ihm gesagt haben –

+0

$ _FILES ['bild '] [' name '] gibt nur den Namen des Bildes zurück (zB file.jpg) – Akintunde007

+0

wie ändere ich das? – Joel

Antwort

1

starten:

// The directory that will recieve the uploaded file 
$dir = 'uploads/'; 

//variables for images 
$allowedExts = array("gif", "jpeg", "jpg", "png"); 
$temp = explode(".", $_FILES["picture"]["name"]); 
$extension = end($temp);   

if(isset($_POST['submit'])) { 
    if ((strlen($title) > 0) && (strlen($description) > 0)) { 
     $newFilePath = $dir . "/" . $title . "." . $extension; 
     move_uploaded_file($_FILES['picture']['tmp_name'], $newFilePath); 

     // Query database and insert data into item table 
     $itemQry = 'INSERT INTO items 
      (title, picture, startPrice, category, description, 
      quantity, location, sellingFormat, duration, 
      paymentType, postageDetails) 
      VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'; 

     $statement = $conn->prepare($itemQry); 
     $statement->bind_param('sssssssssss', $title, $newFilePath, 
         $startPrice, $category, $description, 
         $quantity, $location, $sellingFormat, 
         $duration, $paymentType, $postageDetails); 

     $statement->execute(); 
    } 
} 

Hinweis: Hier bin $newFilePath als neue Variable verwendet haben, die den Weg hält und der neue Dateiname und mit dem gleichen Dateinamen in den DB einzufügen. Hoffe das hilft dir :).

+0

Ausgezeichnet, vielen Dank !! – Joel

+0

Froh, dass es geholfen hat! :) aber ja, von @Akin, "bitte verwenden Sie nicht nur die Erweiterung zu überprüfen. Verwenden Sie stattdessen den Mime-Typ. Was Sie haben, kann leicht gefälscht werden". Pass auf diese Sache auf. – prava