Hunter Winchester
Hunter Winchester

Reputation: 305

Fetch img source from database

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!

Upvotes: 10

Views: 252

Answers (6)

Jose Castro
Jose Castro

Reputation: 99

here is the problem -> if (!empty($_FILES['fileToUpload'])) you are saying the if the file is not empty do this. what you want to say if file is empty do this just remove the '!' should work.

You can also try -> $_FILES['myVarName']['size'] > 0 to check the size of the file to see if the file is empty.

Upvotes: 0

meru matt
meru matt

Reputation: 1

$file = "";

//Run SQL query to update the table fields except file field here

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

if($file!="")
{
    //Query to update file field only
}

Upvotes: 0

Gayasuddin Usmani
Gayasuddin Usmani

Reputation: 47

if(strlen($_FILES)<=4){
$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'];
}

/* Note : strlen() check if $_FILES have value then it is more then 4 char (3 file extension and 1 dot */ 

Upvotes: 0

shadyhossin
shadyhossin

Reputation: 575

I think your question is not really reflecting your problem? It seems you actually have problem fetching the image source from the database? Beside the code you provided doesn't help much. maybe post the code that is related to the database activity so we can see what's wrong with updating/fetching the data from the database.

However based on assumptions I will try answer your question.

If no other data is passed along with the request except image update, then the solution should be, move your Database queries into the if block. Meaning.. don't update the database if no image is uploaded.

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

    //database update here
}

because if your database queries happen outside the IF block, the database will get updated anyway, and I assume you pass in the $file variable in the query. and $file in this case will be empty since $file is only defined and given a value in the IF block.

I would love to help you further, but to do so you need to help us with some more information like,

You could share more of your code (you can hide sensitive data)

are there other data sent with the upload that you might need to update the user data in the database but keep the image as is?

If so.. you have 2 options

1) get the current user file before making the update by selecting it from the database, store it in the $file variable before the IF block you stated. If the condition is true, the $file variable will be updated, if not, the $file variable will have the old value you assigned to it which is the current user file.

2) create a dynamic sql query, meaning if a user uploaded a new photo add that to the query, if not don't add it to the query and only update the fields that need to be updated. I'm not familiar with PDO, but the concept is here.

Upvotes: 0

Nehal
Nehal

Reputation: 1523

If your file is not empty then ,

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

else if your file is empty which means on update/edit condition, then retreive it from your db relevant to that id

    else
    {
// fetch your image from database relevant to that id
    $select=mysql_query(select image from your_table where id='$id');
    $row=mysql_fetch+assoc($select);

      $file=$row['image'];
    }

Upvotes: 0

jack brone
jack brone

Reputation: 205

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'];
}

Upvotes: 1

Related Questions