Search code examples
phpstrpos

strpos() expects parameter 1 to be string, array given


I'm getting this warning on my upload function and I just can't figure out why. I've looked at other questions of the same error but cannot get a solution.

function upload($name,$old,$dir=NULL,$autoid,$number)
{
    global $def_dir;
    if(!$dir)$dir="../".$GLOBALS["logo"];


    if($_FILES[$name]['name']){
        if( is_file($dir.$old) ) unlink($dir.$old);
        $ext=substr($_FILES[$name]['name'],strpos($_FILES[$name]['name'],$name),strlen($_FILES[$name]['name']));
        $file=$autoid.'_'.$number.$ext;

        move_uploaded_file($_FILES[$name]['tmp_name'],$dir.$file);

    }else{
        $file=$old;
    }

    return($file);
}

I've tried var_dump($_FILES[$name]['name'] and the result was string(24) "generic-project-icon.png"

I've tried print_r($_FILES[$name]['name']) and the result was generic-project-icon.png

am I being blind or is there no array there?

I just cannot figure out why this is happening. Any help is much appreciated. Thanks.


Solution

    1. function signature ($name,$old,$dir=NULL,$autoid,$number) parameters with default value move to end ($name,$old,$autoid,$number,$dir=NULL)
    2. try don't use global variables pass them as parameter

    And answer your question $_FILES[$name]['name'] can be array when you upload multiple files. Documentation