Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-OOP上传文件&;提交到具有用户id的数据库_Php_Mysql_Image_Oop_Upload - Fatal编程技术网

PHP-OOP上传文件&;提交到具有用户id的数据库

PHP-OOP上传文件&;提交到具有用户id的数据库,php,mysql,image,oop,upload,Php,Mysql,Image,Oop,Upload,我正在尝试允许我的用户上传一张个人资料图片,并将其显示在他们的个人资料上 我已经创建了profilepic.php,并在classes/User.php中添加了一个新函数 到目前为止,我试图通过文本输入提交一个字符串,并将其与当前登录的用户id相关联 “classes/User.php”中的函数 public function uploadPhoto($fields = array()) { $photo = $this->_db->insert('userPhotos',

我正在尝试允许我的用户上传一张个人资料图片,并将其显示在他们的个人资料上

我已经创建了profilepic.php,并在classes/User.php中添加了一个新函数

到目前为止,我试图通过文本输入提交一个字符串,并将其与当前登录的用户id相关联

“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表有4个字段:id、用户id(与用户表中id的关系)、url、日期 users表有6个字段:id、用户名、密码、salt、名称、组


到目前为止,如果我导航到profilepic.php,选择一张图片并单击上载,它将显示一条成功消息,说明我的照片已上载。我查看数据库,只有行id(自动增量)和我的id(登录的用户id)被提交,而应该保存图像名称的“url”字段没有注册。

尝试更改
$validation=$validate->check($\u POST,array(
$validation=$validate->check($\u文件,array(
这就是我所能想到的,因为文件与帖子无关,而是与文件关联。在这个插入函数(我相信这是插入到数据库中)的上载phot文件中,只包含userid。$photo=$this->\u db->insert('userPhotos',array('user\u id'=>$this->data()->id));我认为您没有在此处传递本应在此处的url字段。我也在考虑@Maziqbal您是否尝试过死亡并转储内容以查看实际获得的详细信息?在注释掉的
代码段中有卷曲的引号,顺便说一句。
/$this->data()->query('INSERT-INTO-userPhotos(photo)值(“$pic”);
那些单独的“$pic”
肯定会造成大破坏,应该用“
”(“$pic”)
”来代替——只是说说而已。(如果你计划或试图使用它)。