如何使用codeigniter添加链接或文件

时间:2017-07-22 07:28:54

标签: php codeigniter file-upload

我有两个字段用于链接,一个用于表单中的文件上传,我如何只添加其中一个,即数据库中的链接或文件, 对不起任何错误的代码和方法,我是这里的新手,这是我在堆栈溢出中的第一个问题。希望大家帮帮我。

我的控制器

public function add_scrolllink()
{

    $this->form_validation->set_rules('stitle','File Title','trim|required');
    $this->form_validation->set_rules('slink','File Link','trim|required');
    if(empty($_FILES['sfile']['name']))
    {
        $this->form_validation->set_rules('slink','File Link','required');
    }



    if($this->form_validation->run() == FALSE)
    {
        $this->load->view("add_scrolllink");
    }
    else
    {
        $slink_id = $this->input->post('stitle');
        $slink_files = $this->input->post('slink');


        $config['upload_path'] = './assets/uploads/';
        $config['allowed_types'] = 'pdf|docx|doc';
        $config['max_size'] = 9000;

        $this->upload->initialize($config);
        $this->load->library('upload',$config);


        if(!$this->upload->do_upload('slink'))
        {
            $msg1 = $this->upload->display_errors();
            header("Location:".base_url()."news/add_scrolllink?msg1=$msg1");
        }
        else
        {
            $output = $this->upload->data();
            $file2 = $output['file_name'];


            $this->load->model('mpages');

            $output1 = $this->mpages->add_scrolllink($slink_title,$file2);
            if($output1)
            {
                $msg = "Scrolling Link added successfully!";
                header("Location:".base_url()."news/scrolllink_list?
msg=$msg");
            }
            else
            {
                $msg1 = 'Scrolling Link add failed. Try again!';
                header("Location:".base_url()."news/add_scrolllink?
msg1=$msg1");
            }
        }   
    }

//My View is

<form action="<?php echo base_url(); ?>news/add_scrolllink" method="post" 
role="form" enctype="multipart/form-data">
    <div class="row">
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <div class="panel panel-default">
                <div class="panel-body">
                    <div class="form-group">
                        <label>Scrolling Link Title</label>
                        <input type="text" name="stitle" class="form-
control" value="<?php echo set_value('stitle'); ?>" placeholder="Enter 
Scrolling Link Title">
                    </div>
                    <style type="text/css">
                        p.help-block{color:#ff0000;}
                    </style>
                     <div class="form-group">
                        <label>Scrolling Link</label>
                        <input type="text" name="slink" class="form-control" 
value="<?php echo set_value('slink'); ?>" placeholder="Enter Scrolling 
Link">
                    </div>
                    OR ADD FILE TO SCROLL LINK
                    <div class="form-group">
                        <label>Choose File to Upload</label>
                        <input type="file" name="slink" class="form-
  control">
                    </div>
                    <input type="submit" name="submit" class="btn btn-
 primary" value="Add Link"><br>
                    <?php echo "<br><span 
style='color:#ff0000;'>".validation_errors()."</span>"; ?>
                    <?php
                    if(isset($_GET['msg'])){
                        echo "<div class='alert alert-success 
fadein'>".$_GET['msg']."</div>";
                    }
                    if(isset($_GET['msg1'])){
                        echo "<div class='alert alert-danger 
fadein'>".$_GET['msg1']."</div>";
                    }
                    ?>
                </div>
            </div>
        </div>
        <div class="col-md-2"></div>
        <div class="clearfix"></div>
     </div>
     </form>

1 个答案:

答案 0 :(得分:0)

您已使用jquery使用此代码获取输入框值并设置一些条件

$('#slink-id').val(); // set link field input box id
$('#slinkFile-id').val(); //set upload file field id
if($('#slink-id').val() != '' && $('#slinkFile-id').val() == ''){
    // setread only your file field
}else{
  //  set read only link field
}