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!
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'];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With