Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 05 July 2006 - 08:52 PM

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.

Thanks,

Ken

<?php

$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(filename_from_form]","a");
fwrite ($file,$writetocsv);
fclose ($file);
?>

#2 dwees

dwees
  • Members
  • PipPipPip
  • Advanced Member
  • 47 posts
  • LocationUnited Kingdom

Posted 05 July 2006 - 10:28 PM

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

<snip>

// 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");
fclose($fh);

// 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");
fclose($fh);


Does this help?

#3 kenwvs

kenwvs
  • Members
  • PipPipPip
  • Advanced Member
  • 194 posts

Posted 05 July 2006 - 11:52 PM

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,

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users