CodeIgniter上传文件

时间:2014-10-12 16:47:21

标签: php codeigniter upload

我很想用codeIgniter上传文件。我做错了什么,有人可以向我解释文件首映,以及如何在Windows上更改它。我应该只设置我上传文件的文件夹或专利文件夹。我的代码格式为

<div id="contact_box">
    <form id="form" enctype="multipart/form-data" name="form" action="<?php echo site_url('admin/clients/add_client');?>"  method="post" >
         Client Name: </br><input type="text" name="name" id="name"/> </br></br>
         LOgo: <input type="file" name="file" id="file"/> </br></br>

         <input type="submit"/>
    </form>    
</div>

和控制器

$this->load->library('upload');
$config['upload_path'] = './att/';
$config['allowed_types'] = 'png|jpg|bmp';
$this->upload->initialize($config);
$upload = $this->upload->do_upload();

我不知道我做错了什么 我应该atdust htaccess文件吗?

$config['upload_path'] = './att/';

是否可能是这行错误的CodeIgniter文件夹的att文件夹子 我应该检查文件前提。我为此文件夹添加了完全控制权。 在控制器中我得到i print_r $_FILES['file']['name']时的结果,但文件夹中没有任何内容???

3 个答案:

答案 0 :(得分:0)

尝试这样的事情:

$this->upload->do_upload('file'); #The field name of the file you are trying to upload

答案 1 :(得分:0)

尝试这样的事情:

$ this-&gt; config = array(

              'upload_path'     => dirname($_SERVER["SCRIPT_FILENAME"])."/att/",
              'upload_url'      => base_url()."att/",
              'allowed_types'   => "png|jpg|bmp"

            );

$ this-&gt; load-&gt; library(&#39; upload&#39;,$ this-&gt; config);

如果($这 - &GT; upload-&GT; do_upload())

{echo&#34;文件上传成功&#34 ;; }

否则 {echo&#34;文件上传失败&#34 ;;}

答案 2 :(得分:0)

<input type="file" name="userfile1" class="form-control" />
<input type="file" name="userfile2" class="form-control" />

如果要多次使用它,请换个底盖等等

$filename1 ="";
$filename2 ="";
        if(isset($_FILES['userfile1']['name'])){
            $rand = rand(111111,999999);
            $target_dir = "files/img/examination/";
            $target_file = $target_dir . basename($rand."-".$_FILES["userfile1"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            // Check if image file is a actual image or fake image
            if(isset($_POST["submit"])) {
                $check = getimagesize($_FILES["userfile1"]["tmp_name"]);
                if($check !== false) {
                    //echo "File is an image - " . $check["mime"] . ".";
                    $uploadOk = 1;
                } else {
                    echo "File is not an image.";
                    $uploadOk = 0;
                }
            }
            // Check if file already exists
            if (file_exists($target_file)) {
            //  echo "Sorry, file already exists.";
                $uploadOk = 0;
            }
            // Check file size
            if ($_FILES["userfile1"]["size"] > 10000000) {
                //echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }
            // Allow certain file formats
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif" ) {
                //echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                $uploadOk = 0;
            }
            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                //echo "Sorry, your file was not uploaded.";
            // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["userfile1"]["tmp_name"], $target_file)) {
                    $filename1 = $rand."-".$_FILES["userfile1"]["name"]; 
                    //echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
                } else {
                    //echo "Sorry, there was an error uploading your file.";
                }
            }

          }else{
            $filename1 = "";
        }

        if(isset($_FILES['userfile2']['name'])){
            $rand = rand(111111,999999);
            $target_dir = "files/img/examination/";
            $target_file = $target_dir . basename($rand."-".$_FILES["userfile2"]["name"]);
            $uploadOk = 1;
            $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
            // Check if image file is a actual image or fake image
            if(isset($_POST["submit"])) {
                $check = getimagesize($_FILES["userfile2"]["tmp_name"]);
                if($check !== false) {
                    //echo "File is an image - " . $check["mime"] . ".";
                    $uploadOk = 1;
                } else {
                    echo "File is not an image.";
                    $uploadOk = 0;
                }
            }
            // Check if file already exists
            if (file_exists($target_file)) {
            //  echo "Sorry, file already exists.";
                $uploadOk = 0;
            }
            // Check file size
            if ($_FILES["userfile2"]["size"] > 10000000) {
                //echo "Sorry, your file is too large.";
                $uploadOk = 0;
            }
            // Allow certain file formats
            if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
            && $imageFileType != "gif" ) {
                //echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                $uploadOk = 0;
            }
            // Check if $uploadOk is set to 0 by an error
            if ($uploadOk == 0) {
                //echo "Sorry, your file was not uploaded.";
            // if everything is ok, try to upload file
            } else {
                if (move_uploaded_file($_FILES["userfile2"]["tmp_name"], $target_file)) {
                    $filename1 = $rand."-".$_FILES["userfile2"]["name"]; 
                    //echo "The file ". basename( $_FILES["userfile2"]["name"]). " has been uploaded.";
                } else {
                    //echo "Sorry, there was an error uploading your file.";
                }
            }

          }else{
            $filename2 = "";
        }