如何使用php增加从输入数据创建的文件名?

时间:2023-01-27 10:57:49

I created a script for saving a form submission as a .txt file that uses the user's first name as the file name. To keep it simple I created a test file in HTML to show what I'm trying to achieve.

我创建了一个脚本,用于将表单提交保存为.txt文件,该文件使用用户的名字作为文件名。为了简单起见,我在HTML中创建了一个测试文件,以显示我想要实现的目标。

<form action="sender.php" method="post">

    <input type="text" id="f_name" name="First_Name" />
    <input type="text" id="demo_1" name="Demo_01" />
    <input type="text" id="demo_2" name="Demo_02" />
    <input type="text" id="demo_3" name="Demo_03" />
    <input type="text" id="demo_4" name="Demo_04" />
    <input type="text" id="demo_5" name="Demo_05" />
    <input type="text" id="demo_6" name="Demo_06" />



    <input type="submit" value="submit" />

</form>

So far the script I have for processing the form in PHP is this

到目前为止,我用PHP处理表单的脚本是这样的

<?php ini_set('display_errors','on'); ?><?php


$data= "";

foreach ($_POST as $key => $value) {
$data.= str_replace("_"," ",$key)."\n\n ". $value."\n\n\n\n"; preg_replace("/[^ 0-9a-zA-Z]/", "_", $value);
}


$fileName= fopen("Submissions/".$_POST['First_Name'],'w');

fwrite($fileName, $data);
fclose($fileName);

?>

What I want to do is add some code to make it increment the file name so in the event of me getting multiple submissions from people with the same first name it won't over write them. Do say I have a submission from somebody named Bob, and two more Bobs happens to fill out the questionnaire, I want it to save as

我想要做的是添加一些代码以使其增加文件名,这样如果我从具有相同名字的人那里获得多个提交,它将不会过度编写它们。请说我有一个名叫鲍勃的人的提交,还有两个Bobs碰巧填写问卷,我希望它保存为

Bob.txt Bob_02.txt Bob_03.txt

Bob.txt Bob_02.txt Bob_03.txt

This means I need something that will be able to ignore the "_02" at the end to identify the "Bob" at the beginning so it doesn't go

这意味着我需要一些能够忽略最后“_02”的东西来识别开头的“Bob”,所以它不会去

Bob.txt Bob_02.txt Bob_02_02.txt

Bob.txt Bob_02.txt Bob_02_02.txt

I came up with this in an attempt to do just that but got errors on both of the "file_exists" opperators

我试图做到这一点,但是在两个“file_exists”操作符上都出错了

if (file_exists($fileName)){
$num="00";
$fileNameUpd= count(substr.$_POST['First_Name'].$num++);

fopen("Submissions/".$fileNameUpd,'w');
fwrite($fileNameUpd, $data);
fclose($fileNameUpd);
}

else if (!file_exists($fileName)){

fwrite($fileName, $data);
fclose($fileName);
};

the way I added it in is like this

我添加它的方式是这样的

<?php ini_set('display_errors','on'); ?><?php


$data= "";

foreach ($_POST as $key => $value) {
$data.= str_replace("_"," ",$key)."\n\n ". $value."\n\n\n\n"; preg_replace("/[^ 0-9a-zA-Z]/", "_", $value);
}


$fileName= fopen("Submissions/".$_POST['First_Name'],'w');

if (file_exists($fileName)){
$num="00";
$fileNameUpd= count(substr.$_POST['First_Name'].$num++);

fopen("Submissions/".$fileNameUpd,'w');
fwrite($fileNameUpd, $data);
fclose($fileNameUpd);
}

else if (!file_exists($fileName)){

fwrite($fileName, $data);
fclose($fileName);
};

?>

How can I get this to achieve what I need it to do?

我怎样才能达到我需要它的目的?

2 个解决方案

#1


1  

Check the file name and loop till dynamic file name not exists. Each time append a counter variable with the file name.

检查文件名和循环,直到动态文件名不存在。每次附加一个带有文件名的计数器变量。

<?php 
    $name = "Submissions/".$_POST['First_Name'].".txt";
    $actual_name = pathinfo($name,PATHINFO_FILENAME);
    $original_name = $actual_name;
    $extension = pathinfo($name, PATHINFO_EXTENSION);
    $i = 1;
    while(file_exists("Submissions/".$actual_name.".".$extension))
    {           
        $actual_name = (string)$original_name."_".$i;
        $name = $actual_name.".".$extension;
        $i++;
    }
    file_put_contents("Submissions/".$name, $data);
?>

#2


0  

You can run while loop to check if the file already exists. As others have pointed out, first name is not a good unique identifier so perhaps you could consider the naming convention.

您可以运行while循环来检查文件是否已存在。正如其他人所指出的,名字不是一个好的唯一标识符,所以也许您可以考虑命名约定。

<?php 
ini_set('display_errors','on');

$data= "";
foreach ($_POST as $key => $value) {
$data.= str_replace("_"," ",$key)."\n\n ". $value."\n\n\n\n"; preg_replace("/[^ 0-9a-zA-Z]/", "_", $value);
}

$file = $_POST['First_Name'].'.txt';
$i = 0;
while (is_file("Submissions/".$file)) {
    $file = $_POST['First_Name'].'_'.$i.'.txt';
    $i++;
}

$fileName= fopen("Submissions/".$file,'w');
fopen("Submissions/".$fileName,'w');
fwrite($fileName, $data);
fclose($fileName);

?>

#1


1  

Check the file name and loop till dynamic file name not exists. Each time append a counter variable with the file name.

检查文件名和循环,直到动态文件名不存在。每次附加一个带有文件名的计数器变量。

<?php 
    $name = "Submissions/".$_POST['First_Name'].".txt";
    $actual_name = pathinfo($name,PATHINFO_FILENAME);
    $original_name = $actual_name;
    $extension = pathinfo($name, PATHINFO_EXTENSION);
    $i = 1;
    while(file_exists("Submissions/".$actual_name.".".$extension))
    {           
        $actual_name = (string)$original_name."_".$i;
        $name = $actual_name.".".$extension;
        $i++;
    }
    file_put_contents("Submissions/".$name, $data);
?>

#2


0  

You can run while loop to check if the file already exists. As others have pointed out, first name is not a good unique identifier so perhaps you could consider the naming convention.

您可以运行while循环来检查文件是否已存在。正如其他人所指出的,名字不是一个好的唯一标识符,所以也许您可以考虑命名约定。

<?php 
ini_set('display_errors','on');

$data= "";
foreach ($_POST as $key => $value) {
$data.= str_replace("_"," ",$key)."\n\n ". $value."\n\n\n\n"; preg_replace("/[^ 0-9a-zA-Z]/", "_", $value);
}

$file = $_POST['First_Name'].'.txt';
$i = 0;
while (is_file("Submissions/".$file)) {
    $file = $_POST['First_Name'].'_'.$i.'.txt';
    $i++;
}

$fileName= fopen("Submissions/".$file,'w');
fopen("Submissions/".$fileName,'w');
fwrite($fileName, $data);
fclose($fileName);

?>