Drew Peer
Drew Peer

Reputation: 377

Storing filename in db from ajax upload not working

When I use $filename below I can never store anything in the database. I've tried basename function but it doesn't seem to store anything.

        $filename = $_FILES['file']['name'];

        /* Location */
        $location = "../media/images/profile-pics/".$filename;
        $uploadOk = 1;
        $imageFileType = pathinfo($location,PATHINFO_EXTENSION);

        /* Valid Extensions */
        $valid_extensions = array("jpg","jpeg","png");
        /* Check file extension */
        if( !in_array(strtolower($imageFileType),$valid_extensions) ) {
           $uploadOk = 0;
        }

        if($uploadOk == 0){
           echo 0;
        }else{
           /* Upload file */
           if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){


           }else{
              echo 0;
           }
        }

        //$filename = basename($_FILES['file']); removed, but still doesn't store file in db.


        $stmt = $db->prepare("UPDATE contacts SET name = ?, email = ?, phone = ?, jobtitle = ?, profile_pic = ? WHERE id = ?");
        $stmt->bind_param("sssssi", $name, $email, $phone, $jobtitle, $filename, $id);
        $stmt->execute();
        $stmt->close();

Upvotes: 1

Views: 48

Answers (1)

Jason
Jason

Reputation: 13766

Just get rid of $filename = basename($_FILES['file']); - you've already populated $filename with $_FILES['file']['name'] up above, which is the correct way to access the filename of the file as it was uploaded.

Upvotes: 1

Related Questions