将表单和文件提交给控制器codeigniter

时间:2016-05-23 11:54:20

标签: php html forms codeigniter curl

我是codeigniter和MVC模型的新手 我想知道我如何将表单和文件上传到控制器并使用curl上传 并将确认结果返回到视图

这是我的看法:                  编辑问题             

            <form class="editissueform">
            <h7>Name : </h7>
            <input type="text" name="issue_name"><br>
            <h7>Tagline:</h7>
            <input type="text" name="issue_tagline"><br>
            <h7>Description:</h7>
            <input type="text" name="issue_description"><br>
            <h7>Publish Date:</h7>
            <input type="text" name="issue_publish_on"><br>
            </form>                 
        </div>
        <h4>Upload Publication</h4>
        <div class="issuedit">
            <form class="editissueform">
            <input type="file" name="issue_file" size="40" />
            <h7>Please Upload using pdf file format</h7>
            </form>
        </div>
        <br>
        <br>
        <input type="submit" name="submit" value="upload">
        <?php echo form_close(); ?>

这是我的控制者:

$issue_name = $this->input->post('issue_name');
  $issue_tagline = $this->input->post('issue_tagline');
  $issue_description = $this->input->post('issue_description');
  $issue_publish_on = $this->input->post('issue_publish_on');
  $issue_file = $this->input->post('issue_file');

$data2=array(
  'issue_name' => $issue_name,
  'issue_tagline' => $issue_tagline,
  'issue_description' => $issue_description,
  'issue_file' => $issue_file
  ); 

$this->load->helper('form');
$this->load->view('issue_detail',$data2);


  echo $issue_name;

  $target_url = 'https://platform.twixlmedia.com/admin-api/1/upload';

  $file_name_with_full_path = realpath($issue_file);
  $post3 = array(
'admin_api_key'    => 'da06751194bc1xxxxxxxxxxxx',
'app_key'          => 'bd7cf04226c587xxxxxxxxxxx',
'issue_identifier' => $productid,
'issue_file'       =>'@' . realpath($issue_file),
'issue_name'       => $issue_name
  );

$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL, 'https://platform.twixlmedia.com/admin-api/1/upload');
curl_setopt($ch3, CURLOPT_POST, 1);
curl_setopt($ch3, CURLOPT_POSTFIELDS, $post3);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER, 1);
$result3 = curl_exec($ch3);
curl_close ($ch3);
echo $result3;

如何将表单输入传递给控制器​​并将其上传到服务器api? 感谢

1 个答案:

答案 0 :(得分:0)

您的HTML <form>标记缺少属性:

<form class="editissueform" action="upload.php" method="post" enctype="multipart/form-data">
  • 动作
  • 方法
  • 是enctype

调试更新

您的HTML表单

<form class="editissueform" action="www.example.com/process/curl" method="post" enctype="multipart/form-data">
    <!-- all of your form stuff -->
</form>

process.php控制器

<?php

class Process extends CI_controller
{
    public function curl()
    {
        /**
         *
         * Does this output what you expect?
         *
         */

        echo '<pre>'.print_r($this->input->post(), true).'</pre>'

        die();

        // All of your curl code
    }
}