Monday, May 28, 2012

PHP ftp upload images


To upload a file to ftp you need a html form where you can insert the ftp details, like the ones described above:
server – is the server on which he wanna upload the file
username – is the user for which he makes the connection to ftp server
password - is the user password for ftp username
path to server – is the path on the ftp server which he wanna upload his file.
user file – is the file of the you wanna upload to ftp server
Hint: If you want to offer to your website members a form where he will upload his file, you should take the ftp server details and path, from a config file. Here we made a complete example how to upload through ftp putting all the details.
We will have a POST form and an enctype of multipart/form-data because we have a file on our form.
The user form that the user will insert info is like that:
<form action="upload.php" method="POST" enctype="multipart/form-data">
<table align="center">
<tr>
<td align="right">
Server:
</td>
<td>
<input size="50" type="text" name="server" value="">
</td>
</tr>
<tr>
<td align="right">
Username:
</td>
<td>
<input size="50" type="text" name="user"  value="">
</td>
</tr>
<tr>
<td align="right">
Password:
</td>
<td>
<input size="50" type="text" name="password" value="" >
</td>
</tr>
<tr>
<td align="right">
Path on the server:
</td>
<td>
<input size="50" type="text" name="pathserver" >
</td>
</tr>
<tr>
<td align="right">
Select your file to upload:
</td>
<td>
<input name="userfile" type="file" size="50">
</td>
</tr>
</table>
<table align="center">
<tr>
<td align="center">
<input type="submit" name="submit" value="Upload image" />
</td>
</tr>
 
</table>
</form>
Now, when the user submits the form, we should get all the details that he inputed and then upload the file to the ftp server.
// the file name that should be uploaded
$filep=$_FILES['userfile']['tmp_name']; 
// ftp server
$ftp_server=$_POST['server'];
//ftp user name
$ftp_user_name=$_POST['user'];
//ftp username password
$ftp_user_pass=$_POST['password'];
//path to the folder on which you wanna upload the file
$paths=$_POST['pathserver'];
//the name of the file on the server after you upload the file
$name=$_FILES['userfile']['name'];
To upload the file , first we should establish a connection to the ftp server using ftp_connect and specifing as a parameter the ftp servver. This function return a connection id.
$con_id=ftp_connect($ftp_server);
After connecting to server we should login using the function ftp_login passing three parameters: connection id, ftp user, ftp user password and checking if the login was sucessfull
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
// check connection
if ((!$conn_id) || (!$login_result)) {
       echo "FTP connection has failed!";
       echo "Attempted to connect to $ftp_server for user $ftp_user_name....";
       exit;
   } else {
       echo "Connected to $ftp_server, for user $ftp_user_name".".....";
   }
After loging in we can upload the file to the server and after that check if the file was uploaded correctly:
// upload the file
$upload = ftp_put($conn_id, 'public_html/'.$paths.'/'.$name, $filep, FTP_BINARY);
 
// check upload status
if (!$upload) {
       echo "FTP upload has failed!";
   } else {
       echo "Uploaded $name to $ftp_server ";
   }
In the end we should close the ftp connection using ftp_close passing the connection id:
ftp_close($conn_id);
For uploading big files you should set the time limit of the server in order not to finish the script while it’s uploading:
set_time_limit(300);
Here is the complete code:
<?
if(!isset($_POST["submit"])){?>
 
<form action="upload.php" method="POST" enctype="multipart/form-data">
<table align="center">
<tr>
<td align="right">
Server:
</td>
<td>
<input size="50" type="text" name="server" value="">
</td>
</tr>
<tr>
<td align="right">
Username:
</td>
<td>
<input size="50" type="text" name="user"  value="">
</td>
</tr>
<tr>
<td align="right">
Password:
</td>
<td>
<input size="50" type="text" name="password" value="" >
</td>
</tr>
<tr>
<td align="right">
Path on the server:
</td>
<td>
<input size="50" type="text" name="pathserver" >
</td>
</tr>
<tr>
<td align="right">
Select your file to upload:
</td>
<td>
<input name="userfile" type="file" size="50">
</td>
</tr>
</table>
<table align="center">
<tr>
<td align="center">
<input type="submit" name="submit" value="Upload image" />
</td>
</tr>
 
</table>
</form>
<?}
else 
{
 
 set_time_limit(300);//for setting 
 
$paths=$_POST['pathserver'];
 
$filep=$_FILES['userfile']['tmp_name'];
 
$ftp_server=$_POST['server'];
 
$ftp_user_name=$_POST['user'];
 
$ftp_user_pass=$_POST['password'];
 
$name=$_FILES['userfile']['name'];
 
 
 
// set up a connection to ftp server
$conn_id = ftp_connect($ftp_server);
 
// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
 
// check connection and login result
if ((!$conn_id) || (!$login_result)) {
       echo "FTP connection has encountered an error!";
       echo "Attempted to connect to $ftp_server for user $ftp_user_name....";
       exit;
   } else {
       echo "Connected to $ftp_server, for user $ftp_user_name".".....";
   }
 
// upload the file to the path specified
$upload = ftp_put($conn_id, $paths.'/'.$name, $filep, FTP_BINARY);
 
// check the upload status
if (!$upload) {
       echo "FTP upload has encountered an error!";
   } else {
       echo "Uploaded file with name $name to $ftp_server ";
   }
 
// close the FTP connection
ftp_close($conn_id); 
 
}
?>

Tuesday, May 22, 2012

jQuery facebook message add friends


I really appreciate facebook guys for making such a excellent UI.Whenever i see any new feature in facebook i get amazed “How did they made it?”.One day i saw a script for adding multiple recipients to messages and this script grabbed my attention like other scripts on facebook.In first look this script seem critical and innovative to me (may be because i was new to web development that time) but later on after long time i found that it was nothing but sight Cheating.i decided to make exact same script and I MADE IT.
I have made this with jquery,
facebook Facebook Style Add Friends Script
Implimentation Step By Step
  • 1) INCLUDE JAVASCRIPT AND STYLESHEET
    1
    2
    <script src="js/jquery.js"></script>
    <script src="js/facebookStyleInput.js"></script>
  • 2) CREATING WIREFRAME
    1
    <link rel="stylesheet" type="text/css"href="styles/facebookStyleInput.css">
    add below text to your page
    1
    2
    3
    <div id="fb_holder">
    <input type="text" name="friends" id="fb_inputbox">
    </div>
    id=”fb_holder” can be anything you want. you can replace “fb_holder” with any other string.
  • 3) ATTACH WIREFRAME TO JAVASCRIPT (INITIALISATION)
    1
    2
    3
    4
    5
    <script type="text/javascript">
    var mainHolder   = "#fb_holder";
    var inputBox     = "#fb_inputbox";
    var ajaxFilePath = "";
    </script>
    Assign whatever element names you have used in wireframe to the javascript variables.
    ajaxFilePath will hold path of file that will return matched list.if your file is in folder ajaxthen ajaxFilePath = “ajax/ajax_server.php”
  • 4) DATABASE INSTALLATION
    a) In the same directory you should find files named facebook.sql and config.php.
    b) Import facebook.sql to Mysql.
    b) Open config.php file and make neccessary changes in it.
  • 5) OTHER SETTINGS
    Make sure that ajax_server.php file resides in same directory where index.php file is,but if you want to change its location then you need to make change in configuration variable named ajaxFilePath enter full path of ajax_server.php file.
  • 6) AND YOU ARE READY TO GO…!
    Source 

Jquery ajax combo box – So Simple


Step 1:
Create a combo and load main category options from database, and create a combo(dropdown box) with the id selects.
<select id=”selects” name=”selects”> </select>
<script
 type="text/javascript" src="jquery-1.6.2.min.js"></script>
<script type="text/javascript">
<!--
function choosesub(val){
 $('#selects').html('Loading...');
 $('#selects').load('findsub.php',{value:val},function(responseText){
 //alert("Response:\n" + responseText);
 $("#selects").add(responseText);

 });
}
//-->
</script>
Step:2
Now create the ajax file to build options
<?
CateyList = $CateyDaoImpl->getAllCateyByMain($_REQUEST['value']);
foreach ($CateyList as $a){
$result_string .= “<option value=’”.$a->getId().”‘>”.$a->getName().”</option>”;
}
echo $result_string;
?>
You are done now!!!