将图像与表单中的其他数据一起上载

时间:2014-08-13 05:25:56

标签: php codeigniter image-uploading

我试图在同一个表格中插入图像,保存所有其他数据。我无法上传图像并检查其验证。这就是MODEL的代码。请给我一个解决方案吗?

<?php  
    class Dhani_model extends CI_Model {  

        function dhani_model()  
        {  
            // Call the Model constructor  
            //parent::Model();  
        }  

        $targetpath='\images';
        $date=date("Y-m-d-H-i-s");

        $targetpath=$targetpath .$date. basename($_FILES['images']['name']);
        move_uploaded_file($_FILES['images']['tmp_name'], $targetpath);

        public function insertData($postdata)  
        {  
            $r=$postdata['name'];
            $s=$postdata['phone'];
            $p=$postdata['address'];
            $a=$postdata['days'];
            $b=$postdata['email'];
            $c=$postdata['password'];
            $images=$postdata['$targetpath'];



            $data = array(
                    'name' => $r ,
                    'phone'=> $s ,
                    'address' => $p ,
                    'days' => $a,
                    'email' => $b ,
                    'password'=> $c ,
                    '$targetpath'=> $images,

             );
            $query=$this->db->insert('registration', $data);
            if($query)
            return $query;
          }
 }
?>

2 个答案:

答案 0 :(得分:0)

我认为,它可以通过声明你的$ targetpath变量并在函数内部跟随移动操作来解决。请尝试一下。

答案 1 :(得分:0)

<?php  
class Dhani_model extends CI_Model {  

    public function __construct()
    {
        parent::__construct();

    }

    public function do_upload()
    {
        $targetpath='\images';
        $date=date("Y-m-d-H-i-s");

        $targetpath=$targetpath .$date. basename($_FILES['images']['name']);
        move_uploaded_file($_FILES['images']['tmp_name'], $targetpath);
    }

<?php  
class Dhani_model extends CI_Model {  
    private $targetpath='\images';
    private $date=date("Y-m-d-H-i-s");

    public function __construct()
    {
        parent::__construct();

    }

    public function do_upload()
    {
        $targetpath=$this->targetpath .$this->date. basename($_FILES['images']['name']);
        move_uploaded_file($_FILES['images']['tmp_name'], $this->targetpath);
    }
  

PHP Class

相关问题