How to edit a user generated file

so here’s my problem, I have a basic HTML form that includes checkboxes, text input and etc. that when submitted creates a .txt file with a random generated name. My question is how would I allow the user to edit this .txt file that has a random name? Simply getting PHP to open this file won’t work for me because I cannot specify a name to the file that needs to be open, because it has a random name generated. So my guess is that I’d have to assign this random generated file name to a variable and then call the variable to open? e.g

$filename = random_string(11);
$file = fopen("$filename","w");

I hope you all get what I am asking for here, I am not very experienced with PHP so some guidance would be greatly appreciated! Here’s the code I have currently (This only creates a .txt file with a random name. Nothing more. )

HTML;

[code]


  • Basic Details

  • Server Options

  • Final Touches




Add some basic details…


This is step 1







Customize your server…


Max No. of Players

Default Gamemode

Survival
Creative
Adventure

Difficulty



0
1
2
3

Allow Flight



Enable PvP



Enable Command Blocks



Spawn Animals



Spawn NPC’s



Spawn Monsters


		<p>Hardcore Mode</p>
		<input type="checkbox" name="gplus" />

		<p>Allow Nether</p>
		<input type="checkbox" name="gplus" />

		<p>Force Gamemode</p>
		<input type="checkbox" name="gplus" />
		<p>Generate Structures</p>
		<input type="checkbox" name="gplus" />


		<input type="button" name="previous" class="previous action-button" value="Previous" />
		<input type="button" name="next" class="next action-button" value="Next" />
	</fieldset>
	<fieldset>
		<h2 class="fs-title">A few more settings</h2>
		<h3 class="fs-subtitle">You're almost done!</h3>

		<p>Online Mode</p>
		<input type="checkbox" name="gplus" />
				<p>Enable Query</p>
		<input type="checkbox" name="gplus" />

		<p>Enable Snooper</p>
		<input type="checkbox" name="gplus" />

				<p>Enable RCON</p>
		<input type="checkbox" name="gplus" />


		<p>Level Seed</p>
		<input type="text" name="gplus" />

		<input type="button" name="previous" class="previous action-button" value="Previous" />
		<input type="submit" value="Submit" />
	</fieldset>
</form>[/code]

PHP (Currently the code below copies an existing file and pastes it with a random name.)

[php] <?php

function random_string($length) {
    $key = '';
    $keys = array_merge(range(0, 9), range('a', 'z'));

    for ($i = 0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }

    return $key;
}
$name = random_string(11);
$filetype = '.txt';
$file = 'temp/original.txt';
$newfile = 'temp/tmp/' . $name . $filetype;


if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}



?>

[/php]
Sorry if the question seems hard to understand, I will try my best to edit it if necessary. Thanks for any and all help.

[size=10pt]edited to make the code easier to read[/size]

First, why are you creating a randomly generated file name? You would need it to link somehow to a user, if only they can edit it. You could do that with a file holding those values, or a database that linked files to a user, but it still stands for why a random name? Random data in a file is different than a filename that gets lost.

Your code fails in at least one aspect. It stores the filename for a specific user in a specific session. What happens if they decide to to edit the file at a different time, have connectivity issues, or somethings my else? That file is just taking up server space.

Use the database to store displayed filename and generated filename.

Sponsor our Newsletter | Privacy Policy | Terms of Service