使用 PHP 脚本自动创建文件

发布于 2024-10-22 04:36:41 字数 167 浏览 5 评论 0原文

我有一个项目需要使用 php 中的 fwrite 创建文件。我想做的是使其通用,我想让每个文件都是唯一的并且不要覆盖其他文件。 我正在创建一个项目,它将记录 php 表单中的文本并将其保存为 html,所以我想输出生成 generated-file1.html 和 generated-file2.html 等。谢谢。

I have a project that needs to create files using the fwrite in php. What I want to do is to make it generic, I want to make each file unique and dont overwrite on the others.
I am creating a project that will record the text from a php form and save it as html, so I want to output to have generated-file1.html and generated-file2.html, etc.. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

反目相谮 2024-10-29 04:36:41

这将为您提供给定目录中 html 文件数量的计数

 $filecount = count(glob("/Path/to/your/files/*.html"));

,然后您的新文件名将类似于:

$generated_file_name = "generated-file".($filecount+1).".html";

然后使用 $ generated_file_name fwrite

虽然我最近不得不做类似的事情并使用 uniq 代替。像这样:

$generated_file_name = md5(uniqid(mt_rand(), true)).".html";

This will give you a count of the number of html files in a given directory

 $filecount = count(glob("/Path/to/your/files/*.html"));

and then your new filename will be something like:

$generated_file_name = "generated-file".($filecount+1).".html";

and then fwrite using $generated_file_name

Although I've had to do a similar thing recently and used uniq instead. Like this:

$generated_file_name = md5(uniqid(mt_rand(), true)).".html";
如梦 2024-10-29 04:36:41

我建议使用时间作为文件名的第一部分(因为这应该会导致文件按时间/字母顺序列出,然后借用 @TomcatExodus 来提高文件名唯一的机会(如果两个提交是 generated_file

<?php
$data = $_POST;
$md5  = md5( $data );
$time = time();
$filename_prefix = 'generated_file';
$filename_extn   = 'htm';

$filename = $filename_prefix.'-'.$time.'-'.$md5.'.'.$filename_extn;

if( file_exists( $filename ) ){
 # EXTREMELY UNLIKELY, unless two forms with the same content and at the same time are submitted
  $filename = $filename_prefix.'-'.$time.'-'.$md5.'-'.uniqid().'.'.$filename_extn;
 # IMPROBABLE that this will clash now...
}

if( file_exists( $filename ) ){
 # Handle the Error Condition
}else{
  file_put_contents( $filename , 'Whatever the File Content Should Be...' );
}

这将产生如下文件名:

  • -1300080525-46ea0d5b246d2841744c26f72a86fc29.htm
  • generated_file-1300092315-5d350416626ab6bd2868aa84fe10f70c.htm
  • generated_file-13001094 56-77eae508ae79df1ba5e2b2ada645e2ee.htm

I would suggest using the time as the first part of the filename (as that should then result in files being listed in chronological/alphabetic order, and then borrow from @TomcatExodus to improve the chances of the filename being unique (incase of two submissions being simultaneous).

<?php
$data = $_POST;
$md5  = md5( $data );
$time = time();
$filename_prefix = 'generated_file';
$filename_extn   = 'htm';

$filename = $filename_prefix.'-'.$time.'-'.$md5.'.'.$filename_extn;

if( file_exists( $filename ) ){
 # EXTREMELY UNLIKELY, unless two forms with the same content and at the same time are submitted
  $filename = $filename_prefix.'-'.$time.'-'.$md5.'-'.uniqid().'.'.$filename_extn;
 # IMPROBABLE that this will clash now...
}

if( file_exists( $filename ) ){
 # Handle the Error Condition
}else{
  file_put_contents( $filename , 'Whatever the File Content Should Be...' );
}

This would produce filenames like:

  • generated_file-1300080525-46ea0d5b246d2841744c26f72a86fc29.htm
  • generated_file-1300092315-5d350416626ab6bd2868aa84fe10f70c.htm
  • generated_file-1300109456-77eae508ae79df1ba5e2b2ada645e2ee.htm
寄人书 2024-10-29 04:36:41

如果您想绝对确保不会覆盖现有文件,您可以附加 uniqid( ) 到文件名。如果您希望它是连续的,则必须从文件系统读取现有文件并计算下一个增量,这可能会导致 IO 开销。

我会使用 uniqid() 方法:)

If you want to make absolutely sure that you will not overwrite an existing file you could append a uniqid() to the filename. If you want it to be sequential you'll have to read existing files from your filesystem and calculate the next increment which can result in an IO overhead.

I'd go with the uniqid() method :)

朕就是辣么酷 2024-10-29 04:36:41

如果您的实现每次都会产生唯一的表单结果(因此是唯一的文件),您可以将表单数据散列到文件名中,为您提供唯一的路径,以及快速筛选重复项的机会;

// capture all posted form data into an array
// validate and sanitize as necessary
$data = $_POST;

// hash data for filename
$fname = md5(serialize($data));

$fpath = 'path/to/dir/' . $fname . '.html';

if(!file_exists($fpath)){

    //write data to $fpath

}

If your implementation should result in unique form results every time (therefore unique files) you could hash form data into a filename, giving you unique paths, as well as the opportunity to quickly sort out duplicates;

// capture all posted form data into an array
// validate and sanitize as necessary
$data = $_POST;

// hash data for filename
$fname = md5(serialize($data));

$fpath = 'path/to/dir/' . $fname . '.html';

if(!file_exists($fpath)){

    //write data to $fpath

}
山川志 2024-10-29 04:36:41

做这样的事情:

$i = 0;  
while (file_exists("file-".$i.".html")) {  
 $i++;  
}
$file = fopen("file-".$i.".html");

Do something like this:

$i = 0;  
while (file_exists("file-".$i.".html")) {  
 $i++;  
}
$file = fopen("file-".$i.".html");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文