Hi ,
In a form , I have a field for image , in edit page, i can display that image, when I am clicking update, without modifying image it shows error in image.
How to save previous image if its not changed.

Sample Code:

<span><img src="profile/<?=$rows["upload"]; ?>" id="blah" style="height:25%; width:50%;"><br />
        <input type="file"  name="upload" id="upload" onChange="validation();putImage();" style="margin-top:10%;" tabindex="12"/>
 Update page:
$filename = $_FILES['upload']['tmp_name'];
//echo $filename;
$file_size = $_FILES['upload']['size'];
if($file_size> 1048576)
echo "<script type='text/javascript'>alert('File Size Exceed 1 Mb!'); window.location.href='preview.php';</script>";
$target_path = "profile/";
move_uploaded_file($_FILES['upload']['tmp_name'], $target_path .$random_no."_".$_FILES['upload']['name']);

$newupid=mysql_query("update candidate setupload='$uploaded_files' where id='$upid'")or die(mysql_error());
echo "<script type='text/javascript'> alert('Updated Successfully'); window.location.href='otpreview.php';</script>";

Recommended Answers

All 3 Replies

Based on your code, when user did not update the profile picture, the input type="file" name="upload" will be empty field where after being posted to the php, the checking on if(!empty($filename)) will be false and do nothing but still, it will proceed to $newupid=mysql_query("update candidate setupload='$uploaded_files' where id='$upid'") where the $uploaded_files is probably null and causing error in the end.

you can try using


to validate if the file was uploaded.

Another alternative way of doing this is to check if the file has been moved or not.

    //do whatever is needed to done


you can also check based on the user's input,

$there_is_file = (!empty($_POST['upload']) ? true : false );


    //process file


    //leave it alone on update


you can use this code:
first line of existing image is that image, which always fetch from database.

$existing_image = $ri['upload'];

if(isset($_FILES['upload']) && ($_FILES['upload']['size']>0)){
    else { $photo = date('dmY-his');
    $photo .="-".$_FILES['upload']['name'];

    move_uploaded_file($_FILES['upload']['tmp_name'], 'profile/'.$photo);
    else { $photo = $existing_image; }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.