Jump to content


This topic is now archived and is closed to further replies.


FOPEN - Can I get a filename from a form that it is calling data from

Recommended Posts

I have this basic script (below) that is getting the information from a form, and then converting it into a csv file.

Is it possible to have the fopen statement get the filename from the form that it is getting all the data from, and then save the same data to this form in a csv format?

I went with the process of listing each item individually (instead of using an array) as I needed one item to be in a different order in the csv file than it is listed on the form, and although I beleive there is a way to organize it, this is my first attempt at PHP and I was getting confused.

I need to let each person who fills out the form input what they want the filename to be called, and it needs to be saved in csv format.




$writetocsv = $_POST['item_title'] . "," . $_POST['item_category'] . "," . $_POST['item_type'] . "," .  $_POST['quantity_available'] . "," . $_POST['starting_bid'] . "," . $_POST['bid_increment'] . "," .  $_POST['reserve_price'] . "," . $_POST['duration'] . "," . $_POST['auto_relist'] . "," . $_POST['city'] . "," . $_POST['state'] . "," . $_POST['country'] . "," . $_POST['item_description'] . "," . $_POST['paypal_id'] . "," . $_POST['hit_counter'] . "," . $_POST['end_hour'] . "\n";
$file = fopen([color=red]filename_from_form[/color]]","a");
fwrite ($file,$writetocsv);
fclose ($file);

Share this post

Link to post
Share on other sites
I'm doing that exact thing in a script I just wrote:

Basically a user choose a name for a quiz they want to create, and my script creates a file with that name. 


Get user to enter name of quiz in form


// Ths collects the filename.
$quiz_name = $_POST['quizname'];

// This appends the proper ending, here it's xml, but you want cvs.
$quiz_file_name = "$quiz_name".".xml";

// Here it creates the string I'm going to put into the file.
$stringAPP = "<?xml version=\"1.0\"?>\n"."<QUIZ>\n\n"."<TITLE>"."$quiz_name"."</TITLE>";

// Open the file to write
$fh = fopen("$quiz_file_name", 'w') or die("can't create file");
// Add the strings in XML format
fwrite($fh, "$stringAPP");

// I have to create a temporary file to store data between runs off the code (since it
// recursively calls itself) so I'm confusing the name of the file up for a bit of security.
$temp = md5("temporaryfile");

// open the file and add the temporary data.  remember to get rid of this file later.
$fh = fopen($temp, 'w') or die("can't create file");
fwrite($fh, "$quiz_file_name");


Does this help?

Share this post

Link to post
Share on other sites
I am thinking that is going to do exactly what I wanted it to do........awesome.  Thank You.

After they complete the quiz, what are you doing with the results?

Do you have to manually get rid of the file later?  After this file is created and stored, I am hoping to figure out a way to email it back to the original user, or have it come back on the screen where they can save it locally, and once it has been emailed or saved, it can be discarded.

Thanks again,


Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.