1.11M Members

POST and arguments in PHP

 
0
 

Hello,
I'm trying to write a program that will POST information when executed. I can't explain well so I'm going to show how the program works.

This program is intended to run from console (not from web) like this:

php /home/user/upload.php /home/user/documents/2009Final/income.doc

When executed, I want the program to fill up the appropriate fields and POST. Here is the <form> section of the webpage I want it filled.

<form enctype="multipart/form-data" action="http://192.168.0.10/takeupload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="4194304" />

<table border="1" cellspacing="0" cellpadding="10">
<tr><td align=center class=colhead colspan=2><b>File Upload</b></td></tr>
<tr><td class="heading" valign="top" align="right">File</td><td valign="top" align=left><input type=file name=file size=80>
</td></tr>
<tr><td class="heading" valign="top" align="right">Name</td><td valign="top" align=left><input type="text" name="name" size="80" />
</td></tr>
<tr><td class="heading" valign="top" align="right">Description File</td><td valign="top" align=left><input type=file name=nfo size=80><br>
</td></tr>
<tr><td class="heading" valign="top" align="right">Description</td><td valign="top" align=left><textarea name="descr" rows="10" cols="80"></textarea></td></tr>
<tr><td class="heading" valign="top" align="right">Type</td><td valign="top" align=left><select name="type">
<option value="0">(Select One)</option>
<option value="1">Final Report</option>
<option value="2">Income Report</option>
<option value="3">Semi-Final Report</option>
</select>
</td></tr>
<tr><td align="center" colspan="2"><input type="submit" class=btn value="Send" /></td></tr>
</table>
</form>

In this case, I want the program to fill up the values like this:
File: /home/user/documents/2009Final/income.doc
Name: income.doc
Description File: /home/user/documents/2009Final/description.txt
Description: Content of description.txt
Type: 1 (Income Report as the file name is income.doc)

Now the first question is, how can I load the arguements given and load it in, for example, $location? (In this case, /home/user/documents/2009Final/income.doc)

Second question is, how can I make it fill the form automatically(? not exactly filling) and POST?

Is this even possible?
Thanks

 
0
 

Hello.

$argv will contain the command line arguments. See what the array contains using this code.. and work that in...

<?php var_dump($argv); ?>

You can load the description using file_get_contents();

<?php $description =  file_get_contents('/path/to/description.txt'); ?>

You can use basename() to work out 'invoice.doc' from the full file path as demonstrated below.

<?php $basename = basename('/path/to/income.doc'); ?>

You can then use fsockopen/fputs to post to a server.

function post_to_host($host, $path, $data)
{
	$handle = fsockopen($host, 80);
	fputs($handle, "POST $path HTTP/1.1\r\n");
	fputs($handle, "Host: $host\r\n");
	fputs($handle, "Content-Type: application/x-www-form-urlencoded\r\n");
	fputs($handle, "Content-length: " . strlen($data) . "\r\n");
	fputs($handle, "Connection: close\r\n\r\n");
	fputs($handle, $data);
	while (!feof($handle)) { fgets($handle,128); }
	fclose($handle);
}

You will need to url encode the data for $data.

Hope this jumbled mess helps you compile a script ^. But I won't do it all for you :P

Jonathan.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article