I have the following script which is working well at taking the data from my HTML form and writing it to a .conf file.
<?php
$path = '/usr/local/flowsim/data/phptest.conf';
if (isset($_POST['CollectorIP']) && isset($_POST['CollectorPort']) && isset($_POST['NetflowVersion'])) {
$fh = fopen($path,"a+");
$string = 'collector-ip='.$_POST['CollectorIP']. "\n". 'collector-port='.$_POST['CollectorPort']. "\n". 'engine='.$_POST['NetflowVersion'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
However I am needing this script to "auto-name" the .conf files differently using the variables from the HTML form. For example, at the moment the script is creating the file phptest.conf and writing the below info (which will be different each time) which was inputted via the HTML form:
collector-ip=10.0.0.0
collector-port=9000
engine=Netflow Version 10 (IPFIX)
As these three inputs will be unique every time the script is run I would like to use them to name the new file each time the form is submitted.
For example if the collector-ip was 5.5.5.5, collector-port 9996 and engine Netflow Version 10 (IPFIX) the filename would be 5.5.5.5:9996:Netflow Version 10 (IPFIX).conf.
I am quite new to PHP but I believe this could be achieved by using the (isset($_POST['CollectorIP']), ($_POST['CollectorPort']) and isset($_POST['NetflowVersion']) variables in the file path which would complete from the inputted data and name the files as expected each time the form is submitted.
Is this correct or do I have it wrong? Would the following script work or is there a better way to do this?
<?php
$path = '/usr/local/flowsim/data/(isset($_POST['CollectorIP']):isset($_POST['CollectorPort']):isset($_POST['NetflowVersion']).conf';
if (isset($_POST['CollectorIP']) && isset($_POST['CollectorPort']) && isset($_POST['NetflowVersion'])) {
$fh = fopen($path,"a+");
$string = 'collector-ip='.$_POST['CollectorIP']. "\n". 'collector-port='.$_POST['CollectorPort']. "\n". 'engine='.$_POST['NetflowVersion'];
fwrite($fh,$string); // Write information to the file
fclose($fh); // Close the file
}
?>
Update
<?php
if ( isset( $_POST['CollectorIP'] ) && isset($_POST['CollectorPort']) && isset($_POST['NetflowVersion']) && isset($_POST['Flowrate']) && isset($_POST['TemplateFrequency']) && isset($_POST['SourceIPAddress']) && isset($_POST['DestinationIPAddress']) ) {
// ok let's try to create the file
$path = '/usr/local/flowsim/data/' . trim($_POST['CollectorIP']) . ':' . trim($_POST['CollectorPort']) . ':' . trim($_POST['NetflowVersion']) . '.conf';
$contents = "";
if ( $fh = fopen($path,"a+") ) {
if ( trim( $_POST['CollectorIP'] ) != "" ) {
$contents .= 'collector-ip=' . $_POST['CollectorIP'];
}
if ( trim( $_POST['CollectorPort'] ) != "" ) {
$contents .= "\n" . 'collector-port=' . $_POST['CollectorPort'];
}
if ( trim( $_POST['NetflowVersion'] ) != "" ) {
$contents .= "\n" . 'engine=' . $_POST['NetflowVersion'];
}
if ( trim( $_POST['Flowrate'] ) != "" ) {
$contents .= "\n" . 'flow-rate=' . $_POST['Flowrate'];
}
if ( trim( $_POST['TemplateFrequency'] ) != "" ) {
$contents .= "\n" . 'template-freq=' . $_POST['TemplateFrequency'];
}
if ( trim( $_POST['SourceIPAddress'] ) != "" ) {
$contents .= "\n" . 'src-ip=' . $_POST['SourceIPAddress'];
}
if ( trim( $_POST['DestinationIPAddress'] ) != "" ) {
$contents .= "\n" . 'dst-ip=' . $_POST['DestinationIPAddress'];
}
if ( fwrite( $fh, $contents ) ) {
}
fclose($fh); // Close the file
}
else {
if (fclose($fh)) {
echo "Netflow traffic is now being sent to the collector at ". ( $_POST['CollectorIP'] ). " on port ". ( $_POST['CollectorPort'] ). ".";
} else {
echo "The simulator was unable to start the traffic flow, please try again.";
}
}
}
?>
file_put_contents()instead offopen/fwrite/fclose