【问题标题】:Change auto generated filename更改自动生成的文件名
【发布时间】:2015-04-16 01:24:35
【问题描述】:

所以我有这个 PHP 脚本,它链接到我的一个客户网站上的联系表格。 PHP 脚本所做的基本上是获取所有字段和值,并使用表单中给出的信息生成一个 .txt 文件。之后,脚本会根据您在字段 'forefternamn' 中添加的信息来决定文件的名称。

我的问题很直接。我如何不以字段命名文件,而是用序列号命名?

我希望序列号在第一次提交时以 1000 开头,然后我希望它继续使用 1001、1002、1003、1004 等。

所以文件看起来像这样:

1000.txt 1001.txt 1002.txt 1003.txt 1004.txt 等等……

这是我正在使用的 PHP 脚本:

<?php
$nyckel = array(
'privatforetag',
'forefternamn', 
'foretagsnamn', 
'gatuadress', 
'mobil', 
'telefon', 
'e-post', 
'epost', 
'kalender-tidig', 
'kalender-senast', 
'flyttar-fran-gatuadress', 
'flyttar-fran-portkod', 
'flyttar-fran-postadress', 
'boendetyp', 
'meter', 
'hiss', 
'flyttar-till-gatuadress', 
'flyttar-till-postnummer', 
'flyttar-till-portkod', 
'boendetyp2', 
'meter2', 
'hiss2', 
'rum', 
'personer', 
'kontor', 
'moblerat', 
'boyta', 
'biyta', 
'inventarielista', 
'packning', 
'uppackning', 
'inventarie', 
'antalflyttlador', 
'flyttlada', 
'miljostation', 
'flyttstad', 
'magasinering', 
'student', 
'ovriginfo', 
'rekommenderad', 
'hurhittade'
);
foreach ($nyckel as $key) {
    if ($_POST[$key]) {
        $input .= $_POST[$key]. "\t";
    } else {
        $input .= "-\t";
    }
    $thekey .= $key. "\t";
}
$index = 1;
$name = str_replace(" " , "" , $_POST["forefternamn"]);
$filename =  $name . $index . ".txt";
while (file_exists($filename)) {
    $index++;
    $filename =  $name .$index. ".txt";
}
$handle = fopen($filename, 'w') or die('Cannot open file:  '.$filename);
fwrite($handle, $thekey);
fwrite($handle, PHP_EOL);
fwrite($handle, $input);
fclose($handle);
if (file_exists($filename)) {
 echo "<script>window.location = 'www.domain.com'</script>";
}?>

【问题讨论】:

  • 您是否完全依赖这种方法?因为 IMO 更好的方法是使用散列,即当前时间戳的 md5 散列。但除非你愿意接受,否则我不想提出不同的方法……
  • 或者甚至只使用时间戳(如果您打算使用顺序时间戳,我假设您不需要隐藏 ID)。如果您需要这样做,那么排序等仍然非常容易。

标签: php


【解决方案1】:

创建一个包含主键 ID 字段和名称字段的数据库表。将名称插入表中并检索生成的 ID。使用这个生成的 ID 来命名文件。

【讨论】:

  • 这当然可以,但是通过使用数据库会增加很多不必要的 IO(特别是如果站点还没有使用数据库)。
  • 是的。现在我实际上已经阅读了整个问题,我现在的答案是“使用数据库”。
  • 这行得通,但客户很清楚他们不想将提交的内容保存在数据库中..
【解决方案2】:

怎么样:-

    $dir='c:/temp/inv/';
    $col=glob( $dir . '*.txt' );
    $tmp=array();
    foreach( $col as $file ) $tmp[]=intval( pathinfo( $file, PATHINFO_FILENAME ) );

    $last=intval( max( $tmp ) );
    $next=( $last+1 ).'.txt';

    echo $next;

按照要求继续这个想法,并与“活泼”保持一致?评论...怎么样:-

$nyckel = array(
    'privatforetag',
    'forefternamn', 
    'foretagsnamn', 
    /* rest of the elements removed for brevity */
    'student', 
    'ovriginfo', 
    'rekommenderad', 
    'hurhittade'
);

/* Helper function to generate next invoice id */
function getnextinvid( $path=false ){
    if( $path ){
        $col=glob( $path . '*.txt' );
        $tmp=array();
        foreach( $col as $file ) $tmp[]=intval( pathinfo( $file, PATHINFO_FILENAME ) );
        $last=intval( max( $tmp ) );
        return $path . ( $last+1 ).'.txt';
    }
    return false;
}

/* Loop through array to get POSTed values */
$thekey=$input=array();
foreach( $nyckel as $key ) {
    $input[]=isset( $_POST[ $key ] ) ? $_POST[ $key ] : '-';
    $thekey[]=$key;
}




/* The output filename / invoice should be sequentially numbered */
$output=getnextinvid('c:/temp/inv/');

if( $output ) {
    /* write your data to the output file */
    $bytes=file_put_contents( $output, implode( "\t", $thekey ) . PHP_EOL . implode( "\t", $input ), FILE_TEXT );

    /* Does the new invoice exist? */
    $exists=file_exists( $output ) ? true : false;

    /* good practice to call this after calls to certain functions */
    clearstatcache();

    /* Redirect - alternatively use javascript as originally */
    if( $exists && $bytes ) header('location: http://www.domain.com');

} else {
    echo 'failed';  
}

【讨论】:

  • 你能告诉我这段代码在整个文件中的样子吗?
猜你喜欢
  • 1970-01-01
  • 2019-01-21
  • 2011-10-01
  • 2018-07-18
  • 1970-01-01
  • 2014-08-07
  • 1970-01-01
  • 2013-03-31
  • 2020-11-07
相关资源
最近更新 更多