Hello! I need to use the $ name variable in the code, how to get it out of the function? And how to write in an external file? Thank.

function LoadFoto($foto){ $type = $foto['type']; $name = $foto['name']; $uploaddir = "avatars/"; $name = md5(microtime()).".".substr($type, strlen("image/")); if(move_uploaded_file($foto['tmp_name'], $uploaddir.$name)) { return true; } else return false; } 

    2 answers 2

    return $ name instead of return true or $ name = functionName ($ type), and in an external program also call this function

      Var. 1 - pass it to the function by reference:

       $theName = ''; $theFoto = {...}; $result = LoadFoto( $theFoto, $theName); // используем уставновленное из функции значение $theName ... function LoadFoto( $foto, &$name){ ... $name = 'trololo'; return TRUE; } 

      Var. 2 - return an object or array:

       $response = LoadFoto( $foto); if( $response['result']) { echo $response['name']; } ... function LoadFoto($foto){ ... return array( 'result'=>TRUE, 'name'=>$name); }