如何使用curl codeigniter上传图像?



我有一个使用curl上传照片到另一个服务器的问题。我不知道为什么它不适合我。

public function form_submit()
{
$curl_connection = 
curl_init('http://example.com/form');
$file = 'C:xampphtdocshot.jpg';

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_connection, CURLOPT_POST, true);

$post = array(
"userfile" => "@$file;type=image/jpeg"
);
//vd($post);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($curl_connection);
echo $response;
curl_close($curl_connection);
}

和我的上传功能

function do_upload_post()
{
$config = array(
'upload_path' => "./uploads2/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000" // Can be set to particular file size , here it is 2 MB(2048 Kb)
);
$this->load->library('upload', $config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('form', $data);
}
else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('form', $error);
}
}

和我的表单

<body>

<?php echo form_open_multipart('ads/do_upload_post');?>
<?php echo "<input type='file' name='userfile' size='20' />"; ?>
<?php echo "<input type='submit' name='submit' value='upload' /> ";?>
<?php echo "</form>"?>
</body>

在本地,当我上传文件时,上传功能工作。但作为从外部通过旋不行。

当你使用curl发布图像时你得到了控制器但是你使用的图像参数你也提到了$this->upload->do_upload ('userfile').

多个文件post的curl函数

public function form_submit() {
$curl_connection = curl_init ( 'http://example.com/form' );

$photos = [
'img1.jpg',
'img2.jpg'
];

$files = array();

foreach ( $photos as $key => $photo ) {
$cfile = new CURLFile ( '../' . $photo, 'image/jpeg', $key );
$files['userfile'] [$key] = $cfile;
}

curl_setopt ( $curl_connection, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl_connection, CURLOPT_POST, true );

// vd($post);
curl_setopt ( $curl_connection, CURLOPT_POSTFIELDS, $files );
$response = curl_exec ( $curl_connection );
echo $response;

curl_close ( $curl_connection );
}

控制器功能

function do_upload_post() {
$config = array (
'upload_path' => "./uploads2/",
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "2048000" 
);
$this->load->library ( 'upload', $config );
if($_FILES){
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{           
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];    
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload();
$dataInfo[] = $this->upload->data();
}


} else {
$error = array (
'error' => $this->upload->display_errors () 
);
$this->load->view ( 'form', $error );
}
}

相关内容

  • 没有找到相关文章

最新更新