Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch img source from database

Tags:

php

I'm working on updating my data in database and here's my problem:

Here's the code:

if (!empty($_FILES['fileToUpload']))
{
    $dest = 'images/Uploaded/';
    if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $dest.$_FILES['fileToUpload']['name']))
        $file = $dest.$_FILES['fileToUpload']['name'];
}

And I want that if $_FILES if empty, the current file stored in the database we'll just retain it's value when I update. What happens in my code is when there's already an existing image and I don't click Upload File, the image that has been already there vanishes.

Please help!

like image 570
Hunter Winchester Avatar asked May 22 '15 08:05

Hunter Winchester


1 Answers

If empty files post, you need to fetch oldfile name from your database depend by id

if(empty($_FILES)){
    $qry = "select * from tablename where id = ? "; 
                $q = $this->db->conn_id->prepare($qry);
                $q->bindValue(1, $id, PDO::PARAM_INT);
                $q->execute();
                while($rows = $q->fetch(PDO::FETCH_ASSOC) ){
                $filename = $rows['imagename'];
                }
} else {
   $dest = 'images/Uploaded/';
    if (move_uploaded_file($_FILES['fileToUpload']['tmp_name'],    $dest.$_FILES['fileToUpload']['name']))
        $file = $dest.$_FILES['fileToUpload']['name'];
}
like image 61
jack brone Avatar answered Oct 23 '22 23:10

jack brone