10

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!

Will
  • 24,082
  • 14
  • 97
  • 108
  • 1
    You could simply use a condition when building your query. – Burki May 22 '15 at 08:52
  • @Hunter : You can try this http://www.dreamincode.net/forums/topic/118382-how-to-update-an-image-in-mysql/ – Abdul May 22 '15 at 10:03
  • OR use if (file_exists($TARGET_PATH)) { // your code } – Abdul May 22 '15 at 10:08
  • I've tried it too (using PDO) nothing happens. >_ – Hunter Winchester May 24 '15 at 23:48
  • The `$_FILES` array will not necessary be empty just because you didn't upload a file. The file input will still be posted. Try `echo"
    ".print_r($_FILES,true)."
    ";` to troubleshoot. See [How to check if user uploaded a file in PHP?](http://stackoverflow.com/questions/946418/how-to-check-if-user-uploaded-a-file-in-php)
    – showdev Jun 15 '15 at 16:55

6 Answers6

1

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'];
}
jack brone
  • 205
  • 5
  • 24
0

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'];
    }
Nehal
  • 1,542
  • 4
  • 17
  • 30
0

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.

Jose Castro
  • 99
  • 2
  • 5
  • this is not true, `empty()` is used to check if the array containing variables related to the upload are given (aka, if a file has been uploaded) – Sjon Jul 13 '15 at 17:01
0

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.

shadyhossin
  • 575
  • 1
  • 6
  • 16
0
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 */ 
Flexo
  • 87,323
  • 22
  • 191
  • 272
0
$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
}