Page 1 of 1

Writing to a file with posted variable name--HELP

Posted: Tue Oct 14, 2008 9:58 am
by cutiepatootie272
Hi, I'm new at PHP development (and programming in general) but have had to learn it for a college project. So hopefully this code doesn't look too dumb.

A Java program is sending me variables through the post method. Here is my code:

<html>
<?php

$ClockNum = $_POST['clock_registration'];
settype($ClockNum, "string");
$Time = $_POST['time'];
$ampm1 = $_POST['ampm1'];
$Alarm = $_POST['alarm'];
$ampm2 = $_POST['ampm2'];
$fh = fopen($ClockNum, 'w');
fwrite($fh, $ClockNum);
fwrite($fh, $Time);
fwrite($fh, $ampm1);
fwrite($fh, $Alarm);
fwrite($fh, $ampm2);
fclose($fh);

?>
</html>

I thought I needed to convert the variable to a string, but even this doesn't help. It wasn't working before of after I tried inserting that line. The permissions settings are all fine, including the files to which the program writes. Any ideas??? I need help...fast.

Also, I know my code is very sloppy. What is the proper way to combine all of the fwrite lines? And do I need the html tags at all? (I also tried with those removed, just in case.)

Re: Writing to a file with posted variable name--HELP

Posted: Tue Oct 14, 2008 11:00 am
by aceconcepts
I think you need to explain you situation a bit more clearly. Also, it would help seeing your form.

Re: Writing to a file with posted variable name--HELP

Posted: Tue Oct 14, 2008 11:09 am
by cutiepatootie272
Yes, ok. Thanks for looking! =)

Here is the Java program:

public void postNewItem (NetSAC c)
{
try
{

URL url;
URLConnection urlConn;
DataOutputStream dos;
DataInputStream dis;

url = new URL("http://eden.rutgers.edu/~clairj/php_java.php");
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty ("Content-Type", "application/x-www-form-urlencoded");

dos = new DataOutputStream (urlConn.getOutputStream());
//String message = "clock_registration=" + URLEncoder.encode(c.getRegistration());
//System.out.println(c.getRegistrationNum()+"\n"+ URLEncoder.encode(c.getRegistrationNum()));
//dos.writeBytes("clock_registration=" + URLEncoder.encode(c.getRegistrationNum()));
dos.writeBytes("clock_registration=" + URLEncoder.encode("javafile"));
dos.writeBytes("time="+ URLEncoder.encode(""+c.getHours())+":"+URLEncoder.encode(""+c.getMins()));
dos.writeBytes("ampm1="+ URLEncoder.encode(c.getAMPM()));
dos.writeBytes("alarm="+ URLEncoder.encode(""+c.getAlarmHours())+":"+URLEncoder.encode(""+c.getAlarmMins()));
dos.writeBytes("ampm2="+ URLEncoder.encode(c.getAlarmAMPM()));


dos.flush();
dos.close();

dis = new DataInputStream(urlConn.getInputStream());
String s = dis.readLine();
dis.close();

} // end of "try"

catch (MalformedURLException mue)
{
System.out.println("mue error");
}
catch (IOException ioe)
{
System.out.println("IO Exception");
}

} // end of postNewItem() method

It sends variable values to my PHP program on our server. This part is functioning fine. I can receive those values and write them to a specified file in my directory. The problem occurs when I try to call that file as a variable, specifically as a variable posted from the Java program. In other words, the Java program sends me, for example, the variable file=userfile which I assign as $file. When I try to open and write to $file, it doesn't work. =/

Re: Writing to a file with posted variable name--HELP

Posted: Tue Oct 14, 2008 11:37 am
by omniuni
Ugh.... I hate Java.

Um, you might try file_put_contents() as an alternative to all the fwrite things.

It basically just does all the work for you. (Available from PHP 5.x+)