我可以在codeigniter中使用此代码上传文件吗?

时间:2015-05-15 09:08:21

标签: php codeigniter upload

$judul = $_POST['judul'];
$idKategori = $_POST['kategori'];
$idPropinsi = $_POST['propinsi'];
$img = $_FILES['img']['name'];
$img_tmp = $_FILES['img']['tmp_name'];
$idUser = $_POST['user'];
$isi = $_POST['isi'];
$date = $_POST['date'];

if(empty($img)) {
    $query = mysql_query("INSERT INTO `artikel`(`idArtikel`, `idKategori`, `idPropinsi`, `judul`, `idUser`, `isi`, `date`) VALUES ('','$idKategori','$idPropinsi','$judul','$idUser','$isi','$date')");
}else{
    if(move_uploaded_file($img_tmp,"../../../img/".$img)) {
        $query = mysql_query("INSERT INTO `artikel`(`idArtikel`, `idKategori`, `idPropinsi`, `judul`, `img`, `idUser`, `isi`, `date`) VALUES ('','$idKategori','$idPropinsi','$judul','$img','$idUser','$isi','$date')");
    }else{
        echo "Failed to upload image";
    }
}

if($query) {
    header("location:../../index.php?page=artikel");
}else{
    echo "failed to update this post";
}

但结果是

  

move_uploaded_file(http://localhost/mvc/kuliner/assets/img/Green自然壁纸04.jpg):无法打开流:HTTP包装器不支持可写连接

3 个答案:

答案 0 :(得分:0)

没有!

基本上重写此代码以使用Codeigniter比使用CI文档中的示例从头开始编写代码要多得多。上面评论中提供的链接(http://www.codeigniter.com/userguide2/libraries/file_uploading.html)。

有几个原因,因为文件上传,重定向,数据库处理,用户输出等在CI中使用CI方法和编程的MVC结构进行了不同的处理。

答案 1 :(得分:0)

这是我在CI中使用Dropzone上传的方式。

创建Dropzone控制器

class Dropzone extends CI_Controller {

public function __construct() {
   parent::__construct();
   $this->load->helper(array('url','html','form')); 
}


public function upload() {
    if (!empty($_FILES)) {
    $tempFile = $_FILES['file']['tmp_name'];
    $temp = $_FILES["file"]["name"];
    $path_parts = pathinfo($temp);
    $t = preg_replace('/\s+/', '', microtime());
    $fileName = $path_parts['filename']. $t . '.' . $path_parts['extension'];
    $targetPath = UPLOADPATH;
    $targetFile = $targetPath . $fileName ;
    echo $fileName;
    move_uploaded_file($tempFile, $targetFile);
    // if you want to save in db,where here
    // with out model just for example
    // $this->load->database(); // load database
    // $this->db->insert('file_table',array('file_name' => $fileName));
    }
}
}
   // Usage : <form action="<?php echo site_url('/dropzone/upload');" class="dropzone"  >

 /* End of file dropzone.js */
 /* Location: ./application/controllers/dropzone.php */

其次添加以下HTML,(主要是表单以便提交文件名

 <div id="upload" class="form-group">
            <label>Drop file Here</label>
            <input type="hidden" id="file" name="file"/>
            <div
                class="dropzone"
                id="uploadFile"><!--uploadFile is the dropzone name-->
            </div>
        </div>

然后有点Js来做魔术

Dropzone.options.uploadFile = {
    paramName: "file", // The name that will be used to transfer the file
    url:"<?php echo site_url('/dropzone/upload');?>",
    maxFiles:1,
    acceptedFiles:'image/*,application/pdf,.docx,.doc,.xls,.xlsx,.csv',
    dictMaxFilesExceeded:"You can only upload one file per Response",
    init: function() {
        this.on("sending", function(file) { 
           // $('button#submit').attr('disabled','');// Requires Jquery
        });
        this.on("complete", function(file) { 
            //$('button#submit').removeAttr('disabled'); // Requires JQuery
        });
        this.on("success", function(file,xhr) { 
            //$('input[type="hidden"]#file').val(xhr); // Requires Jquery
        });
     }
};

如果需要,请记住包含dropzone.js和jquery.js。

答案 2 :(得分:0)

您可以使用 PHP 功能上传文件。但首先,您应该自己制作目录或文件夹。

这是我的代码,我没有添加验证部分。我在根目录中创建了 uploads 文件夹。

$file = $_FILES['verfication_image'];
$file_name_with_extenstion = $file['name'];
$file_name = pathinfo($file_name_with_extenstion, PATHINFO_FILENAME);
$extension = pathinfo($file_name_with_extenstion, PATHINFO_EXTENSION);
$file_tmp_location =$_FILES['verfication_image']['tmp_name'];
$upload_name = $file_name.time().".$extension";
if(move_uploaded_file($file_tmp_location,"uploads/".$upload_name)){
    echo "The file has been uploaded.";
}else{
    echo "There was an error.";
}

我建议使用 Codeigniter 版本上传文件,因为添加验证很简单。我已经添加了带有验证的 Codeigniter 代码,

$file = $_FILES['verfication_image'];
$file_name_with_extenstion = $file['name'];
$file_name = pathinfo($file_name_with_extenstion, PATHINFO_FILENAME);
$extension = pathinfo($file_name_with_extenstion, PATHINFO_EXTENSION);
$upload_name = $file_name.time().".$extension";

$config['upload_path'] = 'uploads/';
$config['file_name'] = $upload_name;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$config['max_width'] = 1024;
$config['max_height'] = 768;

$this->load->library('upload', $config);//Load the libary with the configuration

if(!$this->upload->do_upload('verfication_image')){
    echo($this->upload->display_errors());//validation error will be printed
}else{
    echo "The file has been uploaded.";
}

更多详情请参考官方指南File Uploading class

相关问题