Welcome Guest, Not a member yet? Register   Sign In
[] operator not supported for strings in upload library
#1

[eluser]batterj2[/eluser]
Had a bit of an odd one today which I managed to circumvent by catching errors in the controller.

I was upload multiple files on one page, specifying each upload form with its own specific config. This had been working fine on other parts of the site. Then when I tried out for uploading an image, then a PDF and then an image again, PHP would yelp:
Quote:[] operator not supported for strings in system/libraries/upload.php on line 842

I guess its trying to say that the error_msg array was in fact a string and you could use the [] operator. My first search on Google suggested putting '' in the [] (so ['']) but this caused problems in other parts of the library.

My way around this was to have two booleans in the controller.

One to act as an indication of whether they had tried to upload a file and then another to indicate if the upload was successful. If it was store the upload data, if not the error messages.

Imagine if I had a form with two file fields named file1 and file2:
Code:
$config = array();
$this->load->library('upload', $config);
$uploadData = array('file1', 'file2');
$uploadErrors = array();

//1. File 1
$triedToUploadFile1 = false;
$uploadedFile1 = false;
if(isset($_FILES['file1']) && $_FILES['file1']['name'] != '')
{
    $triedToUploadFile1 = true;
    $uploadedFile1 = $this->upload->do_upload();
    if(!$uploadedFile1)
        $uploadErrors[] = $this->upload->display_errors();
    else
        $uploadData['file1'] = $this->upload->upload_data();
}

//1. File 2
$triedToUploadFile2 = false;
$uploadedFile2 = false;
if(isset($_FILES['file2']) && $_FILES['file2']['name'] != '')
{
    $triedToUploadFile2 = true;
    $uploadedFile2 = $this->upload->do_upload();
    if(!$uploadedFile2)
        $uploadErrors[] = $this->upload->display_errors();
    else
        $uploadData['file2'] = $this->upload->upload_data();
}

if(($triedToUploadFile1 && !$uploadedFile1) || ($triedToUploadFile2 && !$uploadedFile2))
{
    //show form again
}
else
{
    //process form
}




Theme © iAndrew 2016 - Forum software by © MyBB