Jump to content

Image Not Uploading


Moorcam
 Share

Go to solution Solved by requinix,

Recommended Posts

Hi guys,

Using the following code to upload an image to a directory. It aint being funky...

if ($_FILES['image']['error'] = UPLOAD_ERR_OK)  {
        // image file directory
        $target = "images/uploads/".basename($_FILES['image']['name']);
        if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
          $msg = "Image uploaded successfully";
          $post['image'] = $target;
      }else{
          $msg = "Failed to upload image";
          $post['image'] = null;
      }
    } else {
        $post['image'] = null;
    }

Can anyone tell me what's up?

Thanks guys :)

Link to comment
Share on other sites

3 minutes ago, requinix said:

Step away from the computer, make yourself a sandwich or watch some TV or whatever you like, then come back to your code and look at the first line.

Had the sandwich and the coffee. No smoke because I gave them up about 2 weeks ago (God help everyone around me) and had a look at the first line. Changed it to the following:

 if(!empty($_FILES['image']['name'])) {

Works perfectly. No idea why that didn't shout at me earlier.

Thank you kindly.

Link to comment
Share on other sites

2 minutes ago, Moorcam said:
 if(!empty($_FILES['image']['name'])) {

Works perfectly. No idea why that didn't shout at me earlier.

Perhaps because what you had before was better. In fact it wasn't just better, it was correct. All it was missing was an equals sign.

Link to comment
Share on other sites

Thanks heaps. Now, if there is a scenario where the form needs to be updated but without uploading a new image, at this time it just gets rid of the current image. I need to work out how to prevent that.

Previously, before moving to PDO, I had a second insert query inside an if clause. Tried it with this but got an UNEXPECTED else.

Link to comment
Share on other sites

If you change the query to this

$sql = "UPDATE users SET name = :name, email = :email, phone = :phone, address = :address, license_number = :license_number, 
                position = :position, role = :role, submittedby = :submittedby, image = COALESCE(:image, image)
                WHERE id = :id
                ";

then, if :image is NULL, the existing value will not be changed.

Link to comment
Share on other sites

6 minutes ago, Barand said:

If you change the query to this

$sql = "UPDATE users SET name = :name, email = :email, phone = :phone, address = :address, license_number = :license_number, 
                position = :position, role = :role, submittedby = :submittedby, image = COALESCE(:image, image)
                WHERE id = :id
                ";

then, if :image is NULL, the existing value will not be changed.

Thank you so much man. Really appreciate it.

Promise I won't bother you anymore ... until the next issue :D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.