Jump to content

PHP OOP Upload file & submit to database with user_id


Snitz

Recommended Posts

I'm trying to allow my users to upload a profile picture and display it on their profile.

I've created profilepic.php and added a new function in classes/User.php

 

The function inside 'classes/User.php'

public function uploadPhoto($fields = array()) {

    $photo = $this->_db->insert('userPhotos', array('user_id' => $this->data()->id));
    if(!$photo) {
        throw new Exception('There was a problem creating your account.');
    }
}

profilepic.php

<?php
require_once 'core/init.php';
include('includes/header.php');
$user = new User();

if(!$user->isLoggedIn()) {
    Redirect::to('index.php');
}

if(Input::exists()) {
    if(Token::check(Input::get('token'))) {


        $pic = ($_FILES['url']['name']);
        if($pic) {

            try {
               /* $user->uploadPhoto(array(
                    'url' => Input::get('url'),
                )); */
                   //This is the directory where images will be saved
                  $target = 'var/www/app/img/';
                  $target = $target . basename($pic);


                  var_dump($pic);

                  //Writes the information to the database
                  //$this->data()->query('INSERT INTO userPhotos (photo) VALUES (‘$pic’)');
                  $user->uploadPhoto(array(
                    'url' => $pic
                  ));

                  //Writes the photo to the server
                  /* if(move_uploaded_file($_FILES['url']['tmp_name'], $target))
                  {
                        echo 'Ok' . basename( $_FILES['uploadedfile']['name']);
                  }
                  else {
                        //Gives and error if its not
                        echo 'Sorry, there was a problem uploading your file.';
                  } */

                //Session::flash('home', 'Your profile photo has been uploaded.');
                //Redirect::to('index.php');

            } catch(Exception $e) {
               die($e->getMessage()); 
            }

        } else {
            foreach($validation->errors() as $error) {
                echo $error, '<br>';
            }
        }
    }
}
?>
    <div class="container">

          <div class="row row-offcanvas row-offcanvas-right">

            <div class="col-xs-12 col-sm-9">
              <p class="pull-right visible-xs">
                <button type="button" class="btn btn-primary btn-xs" data-toggle="offcanvas">Toggle nav</button>
              </p>
              <div class="jumbotron">
                <h1>Edit Profile</h1>
                <p>
                <form action="" method="post" enctype="multipart/form-data">


                    <div class="form-group">
                      <label for="url">URL</label>
                      <input type="file" id="url" name="url" value="">
                      <p class="help-block">Example block-level help text here.</p>
                      <input type="submit" value="Update">

                      <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
                    </div>
                </form>
                </p>
              </div>
              <div class="row">

              </div><!--/row-->
            </div><!--/span-->

            <?php include 'includes/sidebar.php'; ?>
          </div><!--/row-->

          <hr>

    <?php include('includes/footer.php'); ?>

userPhotos table has 4 fields: id, user_id (relationship with id in users table), url, date

users table has 6 fields: id, username, password, salt, name, group

 

So far if I navigate to profilepic.php select a picture and click upload, it will display a success message that my photo has been uploaded. I look into the database, only the row id (auto_increment) and my id (logged_in user id) are being submitted while the "url" field which is supposed to hold the name of the image is not registering.

 

If I disable the db query and redirection methods and only var_dump($pic) I get

string '3W2z8RK.jpg' (length=11)

So I guess there is a value but it's not registering in the db.

Link to comment
Share on other sites

In the uploadPhoto method you're not using $fields anywhere, You set the user_id, but nothing for the url field.

public function uploadPhoto($fields = array()) {

    $photo = $this->_db->insert('userPhotos', array('user_id' => $this->data()->id));
    if(!$photo) {
        throw new Exception('There was a problem creating your account.');
    }
}

I assume you need to add the url field as the second param of insert method, eg

$photo = $this->_db->insert('userPhotos', array('user_id' => $this->data()->id, 'url' => $fields['url']));
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.