Upload File dynamically on Bloomberg FTP and Insert Record in DB



<?php

/*
SET NAMES utf8;
SET foreign_key_checks = 0;
SET time_zone = '+05:30';
SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
DROP TABLE IF EXISTS `usertbl`;
CREATE TABLE `usertbl` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`FirstName` varchar(255) NOT NULL,
`LastName` varchar(255) NOT NULL,
`Email` varchar(255) NOT NULL,
`VideoName` varchar(255) NOT NULL,
`DateTime` datetime NOT NULL,
`CourseCode` varchar(255) NOT NULL,
`VideoDescription` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
*/


if(isset($_POST['submit']))
{
// $output_form = false; // Set output_form variable to false by default.
$fileInfo = $_FILES['uploadedfile'];  // Make an array out of the file information. store in $fileInfo

/*
echo '<pre>';
print_r($_FILES['uploadedfile']);
echo '</pre>';
*/

$fileName = basename($_FILES['uploadedfile']['name']);  // Get filename from path
// Add a check for file name for spaces or dashes - - -   <---Those things.  Return an error message if spaces/dashes are found.
// Add a check form for valid fields.
//$destination_path = "/.$fileName";   // The location where you will upload the file on the server.



// FTP Connection Settings
$ftp_server = "bfmrr.bloomberg.com";  // Address of FTP server
$ftp_user = trim($_REQUEST['username']);
$ftp_password = trim($_REQUEST['password']);


// Setup Connection
$connect_id = ftp_connect($ftp_server) or die("<h2>Couldn't connect to $ftp_server</h2>"); // Try connection
// Try username and password with connection.  Give die message if failed.
$login_result = ftp_login($connect_id, $ftp_user, $ftp_password) or die("<h2>You do not have access to this ftp server!</h2>");
if ((!$connect_id) || (!$login_result))    // Check Connection
{
echo "<h2>FTP Connection has failed! <br />";
echo "Atttempted to connect to $ftp_server for user $ftp_user</h2>";
exit;
}else
{
echo "Connected to $ftp_server, as $ftp_user <br />";
}// Upload file


// Activation du mode passif
ftp_pasv($connect_id, true);

$remote_file_name = 'pragsoutput'.date('YmdHis').'.req';

// Chargement d'un fichier
$up_file = $fileInfo['tmp_name'];
if (ftp_put($connect_id, $remote_file_name, $up_file, FTP_ASCII)) {
echo "<b>Your file $fileName is successfully uploaded</b>\n";
} else {
echo "some problem in uploading file $fileName\n";
}
ftp_close($connect_id);




$con = mysql_connect("localhost","root","pragnesh");
if (!$con)
{ die('Could not connect: ' . mysql_error());
}mysql_select_db("bloomberg_test", $con);
$sql="INSERT INTO usertbl (FirstName, LastName, Email, VideoName, DateTime, CourseCode,   VideoDescription)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[videodescription]',    Now(),'$_POST[coursecode]','$_POST[videodescription]')";
if (!mysql_query($sql,$con))
{die('Error: ' . mysql_error());
}echo "<br />1 record added in database";
mysql_close($con);
}


?>
<html>
<head></head>
<body >

<div style="align:center;margin:auto;border:1px solid #cccccc">
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<h2>Upload File dynamically on Bloomberg FTP and Insert Record in DB</h2>
<table style="border:1px solid #cccccc;padding:20px;margin:auto">
<tr>
<td>Firstname: </td>
<td>
<input type="text" name="firstname" value="pragnesh"/>
</td>
</tr>
<tr>
<td>Lastname:</td>
<td><input type="text" name="lastname" value="karia" />
</td>
</tr>

<tr>
<td>Email: </td>
<td><input type="text" name="email" value="pk@pk.pk" /></td>
</tr>

<tr>
<td>Course Code:</td>
<td>
<input type="text" name="coursecode"  value="0123"/>
</td>
</tr>

<tr>
<td>Video Description: </td>
<td>
<input type="text" name="videodescription" value="Desc"/>
</td>
</tr>

<tr>
<td>Server User Name: </td>
<td>
<input name="username" type="text" id="username" size="15" value="SETUSERHERE"/>
</td>
</tr>

<tr>
<td>Server Password: </td>
<td>
<input name="password" type="text" id="password" size="15" value="SETPASSHERE"/>
</td>
</tr>

<tr>
<td> Choose a file to upload:  </td>
<td>
<input name="uploadedfile" type="file" id="uploadedfile" onChange="uploadedfileName.value=uploadedfile.value"/><br /><br>
<input disabled="disabled" name="uploadedfileName" type="hidden" id="uploadedfileName" tabindex="99" size="1" />
</td>
</tr>

<tr>
<td>  </td>
<td>
<input type="submit" name="submit" value="Upload File" />
</td>
</tr>
</table>

</form>

</div>
</body>
</html>

&nbsp;

Output will Look Like

bloomberg_1

 

Now you need to check that is your file uploaded on live FTP or not.

 

 

 

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools