Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

So far I'm trying to submit a string via text input and associating it with the current user_id that is logged in.

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'))) {

        $validate = new Validate();
        $validation = $validate->check($_POST, array(
            'url' => array(
                'required' => false
            )
        ));

        if(!$validation->passed()) {

            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($_FILES['url']['name']);

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

                  //Writes the information to the database
                  //$this->data()->query('INSERT INTO userPhotos (photo) VALUES (‘$pic’)');
                  $user->uploadPhoto(array(
                    'url' => Input::get($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.

share|improve this question
    
Try to change $validation = $validate->check($_POST, array( to be $validation = $validate->check($_FILES, array( that's all I can think of, since files are not associated with POSTs but with FILES. –  Fred -ii- Jan 9 at 9:23
    
in your upload phot file in this insert function ( i believe this is inserting into database) only contains userid. $photo = $this->_db->insert('userPhotos', array('user_id' => $this->data()->id)); I think your are not passing the url field here that should be here. –  Learner Student Jan 9 at 9:25
    
I was thinking of that as well @MazIqbal –  Fred -ii- Jan 9 at 9:26
    
Have you tried to die and dump the content to see what details you are actually getting? –  Simon Davies Jan 9 at 9:28
    
There are curly quotes inside a commented out piece of code, btw. //$this->data()->query('INSERT INTO userPhotos (photo) VALUES (‘$pic’)'); Those alone ‘$pic’ will surely cause havoc, and should be replaced with ('$pic') --- Just saying. (If you plan or were trying to use it). –  Fred -ii- Jan 9 at 9:43
add comment

1 Answer

Let's assume you tested if $_FILES['url']['name'] has an actual value. If you did try changing:

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

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

into

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

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

Escpecially

$user->uploadPhoto(array(
'url' => $pic
)); 

You're saving the value of $_FILES['url']['name'] into an own variable and afterwards you try to grab the value via Input::get($pic).

I assume Input::get() is only able to grab $_GET and $_POST, but not $_FILES variables.

If that's not the solution, try to provide more information on how your Input und Validation classes work.

share|improve this answer
    
your right in that he would need to use Input::file() –  Simon Davies Jan 9 at 9:28
    
well, without knowing how his Input class works it's hard to make assumptions on that, isn't it? :D –  Thomas David Plat Jan 9 at 9:29
    
Yet alone not seeing the actual INSERT query. @ThomasDavidPlat too many things could be at play. –  Fred -ii- Jan 9 at 9:29
    
@Fred-ii- , true that. See my last sentence in my answer. But referring to his comments I assume the uploadPhoto() method takes care of inserting the data. –  Thomas David Plat Jan 9 at 9:32
    
If it's Laravel, from what appears to be a "now deleted comment", I know nothing about that framework, if that's the case. TBH, I'm fresh into using classes, but can get around them to a certain extent. However, I can tell that a lot of things are not adding up, and as you said, we need to see more or know more on how the OP is using the class(es). @ThomasDavidPlat –  Fred -ii- Jan 9 at 9:36
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.