douzai8285 2014-01-09 09:17
浏览 28

too long

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线