user1269625
user1269625

Reputation: 3209

PHP File upload, if left blank

I having a bit of an issue with my file upload, it works fine and everything, but I goto update my data and I want to change everything except for the image and hit submit, I get the Invalid Image message, I am not updating the images so there is no file in the file input text box, is there a way to bypass this?

if (($_FILES["image"]["type"] == "image/jpeg") || ($_FILES["image"]["type"] == "image/pjpeg")){
            if ($_FILES["image"]["error"] > 0){
                echo $_FILES["image"]["error"];
            }else{
                move_uploaded_file($_FILES["image"]["tmp_name"],
                "../upload/CV_1_" . date("Ymd") . $_FILES["image"]["name"]);                                                                    
                $class->update($id, $text, $image);
                echo "<div style='padding-left:50px'><strong>Updated!</strong></div>";
            }
        }else{
            echo "<div style='padding-left:50px'><strong>Invalid Image!</strong></div>";
        }

What the code above does is take the file, see if its an image and if there are any errors, show them, then it moves the file into the proper folder and updates my database.

Here is the form...

<form action="CV.php?action=updatesubmit" method="post" enctype="multipart/form-data">
<input type="hidden" value="<?php echo $array['id']; ?>" name="id" />
<p>
<label for="text" style="vertical-align:top;">Text</label>
<textarea name="text" id="text" cols="70" rows="20"><?php echo $array['text']; ?></textarea>
</p>

<p>
<label for="image">Image</label>
<input type="file" name="image" id="image" value="<?php echo $array['image']; ?>" />
</p>

<p>
<input type="submit" name="submit" id="submit" value="Update" />
</p>

</form>

I hope I made sense, I have a habit of tying something out, it sounds good to me but other people wont know what I am talking about.

Played with my code and this was suggested but its not working, I'm probably doing something wrong.

    if ($_FILES['image']['error'] === UPLOAD_ERR_OK) {

    if (($_FILES["image"]["type"] == "image/jpeg") || ($_FILES["image"]["type"] == "image/pjpeg")){
        if ($_FILES["image"]["error"] > 0){
            echo $_FILES["image"]["error"];
        }else{
            move_uploaded_file($_FILES["image"]["tmp_name"],
            "../upload/CV_1_" . date("Ymd") . $_FILES["image"]["name"]);    
}
            $class->update($id, $text, $image);
            echo "<div style='padding-left:50px'><strong>Updated!</strong></div>";
        }
    }else{
        echo "<div style='padding-left:50px'><strong>Invalid Image!</strong></div>";
    }

Upvotes: 1

Views: 3183

Answers (3)

Milap
Milap

Reputation: 7231

It will submit if and only if image is uploaded.

<?php
if ($_FILES["image"]["tmp_name"] && $_FILES["file"]["error"] < 0){
    if (($_FILES["image"]["type"] == "image/jpeg") || ($_FILES["image"]["type"] == "image/pjpeg")){
                if ($_FILES["image"]["error"] > 0){
                    echo $_FILES["image"]["error"];
                }else{
                    move_uploaded_file($_FILES["image"]["tmp_name"],
                    "../upload/CV_1_" . date("Ymd") . $_FILES["image"]["name"]);                                                                    
                    $class->update($id, $text, $image);
                    echo "<div style='padding-left:50px'><strong>Updated!</strong></div>";
                }
            }else{
                echo "<div style='padding-left:50px'><strong>Invalid Image!</strong></div>";
            }
}
?>

Upvotes: 0

Marc B
Marc B

Reputation: 360702

You need to check for an actual upload before you do ANYTHING ELSE with the upload data:

if ($_FILES['image']['error'] === UPLOAD_ERR_OK) {
   ... got an upload ...
}

Upvotes: 4

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324650

Just put if(isset($_FILES['image'])) around the whole block. If no image is uploaded, the file entry won't be there.

Upvotes: 1

Related Questions