codeigniter file upload with verot_upload class an

2019-08-29 10:54发布

问题:

I'm trying to get working my upload script.

I'm using CodeIgniter, dropzone.js and Verot_upload class

form:

<form action="/admin/images/upload" 
      enctype="multipart/form-data" method="post"
      class="dropzone"
      id="my-awesome-dropzone"></form>

<script src="/skin/js/dropzone.js"></script>

and /admin/images/upload method

public function upload()
{
    $data = array();
    $this->load->library('verot_upload');
    if ($this->authentication->is_loggedin())
    {
        if (!empty($_FILES))
        {
            //                $tempFile = $_FILES['file']['tmp_name'];
            $foo = new Verot_upload($_FILES['file']);

            if ($foo->uploaded)
            {
                // save uploaded image with no changes
                $foo->Process('./media/test/');

            }
        }

    } else
    {
        redirect('/admin/login/', 'refresh');
    }
}

it works with regular style:

function upload()
{

    if (!empty($_FILES))
    {
        $tempFile = $_FILES['file']['tmp_name'];
        $targetPath = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
        $targetFile = $targetPath . $_FILES['file']['name'];
        move_uploaded_file($tempFile, $targetFile);
        // save data in database (if you like!)
    }
}

But not with verot_upload.

回答1:

So the issue was that, I was trying to upload image as in example at the class initialization. if I initialize empty class and then use upload method everything works.

/**
 * Method for uploading Images from dropdown form.
 *
 * @param $size
 * @param $path
 * @param $file
 */
public function upload_image($size, $path, $file)
{
    $this->load->library('verot_upload');
    $foo = new Verot_upload();

    $foo->upload($file);
    if ($foo->uploaded)
    {
        $foo->image_resize = true;
        $foo->image_x = $size;
        $foo->image_ratio_y = true;
        $foo->Process($path);
        if ($foo->processed)
        {
            $new_path = substr($foo->file_dst_pathname,1);
            $this
                ->db
                ->set('date_created', 'NOW()', false)
                ->set('path', $new_path, true)
                ->insert('wysiwyg_img_uploads');

            $foo->Clean();
        }
    }
}