从php中的私有函数中调用函数

时间:2016-06-27 21:48:43

标签: php

我正在使用jQuery Filer。在class.uploader.php页面中有这个私有函数:

private function prepareFiles(){

$user_id = $_SESSION['user_id'];

function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }

    $field = $this->field;
    $validate = $this->validate();

    if($validate){
        $files = array();
        $removed_files = $this->removeFiles();

        for($i = 0; $i < count($field['name']); $i++){

            $metas = array();

            if($field['Field_Type'] == 'input'){
                $tmp_name = $field['tmp_name'][$i];
            }elseif($field['Field_Type'] == 'link'){
                $link = $this->downloadFile($field['name'][0], false, true);

                $tmp_name = $field['name'][0];
                $field['name'][0] = pathinfo($field['name'][0], PATHINFO_BASENAME);
                $field['type'][0] = $link['type'];
                $field['size'][0] = $link['size'];
                $field['error'][0] = 0;
            }



            $metas['extension'] = substr(strrchr(strtolower($field['name'][$i]), "."),1);
            $metas['type'] = preg_split('[/]', $field['type'][$i]);
            $metas['extension'] = $field['Field_Type'] == 'link' && empty($metas['extension']) ? $metas['type'][1] : $metas['extension'];
            $metas['old_name'] = substr($field['name'][$i], 0, -(strlen(strrchr($field['name'][$i], $metas['extension']))+1));
            $metas['size'] = $field['size'][$i];
            $metas['size2'] = $this->formatSize($metas['size']);
            $metas['name'] = $this->generateFileName($this->options['title'], array('name'=>$metas['old_name'], 'size'=>$metas['size'])) . (!empty($metas['extension']) ? "." . $metas['extension'] : "");
            $metas['file'] = $this->options['uploadDir'] . $user_id . "_" . $metas['name'];
            $metas['replaced'] = file_exists($metas['file']);
            $metas['date'] = date('r');

            if(!in_array($field['name'][$i], $removed_files) && $this->validate(array_merge($metas, array('index'=>$i, 'tmp'=>$tmp_name))) && $this->uploadFile($tmp_name, $metas['file'])){
                if($this->options['perms']) @chmod($metas['file'], $this->options['perms']);

                $custom = $this->_onUpload($metas, $this->field); if($custom && is_array($custom)) $metas = array_merge($custom, $metas);

                ksort($metas);

                $files[] = $metas['file'];
                $this->data['data']['metas'][] = $metas;   
            }
        }

        $this->data['isSuccess'] = count($field['name']) - count($removed_files) == count($files);
        $this->data['data']['files'] = $files;

        if($this->data['isSuccess']) $custom = $this->_onSuccess($this->data['data']['files'], $this->data['data']['metas']);

        $this->data['isComplete'] = true;
        $custom = $this->_onComplete($this->data['data']['files'], $this->data['data']['metas']);
    }

    return $this->data;
}

我添加了一行:

 $user_id = $_SESSION['user_id'];

并修改此行以包含它:

 $metas['file'] = $this->options['uploadDir'] . $user_id . "_" . $metas['name'];
        $metas['replaced'] = file_exists($metas['file']);

我想在user_id之后但在文件名

之前添加它
 function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }

但由于某种原因,它不能在私有函数内部或外部识别此函数。如何让private函数识别函数generateRandomString()的输出?

我认为以下内容可行:

 $metas['file'] = $this->options['uploadDir'] . $user_id . "_" . $randomString . "_" . $metas['name'];
        $metas['replaced'] = file_exists($metas['file']);

但它没有“看到”$ randomString

1 个答案:

答案 0 :(得分:0)

我忘了打电话给这个功能,虽然Chay22的回应也会起作用。