繁体   English   中英

CodeIgniter上传文件

[英]CodeIgniter uploading files

我正在努力用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();

我不知道自己在做什么错我应该删除htaccess文件吗?

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

我应该检查文件权限吗,这行可能是CodeIgniter文件夹的att文件夹子级错误。 我为此文件夹添加了完全控制权。 在控制器中,当我print_r $_FILES['file']['name'] ,我得到结果,但是在文件夹中却什么也没有???

尝试这样的事情:

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

尝试这样的事情:

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

$this->load->library('upload', $this->config);
if($this->upload->do_upload()){ 
    echo "file upload success"; }
else{
    echo "file upload failed";
}
<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 = "";
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM