daugaard47
daugaard47

Reputation: 1868

Submit form data with optional image upload

I have a form that I want users to have the option to add text only, an image only or both.

If you look at the database table here:

enter image description here

Fig 1 errors:

Warning: getimagesize(): Filename cannot be empty in D:\htdocs\site.com\classifieds\addlisting-test.php on line 26
Your Listing has been posted!

Warning: unlink(../images/users/lea47/classifieds/53bce4ccaa93d): No such file or directory in D:\htdocs\site.com\classifieds\addlisting-test.php on line 200

Line 26 is here in this block. I'll add a comment to show line 26:

$name       =   $_FILES['image']['name'];
$temp       =   $_FILES['image']['tmp_name'];
$type       =   $_FILES['image']['type'];
$size       =   $_FILES['image']['size'];
$ext        =   strtolower(end(explode('.', $name)));
$size2      =   getimagesize($temp); //LINE 26!!!
$width  =   $size2[0];
$height     =   $size2[1];
$upload     =   uniqid();

If someone could help me get rid of the first warning I think it would take care of the unlink warning...Maybe

I'm thinking I need an if / else statement on the getimagesize, but to be honest it's got me totally confused on how to fix this issue.

My Question:

So what I'm asking is if someone could help me figure out how to submit text only. Without the Warning messages and leave the upload_images column blank while doing so. Similar to this photoshop mock up on id 123.

enter image description here

Also instead of dumping the entire script in here, because I'm resizing the images and the code is a little lengthy I added it to pastebin. You can find the link here.

CODE LINK

I would appreciate any solution. Thanks for your time.

Upvotes: 2

Views: 1663

Answers (1)

Shail Paras
Shail Paras

Reputation: 1163

Make a little change on your script-

Change

if (isset($_FILES['image'])) { // line : 17

To

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

[UPDATED-1]

Can you do this?

$datatobeinserted = array();

if (!empty($_FILES['image']['name']) && !empty($_FILES['image']['tmp_name'])) {
    // If you have added any image on your form field then    
    // DO the all your work here and add image into the array

        $upload_image           =       $upload_image;
        $datatobeinserted['upload_image'] = str_replace("../images/users/".escape($user->data()->username)."/classifieds/", "", $upload_image);



    // with that you can skip image upload part if you have added only text

}


$datatobeinserted['userid']                 =       $user->data()->id;
$datatobeinserted['title']                  =       Input::get('title');
$datatobeinserted['posted']                 =       date('Y-m-d H:i:s');
$datatobeinserted['description']            =       Input::get('description');

$insertdata = DB::getInstance()->insert('records', $datatobeinserted);

[UPDATED-2]

Sorry for delay-

I have checked your complete code and i found lots of if else and your text is not uploading because you have put all your insert code inside the image_upload block there is no any other block for insert.

Check this i have some changes.

Pastebin

You should do this like this example:

<?php
   if (!empty($_FILES['image']['name']) && !empty($_FILES['image']['tmp_name']))
   {
     // do your all image upload work here
      $datatobeinserted['image'] = $yourimagename // this should be after image upload
   }

   //Add all your data in array

    $datatobeinserted['userid']                 =       $user->data()->id;
    $datatobeinserted['title']                  =       Input::get('title');
    $datatobeinserted['posted']                 =       date('Y-m-d H:i:s');
    $datatobeinserted['description']            =       Input::get('description'); 
    $insertdata = DB::getInstance()->insert('records', $datatobeinserted);    



   if($insertedData)
    {
       echo "Added Successfully"
    } else {
      echo "Something went wrong"; 
    }
?>

Insert Query should not be inside any if else. And if you really want to that then you should check for both (Text && Image) should not be empty for Insert Query.

Hope now you can understand what i'm trying to say.

Thanks.

[UPDATE-4]

Here is how you can manage it all -

if(empty(Image name))
{
   //HERE YOU SHOULD FOR THE $_POST
   // check here for both text fields
   if(!empty($_POST['description']) && !empty($_POST['title'])
   {
      //put your all stuff here with insert query for text only
   }

} else if(!empty(Image name)) {
  // do your image stuff
}

Upvotes: 1

Related Questions