PDA

View Full Version : Generate random filename and then get the filename?



Stefy09
17-06-2009, 10:11 PM
Okay so I want to have it so someone puts something into a form on another page and when they click the button it creates a random number generated textfile AND THEN writes to that textfile, I've got the random number textfile working okay but how do I then write to the new file I just created without knowing what the filename is?

Basically here's how it goes..

*Name into form*
*Press the button*
*create random named textfile*
*write the name that the person entered into the form into the textfile*

But I do not know how to get the textfile name because its random :(

Help appreciated thankzz

grnr
17-06-2009, 10:29 PM
Google fwrite, that's all I need to say.

Excellent2
18-06-2009, 12:59 AM
Just write the data to that single file?

Once you've ran it through a random text generator, have php write to that file using fwrite and then save it?

RichardKnox
20-06-2009, 12:27 PM
Not 100% sure if this works (haven't tested) and it's a long time since I've done anything like this, but it's worth a shot anyway:


<?php

if($_POST['form_name']) {

$name = $_POST['name'];
$rand = random(1,10000);

while(file_exists($rand . '.txt') {
$rand = random(1,10000);
}

$full_path = $rand . '.txt';

$file_op = fopen($full_path, 'w');

fwrite($file_op, $name);

fclose($file_op);

}

?>

Dentafrice
22-06-2009, 04:18 AM
PHP5 has a neat function.. file_put_contents.

Want to hide these adverts? Register an account for free!