【问题标题】:Uploading an image along with other datas in a form [closed]以表格形式上传图像和其他数据[关闭]
【发布时间】:2014-08-13 05:25:55
【问题描述】:

我试图将图像插入到包含所有其他数据的同一个表中。我无法上传图像并检查其验证。 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;
          }
 }
?>

【问题讨论】:

  • 如果你想正确实现它,你需要一个完整的层来执行它。
  • 这是我运行文件时显示的错误 Parse error: syntax error, unexpected '$targetpath' (T_VARIABLE), expecting function (T_FUNCTION) in C:\xampp\htdocs\bodhi\application\ models\dhani_model.php 第 12 行

标签: php codeigniter image-uploading


【解决方案1】:
<?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

【讨论】:

    【解决方案2】:

    我认为,可以通过声明 $targetpath 变量并在函数内执行移动操作来解决此问题。请尝试这样做。

    【讨论】:

    • 谢谢...它成功了... :)
    • 这真的很好@sreevidya。
    猜你喜欢
    • 2011-04-24
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多