Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ajax在codeigniter中上载带有图像的表单_Ajax_Codeigniter_File Upload - Fatal编程技术网

使用ajax在codeigniter中上载带有图像的表单

使用ajax在codeigniter中上载带有图像的表单,ajax,codeigniter,file-upload,Ajax,Codeigniter,File Upload,我是codeigniter的新手,正在尝试上载带有文件字段的表单。我成功地将没有文件的表单数据插入数据库。但当我尝试使用文件字段时,它显示以下错误 `<p>You did not select a file to upload.</p>` form_actions.php 我认为必须在较新的浏览器上使用FormData()类才能使其正常工作。看到这个答案了吗 或者使用与此类似的第三方插件,您可以使用以下代码 $(document).ready(function() {

我是codeigniter的新手,正在尝试上载带有
文件
字段的
表单。我成功地将没有
文件的表单数据插入数据库。但当我尝试使用文件字段时,它显示以下错误

`<p>You did not select a file to upload.</p>`
form_actions.php


我认为必须在较新的浏览器上使用
FormData()
类才能使其正常工作。看到这个答案了吗


或者使用与此类似的第三方插件,您可以使用以下代码

 $(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#add_author_form).ajaxForm(function() { 
            alert("Thank you for your comment!"); 
        }); 
    }); 

我希望这对您有所帮助

我使用
上载库在
codeigniter
中执行了以下上载图像的功能

通过使用此功能,我可以添加单个/多个文件

function add_image ( $path, $field ) //upload and the file field name
{
    $config = array (
        'upload_path' => $path,
        'allowed_types' => 'gif|jpg|jpeg|png',
        'max_size' => '1024',
        'max_width' => '1024',
        'max_height' => '1024',
        'encrypt_name' => TRUE
    );
    $this->load->library ( 'upload', $config ); //load codeigniter libraries
    $name_array = array ();
    $count = count ( $_FILES[ $field ][ 'size' ] );
    if ( count ( $_FILES[ $field ] ) == count ( $_FILES[ $field ], COUNT_RECURSIVE ) ) //if only one image is present
    {
        if ( !$this->upload->do_upload ( $field ) )
        {
            return FALSE;
        }
        $data = $this->upload->data ();
        return $data[ 'file_name' ]; //return the uploaded file name
    }
    else //if multiple images selected
    {
        foreach ( $_FILES as $key => $value )
        {
            for ( $s = 0; $s < $count; $s++ )
            {
                $_FILES[ 'userfile' ][ 'name' ] = $value[ 'name' ][ $s ];
                $_FILES[ 'userfile' ][ 'type' ] = $value[ 'type' ][ $s ];
                $_FILES[ 'userfile' ][ 'tmp_name' ] = $value[ 'tmp_name' ][ $s ];
                $_FILES[ 'userfile' ][ 'error' ] = $value[ 'error' ][ $s ];
                $_FILES[ 'userfile' ][ 'size' ] = $value[ 'size' ][ $s ];
                if ( $this->upload->do_upload () )
                {
                    $data = $this->upload->data ();
                    $name_array[ ] = $data[ 'file_name' ];
                }
                else
                {
                    return FALSE;
                }
            }
            return $name_array;//return the names of images uploaded
        }
    }
}
函数add_image($path,$field)//上传和文件字段名
{
$config=array(
“上传路径”=>$path,
“允许的_类型”=>“gif | jpg | jpeg | png”,
“最大大小”=>“1024”,
“最大宽度”=>“1024”,
“最大高度”=>“1024”,
“encrypt_name”=>TRUE
);
$this->load->library('upload',$config);//加载codeigniter库
$name_array=array();
$count=count($_文件[$field]['size']);
if(count($\u FILES[$field])==count($\u FILES[$field],count\u RECURSIVE))//如果只存在一个图像
{
如果(!$this->upload->do_upload($field))
{
返回FALSE;
}
$data=$this->upload->data();
return$data['file_name'];//返回上传的文件名
}
else//如果选择了多个图像
{
foreach($\文件为$key=>$value)
{
对于($s=0;$s<$count;$s++)
{
$\u文件['userfile']['name']=$value['name'][$s];
$\u文件['userfile']['type']=$value['type'][$s];
$\u FILES['userfile']['tmp\u name']=$value['tmp\u name'][$s];
$\u文件['userfile']['error']=$value['error'][$s];
$\u文件['userfile']['size']=$value['size'][$s];
如果($this->upload->do_upload())
{
$data=$this->upload->data();
$name_数组[]=$data['file_name'];
}
其他的
{
返回FALSE;
}
}
return$name_array;//返回上传图像的名称
}
}
}
public function add_author ()
{
    $this -> load -> helper ( 'form' );
    $config = array (
        'upload_path' => './images/author/',
        'allowed_types' => 'gif|jpg|jpeg|png',
        'max_size' => '2000',
        'max_width' => '2000',
        'max_height' => '2000',
        'encrypt_name' => true,
    );

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

    if ( ! $this -> upload -> do_upload ( 'author_image' ) )
    {
        echo $error = $this -> upload -> display_errors ();
    }
    else
    {
        echo $data = $this -> upload -> data ();
    }
}
 $(document).ready(function() { 
        // bind 'myForm' and provide a simple callback function 
        $('#add_author_form).ajaxForm(function() { 
            alert("Thank you for your comment!"); 
        }); 
    }); 
function add_image ( $path, $field ) //upload and the file field name
{
    $config = array (
        'upload_path' => $path,
        'allowed_types' => 'gif|jpg|jpeg|png',
        'max_size' => '1024',
        'max_width' => '1024',
        'max_height' => '1024',
        'encrypt_name' => TRUE
    );
    $this->load->library ( 'upload', $config ); //load codeigniter libraries
    $name_array = array ();
    $count = count ( $_FILES[ $field ][ 'size' ] );
    if ( count ( $_FILES[ $field ] ) == count ( $_FILES[ $field ], COUNT_RECURSIVE ) ) //if only one image is present
    {
        if ( !$this->upload->do_upload ( $field ) )
        {
            return FALSE;
        }
        $data = $this->upload->data ();
        return $data[ 'file_name' ]; //return the uploaded file name
    }
    else //if multiple images selected
    {
        foreach ( $_FILES as $key => $value )
        {
            for ( $s = 0; $s < $count; $s++ )
            {
                $_FILES[ 'userfile' ][ 'name' ] = $value[ 'name' ][ $s ];
                $_FILES[ 'userfile' ][ 'type' ] = $value[ 'type' ][ $s ];
                $_FILES[ 'userfile' ][ 'tmp_name' ] = $value[ 'tmp_name' ][ $s ];
                $_FILES[ 'userfile' ][ 'error' ] = $value[ 'error' ][ $s ];
                $_FILES[ 'userfile' ][ 'size' ] = $value[ 'size' ][ $s ];
                if ( $this->upload->do_upload () )
                {
                    $data = $this->upload->data ();
                    $name_array[ ] = $data[ 'file_name' ];
                }
                else
                {
                    return FALSE;
                }
            }
            return $name_array;//return the names of images uploaded
        }
    }
}