如何将普通的php函数转换为codeigniter函数

时间:2016-05-01 04:28:32

标签: php

这是在codeigniter中使用的普通php函数,它从android设备上传图片。我希望有人帮助转换为codeigniter功能,使其工作。

private function uploadImages(){

    //get the data from the $_POST and $_FILES
    $postKeyPairs = $_POST;
    $files = $_FILES['files'];

    $titles = "";$description = "";

    //fetch the titles
    if(array_key_exists('title',$postKeyPairs)){
        $titles = $postKeyPairs['title'];
    }
    //get the description
    if(array_key_exists('description',$postKeyPairs)){
        $description = $postKeyPairs['description'];
    }

    //the root file path for the uploaded images
    $file_path = "uploads/";

    //count the number of files to upload
    for($i = 0; $i < count($files['name']); $i++){

        //make sure the filename is unique
        $filename = basename($files['name'][$i]);

        //get the proper filepath for the individual files
        $file_path = $file_path.$filename;

        //get the title for the current image
        if(isset($titles[$i]) && $titles[$i] != null){
            $imageTitle = $titles[$i];
        }
        else{
            $imageTitle = "NA";
        }

        //and get the image description
        if(isset($description[$i]) && $description[$i] != null){
            $imageDescp = $description[$i];
        }
        else{
            $imageDescp = "NA";
        }

        //upload the file into the file system
        if(!move_uploaded_file($files['tmp_name'][$i], $file_path)){
            //any exception occurs
            throw new Exception("Couldn't upload image ".$files['name'][$i]);
        }
    }

    return true;
}

0 个答案:

没有答案