开发者

Create Files Automatically using PHP script

开发者 https://www.devze.com 2023-02-16 09:52 出处:网络
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 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开发者_Python百科 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.


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";


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


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 :)


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

}


Do something like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消