Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-生成具有随机名称的文件?_Php_Download - Fatal编程技术网

PHP-生成具有随机名称的文件?

PHP-生成具有随机名称的文件?,php,download,Php,Download,如何创建一个脚本,从具有随机名称的表单数据生成txt文件,并允许临时下载?您可以使用php函数生成随机名称 $content = "some text here"; $fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/". uniqid() .".txt","wb"); fwrite($fp,$content); fclose($fp); 使用以下命令创建文件: $fileName = tempnam( $yourPath ); 将创建日期存储在某个位置(数据

如何创建一个脚本,从具有随机名称的表单数据生成txt文件,并允许临时下载?

您可以使用php函数生成随机名称

$content = "some text here";
$fp = fopen($_SERVER['DOCUMENT_ROOT'] . "/". uniqid() .".txt","wb");
fwrite($fp,$content);
fclose($fp);
使用以下命令创建文件:

$fileName = tempnam( $yourPath );
将创建日期存储在某个位置(数据库、文本文件)


每次请求脚本时,请检查日期是否已超过您的时间限制。如果是,请使用
unlink

tempnam函数删除文件,该函数将生成一个随机名称为的文件。

生成一个名为
download.PHP的PHP脚本

下载链接/表单操作可以是
FDSFASFA.txt
,从
htaccess
发送到
download.php?id=FDSFASFA

在download.php中,输出如下内容

header('Content-type: application/txt');
header('Content-Disposition: attachment; filename="FDSFASFA.txt"');
echo $content;

您将
FDSFASFA
令牌存储在数据库中,并在几分钟后将其删除。

这个问题实现了很多事情。要创建一个随机名称文本文件,您可以使用如下内容:

 /**
 * @param $file string full path to temp file
 * @param $content string to be written to the file
 * @return bool
 */
function writeContentToFile($file, $content)
{
    //mode 'a' - open to write - pointer to end - if not exists ->try to create
    $handle = fopen($file, 'a');

    //check if creation was successful
    if (!$handle) {
        $error = "Cannot open file ($file)";
        return false;
    }

    if (is_writable($file)) {
        //try to write content
        if (!fwrite($handle, $content)) {
            $error = "Cannot write to file ($file)";
            return false;
        }
        //close the file
        fclose($handle);
        return true;
    } else {
        $error = "The file $filename is not writable";
        return false;
    }
}

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

$yourPath="your/path/to/";
$file=$yourPath.generateRandomString().".txt";
$content="content";
writeContentToFile($file,$content);
function initDownload($file)
{
    if (file_exists($file)) {
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file);
        header("Content-Type: text/plain");
        header("Content-Transfer-Encoding: binary");

        //actually download
        readfile($file);

        //delete the file since you said temporaly
        unlink($file);
        return "";
    }
}