Welcome Guest, Not a member yet? Register   Sign In
Some help converting my code to codeigniter
#1

[eluser]Fabdrol[/eluser]
Hi guys!

I've written a js class for Google Gears multiple file uploading, and a corresponding file upload script.

Thing is, gears doesn't send the file via a 'regular' file upload field, but rather as a stream of bytes, ready to be processed.

Below you see my code, but I want to convert it to a CI lib or helper or so, in a way that I can use CI's image manipulation lib to save the images in different sizes and so on.

Anybody knows enough of CI library's to help me out?

Thanks in advance!

Code:
<?php
    // function to catch the error and send it along with a 500 header
    function report_error($title, $msg) {
        header("HTTP/1.1 500 $title");
        echo $msg;
        exit;
    }
    
    // generate a unique md5'ed name for the file
    function uid() {
        mt_srand((double)microtime()*1000000);
        $token = mt_rand(1, mt_getrandmax());
    
        $uid = uniqid(md5($token), true);
        if($uid != false && $uid != '' && $uid != NULL) {
            $uid30 = md5($uid);
        } else {
            $uid30 = 'error';
        }
        
        return $uid30;
    }
    
    // define directory
    define('UPLOAD_DIRECTORY', 'upload/');
    
    // check if writeable
    if (!is_writeable(UPLOAD_DIRECTORY)) {
        report_error("System error", "Dir not writeable");
    }
    
    $filename = '';
    // fetch the headers
    $headers  = array_change_key_case(getallheaders(), CASE_LOWER);
    
    if (!isset($headers['content-disposition']) || !preg_match('~filename="([a-z0-9_\. \-]+)"$~i', $headers['content-disposition'], $matches)) {
        // if it misses the content-disposition header or if it isn't filled with the file's info, go error.
        report_error('System error', 'No file info: ' . $headers['content-disposition']);
    }
    
    // check the file name
    $filename     = basename($matches[1]);
    $nameArr     = explode('.', $filename);
    $total         = count($nameArr);
    $ext        = $nameArr[$total - 1];
    $finalname    = uid() . '.' . $ext;
    
    // write the file (it's still empty)
    $fp_dst = @fopen(UPLOAD_DIRECTORY . $finalname, 'w');
    
    // write error
    if(!$fp_dst) {
        report_error("System error", "Couldn't write file");
    }
    
    // the file stream
    $fp_src = fopen('php://input', 'r');
    $length = 0;
    
    
    // do things below for as long as the stream isn't to its end
    while(!feof($fp_src)) {
        // read the bytes in php://input
        $data = fread($fp_src, 1024);
        
        // catch the length and add it to the bytes read before
        $length += strlen($data);
        
        // write the bytes into the file we created earlier
        fwrite($fp_dst, $data);
    }
    
    // close the file and the byte stream
    fclose($fp_dst);
    fclose($fp_src);
    
    echo "<a >Success!</a>";
?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB