PHP 任何带有字符串的内容


PHP anything with string

我正在制作打开目录并在文件中写入内容的php脚本。目录中有问题。目录名称不是固定的,每次名称"dir +一些随机数"都会更改。

有没有办法制作这样的东西我需要那个函数"任何东西"

<?php
$directory = "files/dir".anything()."/other..";
?>

当然...首先检查所需的目录是否存在。如果没有创建它。

<?php
    // check if dir exists, if not create it
    if(!is_dir("files/dir".anything()."/other.."))
        mkdir("files/dir".anything()."/other..");
    // ... rest of code here
    $directory = "files/dir".anything()."/other..";
?>

函数 anything是dir +一些随机数。只需使用php随机...

// this function returns a random number from 0 to 100000
function random()
{
    echo rand(0, 100000);
}

除非我错过了什么,否则你不能做

function anything(this $str){}

在混淆之后,您似乎想创建一个函数来创建一个唯一的文件名,然后将其应用于目录路径,使用 rand() 不会获得唯一 id,因为 rand() 有时不是那么 rand,而且每次创建新文件时,它都会降低新随机文件的机会。使用 uniqid() 和 md5,它将从 uniqid/microtime 创建一个 128 位字母数字哈希,返回相同哈希的几率非常小甚至没有:

所以你应该做这样的事情:

<?php 
/**
 * Function to create 128bit unique hash string.
 * 
 * @return string
 */
function anything(){
    return md5(uniqid(rand(), true));
}
/*
You should store value in a variable if you need to use it further along
or in the future.
*/
$uniq_token = anything();
$directory = "files/".$uniq_token."/other..";
?>

您需要将$uniq_token存储在数据库中的某个位置,否则在尝试检索 ect 时将丢失对该文件/文件夹的引用。

我在这里错过了什么吗?

WRite 一个函数anything它返回你想要的任何内容。这是非常基本的PHP编码。