Wednesday, 27 June 2012

php send a mail with web templete

           

                      $to =$email.',issacbalaji@yahoo.com';                    
                      $subject = "Registration ";
                      $mailfrom = "sales@.in";
                      $headers = "From:" . $mailfrom;
                      $message=$body;
                      $subject = 'Registration Confirmation - Online Store';
                     $message = '
                      <html>
                          <head>
                              <title>Registration Confirm</title>
                              <style>
                                    html{background:#e6e6e6 url("http://i..in/images/home_headertext.png") 50% 0 repeat;}
                                    body{background:url("http://i..in/images/home_headertext.png") 50% 0 repeat-x; padding:0;}
                              </style>
                          </head>

                              <body bgcolor="#e6e6e6">
                              <p></p>
                              <table bgcolor="#e6e6e6">
                                  <tr>
                                      <td>
                                          <table width=700>
                                            <tr>
                                                <td  align=left><img src="http://i..in/images/logo_mamidi.png" />&nbsp;</td>
                                                <td align=right><img src="http://i..in/images/logo_url.png" /><br/><font color="#213993"> &nbsp;Support Related<br>&nbsp;&nbsp;&nbsp; 080-49048150</font></td></tr>
                                          </table>
                                      </td>
                                  </tr>
                                  <tr>
                                    <td>
                                        <p>Dear '.$fName.',</p>                                      
                                        <p>
                                            <fieldset>
                                                    <legend><font color="#000000" size=5><b>Registratio Details details<b/></font></legend>
                                                    <p>
                                                    Thank you for registration, our support team will contact shortly !
                                                    </p>

                                                    <p>
                                                Feel free to contact us for any assistance on our Zip Dial 080-49048150 or you can mail us at support @.in
                                                    </p>
                                            </fieldset>
                                        </p>

                              </td></tr>
                              <tr><td><font color="#213993">
                              Thanks & Regards,<br>
                              Online Store</font>
                              </td></tr>
                              </table>
                              </body>
                          </html>
                      ';
  // To send HTML mail, the Content-type header must be set
                      $headers  = 'MIME-Version: 1.0' . "\r\n";
                      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

                      // Additional headers
                      $headers .= 'From: Registration Confirmation <sales@.in>' . "\r\n";
                     // $headers .= "Reply-To: support@.in \r\n";
                                    // $headers .= "BCC: balaji@.in\r\n";
                      mail($to, $subject, $message, $headers);

Thursday, 21 June 2012

PHP for current url

 $current_url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
         

PHP creating the folder

Note:  $thisdir ------this is the current directery
mkdir---------creating folder



 if(mkdir($thisdir ."/upload/".$fname , 0777))
                {
                   echo "Directory has been created successfully...";
                }
                else
                {
                   echo "Failed to create directory...";
                }


or


 $thisdir= $thisdir.'/upload';
            mkdir ("/htdocs/video/upload/.$fname", 0777);

Tuesday, 19 June 2012

php for simple file upload





         /*************start file upload************************/
         $target = "upload/";
         $target = $target . basename( $_FILES['uploaded']['name']) ;
         $ok=1;
         if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
         {
         echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
         }
         else {
         echo "Sorry, there was a problem uploading your file.";
         }
          /*************end file upload************************/
         $file_name=$_FILES["uploaded"]["name"] ;
         $file_type= $_FILES["file"]["type"];
         $file_size =$_FILES["file"]["size"] / 1024);// in KB




Monday, 18 June 2012

Storing Images in MySQL with PHP

Why store binary files in MySQL using PHP?
You may want to allow users to upload files to your PHP script or store banner images from advertisers inside of your SQL database.  Instead of creating and using a messy directory structure it may be more suitable to store them directly in your SQL database along with the advertiser or user information.

Reasons to store your images (binary files) in a database:

    Storing in a database allows better security for your files and images.
    Eliminates messy directory and file structures (this is especially true when users are allow to contribute files).
    Files/Images can be stored directly linking to user or advertiser information.

You may want to reconsider storing your binary files in the database.  Here are some reason you may not want to:

    You can't directly access your files using standard applications such as FTP.
    If you database becomes corrupt, so do your files.
    Migrating to a new database is made more difficult
    Pulling images from a database and displaying them is slower than using file access.

Note: all of the banners and user attachments that CodeCall has are stored directly in MySQL.

Creating the Database and Table
Using the console we will access MySQL and create a database and table for our binary upload PHP script.  Using a tool such as PHPMyAdmin can make this task a bit easier.  If your webhost has cpanel or Plesk then you probably have access to PHPMyAdmin.  If your host does not you might want to consider hosting from ToastedPenguin.com.

First login to MySQL:

# mysql -u root -p
   Enter Password: ******

Replace the user root with your username for MySQL.  Since I'm running this on my local machine I will access the database using the root account via the console and PHP Script.  This is not recommended and you should not access your database in a public environment, such as the internet, using the root user, ever!

Now create the database:

mysql> CREATE DATABASE binary;

Press Enter, you should see results similar to:

Query OK, 1 row affected (0.00 sec)

Lets create the table now:

mysql> CREATE TABLE tbl_images (
         > id tinyint(3) unsigned NOT NULL auto_increment,
         > image blob NOT NULL,
         > PRIMARY KEY (id)
         > );

What is a BLOB?
Above we created two tables, one the primary ID (of the row/entry) and the binary BLOB. A BLOB is a binary large object that can hold a variable amount of data. The four BLOB  types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. These differ only in the maximum length of the values they can hold. The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements.

Source


You've now created your database and are ready to upload binary files to it.  In this case, images.  Next we will create a simple upload script. 


Creating the HTML
Creating the user interface where you or your visitors will upload files is very simple.  Using basic HTML and relying on the browser to do most of the heavy lifting you can easily create an upload form.  Here is the one we will use:

<form enctype="multipart/form-data" action="insert.php" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="102400" type="hidden">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">


Copy this code into a file and save it as add.html

The code above allows the uses to browse for a file and select any image/jpeg file type.   Once selected and the submit button is pressed the image contents are forwarded to PHP script, insert.php.


Inserting the Image into MySQL
Create a new file named insert.php.  If you change the name of this file you will also need to change the action="" part in your HTML file above.  Open the file in your favorite text editor or PHP IDE. 

Write or copy/paste this code into your file:

 // Create MySQL login values and
// set them to your login information.
$username = "YourUserName";
$password = "YourPassword";
$host = "localhost";
$database = "binary";

// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
        die('Could not connect: ' . mysql_error());
}

// Select your database
mysql_select_db ($database);

The above code sets your connection variables, creates a connection to MySQL and selects your database (binary if you followed my instructions above).  Next we need to read the form data and insert it into the database.

// Make sure the user actually
// selected and uploaded a file
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) {

          // Temporary file name stored on the server
          $tmpName  = $_FILES['image']['tmp_name']; 
          
          // Read the file
          $fp     = fopen($tmpName, 'r');
          $data = fread($fp, filesize($tmpName));
          $data = addslashes($data);
          fclose($fp);
         

          // Create the query and insert
          // into our database.
          $query = "INSERT INTO tbl_images ";
          $query .= "(image) VALUES ('$data')";
          $results = mysql_query($query, $link);
         
          // Print results
          print "Thank you, your file has been uploaded.";
         
}
else {
   print "No image selected/uploaded";
}

// Close our MySQL Link
mysql_close($link);
?>

The PHP code above takes the user selected image, reads it with fopen and stores the data in a variable named $data.  The binary data is then inserted into our database for retrieval at a later time. 

Save the file above and access add.html via your browser.  Select an image file and upload it. 

Questions and Attachments
If you have any questions please ask them here.  I'll try to answer them when I can.  I've attached working copies of the add.html and insert.php scripts to this thread.  If the tutorial was useful please post a simple "thank you".

In the next PHP Tutorial I'll show you how to retrieve these binary images from MySQL and display them to your visitors.

Inserting and displaying images in MySQL using PHP


Inserting and displaying images in MySQL using PHP
Posted on July 7, 2010 by Vikas Mahajan   

Well working with images is quite easy task in MySQL  using php code. Some years back managing images in relational database is quite complex task as at those times relational databases are able to store only textual data so file path to the images are stored in database and images are stored and retrieved externally. Also special  file functions are necessary for retrieving  images this way and this approach is system dependent (because of path names used). Nowadays, almost all major DBMS support storing of images directly in database by storing images in the form of binary data. Here, I am explaining the method of storing and retrieving images in MySQL database using PHP code.
Inserting images in mysql-:

MySQL has a blob data type which can used to store binary data. A blob is a collection of binary data stored as a single entity in a database management system. Blobs are typically images, audio or other multimedia blob objects. MySQL has four BLOB types:

    TINYBLOB
    BLOB
    MEDIUMBLOB
    LONGBLOB

All these types differ only in their sizes.

For my demonstration, lets us create a test table named test_image in MySQL having 3 columns show below-:

    Id (INT) -Act as primary key for table.
    Name (VARCHAR) – Used to store image name.
    Image (BLOB) – Used to store actual image data.

You can use phpMyAdmin tool to create the above table else use the following MySQL query-:

create table test_image (
id              int(10)  not null AUTO_INCREMENT PRIMARY KEY,
name            varchar(25) not null default '',
image           blob        not null
 );

PHP code to upload image and store in database-:

To upload the image file from client to server and then store image in MySQL database on server, I am posting here the PHP code for our test/sample table (test_image).

Please change the values of variables in file_constants.php file according to your system. Save the following scripts with names as shown in your web directory.
file_constants.php

<?php
$host="your_hostname";
$user="your_databaseuser";
$pass="your_database_password";
$db="database_name_to_use";
?>

file_insert.php

<html>
<head><title>File Insert</title></head>
<body>
<h3>Please Choose a File and click Submit</h3>

<form enctype="multipart/form-data" action=
"<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
<input name="userfile" type="file" />
<input type="submit" value="Submit" />
</form>

<?php

// check if a file was submitted
if(!isset($_FILES['userfile']))
{
    echo '<p>Please select a file</p>';
}
else
{
    try {
    $msg= upload();  //this will upload your image
    echo $msg;  //Message showing success or failure.
    }
    catch(Exception $e) {
    echo $e->getMessage();
    echo 'Sorry, could not upload file';
    }
}

// the upload function

function upload() {
    include "file_constants.php";
    $maxsize = 10000000; //set to approx 10 MB

    //check associated error code
    if($_FILES['userfile']['error']==UPLOAD_ERR_OK) {

        //check whether file is uploaded with HTTP POST
        if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {   

            //checks size of uploaded image on server side
            if( $_FILES['userfile']['size'] < $maxsize) { 
 
               //checks whether uploaded file is of image type
              //if(strpos(mime_content_type($_FILES['userfile']['tmp_name']),"image")===0) {
                 $finfo = finfo_open(FILEINFO_MIME_TYPE);
                if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {   

                    // prepare the image for insertion
                    $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name']));

                    // put the image in the db...
                    // database connection
                    mysql_connect($host, $user, $pass) OR DIE (mysql_error());

                    // select the db
                    mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error());

                    // our sql query
                    $sql = "INSERT INTO test_image
                    (image, name)
                    VALUES
                    ('{$imgData}', '{$_FILES['userfile']['name']}');";

                    // insert the image
                    mysql_query($sql) or die("Error in Query: " . mysql_error());
                    $msg='<p>Image successfully saved in database with id ='. mysql_insert_id().' </p>';
                }
                else
                    $msg="<p>Uploaded file is not an image.</p>";
            }
             else {
                // if the file is not less than the maximum allowed, print an error
                $msg='<div>File exceeds the Maximum File limit</div>
                <div>Maximum File limit is '.$maxsize.' bytes</div>
                <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size'].
                ' bytes</div><hr />';
                }
        }
        else
            $msg="File not uploaded successfully.";

    }
    else {
        $msg= file_upload_error_message($_FILES['userfile']['error']);
    }
    return $msg;
}

// Function to return error message based on error code

function file_upload_error_message($error_code) {
    switch ($error_code) {
        case UPLOAD_ERR_INI_SIZE:
            return 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
        case UPLOAD_ERR_FORM_SIZE:
            return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
        case UPLOAD_ERR_PARTIAL:
            return 'The uploaded file was only partially uploaded';
        case UPLOAD_ERR_NO_FILE:
            return 'No file was uploaded';
        case UPLOAD_ERR_NO_TMP_DIR:
            return 'Missing a temporary folder';
        case UPLOAD_ERR_CANT_WRITE:
            return 'Failed to write file to disk';
        case UPLOAD_ERR_EXTENSION:
            return 'File upload stopped by extension';
        default:
            return 'Unknown upload error';
    }
}
?>
</body>
</html>

Below is screenshot of above web page when executed by browser-:

With this you will be able to upload and store images in MySQL database. Also check for the presence of file in the database using phpMyAdmin or any other tool.
Displaying images stored in MySQL-:

Now we are in a position to write PHP code to see images stored by the above script. For that firstly save the script below with name file_display.php in your web directory.
file_display.php

<?php
 include "file_constants.php";
 // just so we know it is broken
 error_reporting(E_ALL);
 // some basic sanity checks
 if(isset($_GET['id']) && is_numeric($_GET['id'])) {
     //connect to the db
     $link = mysql_connect("$host", "$user", "$pass")
     or die("Could not connect: " . mysql_error());

     // select our database
     mysql_select_db("$db") or die(mysql_error());

     // get the image from the db
     $sql = "SELECT image FROM test_image WHERE id=" .$_GET['id'] . ";";

     // the result of the query
     $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

     // set the header for the image
     header("Content-type: image/jpeg");
     echo mysql_result($result, 0);

     // close the db link
     mysql_close($link);
 }
 else {
     echo 'Please use a real id number';
 }
?>

Now you can see the images stored in the database using the following query string-:

Thursday, 7 June 2012

sending mail dynamically using function




this is code for sending the mail using the fucntion with effects like normal mail id as well as GUI effects


this is the code for the sending mail mail set up local from ,subj,cc,bcc


 // $headers .= "Reply-To: support@mmad.in \r\n";
                     // $headers .= "BCC: gopal@**********.in,balaji@*************.in\r\n";
                  


 $to =$email.',issacbalaji@yahoo.com';                  
                      $subject = "MMAD Registration ";
                      $mailfrom = "sales@**********.in";
                      $headers = "From:" . $mailfrom;
                      $message=$body;
                      $subject = 'Registration Confirmation - ***************';
                     $message = '



 function send_register_mail($fName,$lname,$address,$city,$state,$pin,$email,$mobile,$phone) {

   /*************to customer**********************/
                   
                      $to =$email.',issacbalaji@yahoo.com';                    
                      $subject = "MMAD Registration ";
                      $mailfrom = "sales@mmad.in";
                      $headers = "From:" . $mailfrom;
                      $message=$body;
                      $subject = 'Registration Confirmation - MMAD Online Store';
                     $message = '
                      <html>
                          <head>
                              <title>Registration Confirm</title>
                              <style>
                                    html{background:#e6e6e6 url("http://i.mmad.in/images/home_headertext.png") 50% 0 repeat;}
                                    body{background:url("http://i.mmad.in/images/home_headertext.png") 50% 0 repeat-x; padding:0;}
                              </style>
                          </head>

                              <body bgcolor="#e6e6e6">
                              <p></p>
                              <table bgcolor="#e6e6e6">
                                  <tr>
                                      <td>
                                          <table width=700>
                                            <tr>
                                                <td  align=left><img src="http://i.mmad.in/images/logo_mamidi.png" />&nbsp;</td>
                                                <td align=right><img src="http://i.mmad.in/images/logo_url.png" /><br/><font color="#213993"> &nbsp;Support Related<br>&nbsp;&nbsp;&nbsp; 080-49048150</font></td></tr>
                                          </table>
                                      </td>
                                  </tr>
                                  <tr>
                                    <td>
                                        <p>Dear '.$fName.',</p>                                      
                                        <p>
                                            <fieldset>
                                                    <legend><font color="#000000" size=5><b>Registratio Details details<b/></font></legend>
                                                


                                            

                                            </fieldset>
                                        </p>

                              </td></tr>
                

                              </table>
                              </body>
                          </html>
                      ';

                 
                       // To send HTML mail, the Content-type header must be set
                      $headers  = 'MIME-Version: 1.0' . "\r\n";
                      $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

                      // Additional headers
                      $headers .= 'From: Registration Confirmation <sales@mmad.in>' . "\r\n";
                     // $headers .= "Reply-To: support@********.in \r\n";
                     // $headers .= "BCC: gopal@*********.in,balaji@************.in\r\n";
                     // $headers .= "BCC: balaji@**********.in\r\n";
                      mail($to, $subject, $message, $headers);

}

Friday, 1 June 2012

How to user .httacces file in website

create a notepad file and paste this code then save the file as .httacces
now feel that you can avoid using the file existence

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php