avis world

Let’s share my knowledge

Sending Mail with an attachment July 14, 2009

Here is the source code for sending mail with Image as an attachment. In my example, I have used Linux -Debian -PHP 5 version to check mail attachment. I tried with sending Gif and PNG image. Its works well. I hope it works for other too.. Change the content type image/gif to image/png for PNG and image/jpeg for JPG images.

/* Editable part */
$subject = ‘PHP Mail Attachment Test’;
$bound_text = “shiva123″;
$bound = “–”.$bound_text.”\r\n”;
$bound_last = “–”.$bound_text.”–\r\n”;
/* ////////////////FILE NAME & PATH //////////////*/
$filename=”logo.png”;
$file_path=”/tmp/”.$filename;

/* //////////From and To Address ////////////////*/
$to =”from@domain.com”;
$headers =”From: to@domain.com\r\n”;

/* /////////////////////NON-EDITABLE PART /////////////////*/
$headers .= “MIME-Version: 1.0\r\n”
.”Content-Type: multipart/mixed; boundary=\”$bound_text\”";
$message .= $bound;
$message .= “Content-Type: text/html; charset=\”iso-8859-1\”\r\n”
.”Content-Transfer-Encoding: 7bit\r\n\r\n”;

$message.=”Hai this is example of sending Mail with Attachments\r\n”.$bound;

$file = file_get_contents($file_path);

$message .= “Content-Type: image/gif; name=\”".$filename.”\”\r\n”
.”Content-Transfer-Encoding: base64\r\n”
.”Content-disposition: attachment; file=\”".$filename.”\”\r\n”
.”\r\n”
.chunk_split(base64_encode($file))
.$bound_last;
/*////////////////////////////// NON-EDITABLE ENDS HERE ////////// */

mail($to, $subject, $message, $headers); // sending mail

Thanks Jim Plush!

 

Editing the web content on browser May 14, 2009

  1. Install Add-on “Firebug” in the Firefox
  2. Go to Firebug javascript console.
  3. Type the following in the console
    document.body.contentEditable=true;
  4. followed by
    document.designMode=’on’;
    void 0;
  5. Now you can edit the content of any site in screen itself.
 

File Upload – PHP – curl February 12, 2009

  1. Upload file from html page.
  2. Uploaded file coming to same server. The server reads the file and passing it into Another Server. The server passing only data to remote server not as file.( You cant retrieve using $_FILES @ remote server side)
    $data =$_FILES['myfile']; // Retrieve the file in presence server
    $curl = curl_init();
    $filename =”b_”.time().$data['name']; // giving dynamic file name
    move_uploaded_file($_FILES['myfile']['tmp_name'],$filename); // Moving the file to presence server
    $fp = fopen($filename,”r”); // Open the file
    print “File Uploaded Successfully to the Server using Curl“;
    curl_setopt($curl, CURLOPT_URL, “url”);
    curl_setopt($curl, CURLOPT_PUT, 1);
    curl_setopt($curl,CURLOPT_UPLOAD,true);
    curl_setopt($curl,CURLOPT_REFERER,true);
    curl_setopt($curl, CURLOPT_INFILE,$fp);
    curl_setopt($curl, CURLOPT_INFILESIZE,filesize($filename));
    $result = curl_exec($curl);
    unlink($filename);
    fclose($fp);
    curl_close($curl);
  3. Remote Server received the file using the below code and Display the data.
    $putdata=file_get_contents(‘php://input’); // retrieve the data in Remote server
    $filename =”b_”.time();
    $fp = fopen($filename,”w”);
    fwrite($fp,$putdata);
    fclose($fp);
    $file_details =exec(“file $filename”); // using linux command retrieve file information.
    print “**********************At Server Side********************”;
    print “File Details:“.$file_details.”";
    print ‘Contents are :‘;
    echo $putdata;
  4. finish
 

Mysql Questions October 13, 2008

Filed under: Mysql — myworldkvs @ 4:12 pm
Tags: , , ,

Mysql:

1. What is difference between Order By and Group By?

2. How many types of storage engines?

3. what is default engine in mysql?

4. What is difference between MyISAM and InnoDB?

5. What is difference b/w primary key and Unique?

6. Primary Key and foreign Key?

7. How do you store binary data in mysql?

8. Data will be stored in which format of file in MyISAM and InnoDB?

9. How many primary keys are allowed in a table?

10. How many unique keys are allowed in a table?

11. How to increase the speed of query execution?

12. What is Stored procedure and why we use this?

13. Difference between truncate and delete command?

14. Why do need to use InnoDB?

15. How do you import or take a backup of database?

16. I want to change the field data type from int to varchar how to do?

17. How do you start/stop/restart mysql server?

18. have you used Phpmyadmin?

19. How to set privileges to the user?

20. What is Having clause and when it to be used?

21. How to you change the date format?

22. Bulk insert data is possible? if yes, then how?

23. How to make a copy of one table to another?

 

PHP Questions October 6, 2008

Filed under: PHP — myworldkvs @ 12:20 pm
Tags: , , ,

1. What is difference b/w PHP 4 and PHP 5?

2. Suppress Errors?

3. Difference b/w split and Explode.

4. How to set and unset the cookie and session.

5. Difference b/w unset and unlink?

6. Types to connect Mysql?

7. File Upload.?

8. Difference b/w Include and Include_once?

9. Difference b/w Require and require_once?

10. Static variable and static method?

11. private static method?

12. Inheritance concepts..?

13. Is php supports multiple inheritance..?

14. Error handling describe?

15. How to increase the performance of PHP application?

16. What is register_globals and why its off by default?

17. Magic quotes..how its differ from addslashes method?

18. Which method can be used to change the configuration file at run time?

19. Describe Constructor?

20. what is __ (two underscore) represented in PHP?

21. How to get the size of the image?

22. About mail() function?

23. Difference between mysql_fetch_array and mysql_fetch_object?

 

Ajax September 9, 2008

Filed under: Ajax — myworldkvs @ 1:07 pm
Tags: , , ,

Ajax ( Asychronous javascript and Xml)  nowadays used in Web development.

Synchronous:

In ajax, all the request are sending in the form of request. If the request mode is in Synchronous, then the program can continue ONLY after the response got it. Else it wait for some span of time and continue the program after the time elapsed.

Asynchronous :

This type of request, the program will continue the execution and will not wait for the response. The readystate which tells whether response arrived or not.

Where we differentiate the Sync or Async request in a code:

  1. xmlHttp = new XmlHttpRequest();

    xmlHttp.open(“GET”,”test.php”,true); // Asynchronous

    xmlHttp.open(“GET”,”test.php”,false);  // Synchronous

if true means Asynchronous Mode. false means Synchronous mode.

Drawback of Ajax:

* Does not supported cross domain access.

Note : since Firefox 3.0.10, Support for Cross-Site XmlHttpRequest has been removed until the specification becomes more stable and the security model is improved.

I tried to fetch the data from other server. but its failed.
If you want to access the cross domain thru Ajax, use JSON instead.  JSON will support to fetch the data thru javascript.

 

Passing form values in an array format July 24, 2008

Filed under: Form — myworldkvs @ 6:11 am
Tags: , ,

Here is the code that you shall pass the form values in an array format. The name with [] that identify is an array. At the php side, just mention $_POST['dorm'] to receive the parameter values posted from Form.

  1. Html Code:

    <form name=”myform” method=”post” onsubmit=”check()”>
    <input type=”text” name=”dorm[]“>
    <input type=”text” name=”dorm[]“>
    <input type=”text” name=”dorm[]“>
    <input type=”text” name=”dorm[]“>
    <input type=”text” name=”dorm[]“>
    <input type=”text” name=”dorm[]“>
    <input type=”submit” name=”submit” value=”S”>
    </form>

  2. Javascript Validation:
    function check_All()
    {
    var form = document.myform.elements;
    for(var i=0;i<form.length;i++)
    {
    //  alert(form[i].name);
    }
    }
    function check_particular()
    {
    var txtbox = document.myform.elements['dorm[]‘]; // Returns the node list; node list stored as an array;
    // Find an length
    var len = txtbox.length
    // Retrive the value
    if (txtbox[0].value==”")
    alert(” Mandatory to fill up the first field”);

    }

  3. //Receiving the passing values at PHP side
    <?
    $values = $_POST['dorm'];
    print_r($values);
    ?>
 

Rollover Images July 18, 2008

Filed under: Javascript — myworldkvs @ 11:24 am
Tags: , ,

Roll-over Images:

I saw the people who use front page or dreamweaver for rollover images. but if we write the code that takes very less code and maintenance also easy. here is the javascript code which perform the same thing i.e automatically change the images every 100 milli seconds.

  1. Html Code:

    <img src=”images/1.png” id=”img” border=”0″>

  2. Javascript:
    Create a new object called imgtime using Object function.

    imgtime.img=0;
    setTimeout(“img()”,100);

    function img()
    {
    var k= imgtime.img+1;

    if (k<6) <!–5 images max–>
    {
    imgtime.img=k;
    var imgsrc=”images/”+k+”.png”; <!– give the image name here–>
    document.getElementById(“img”).src=imgsrc;
    setTimeout(“img()”,100);
    }
    else
    repeatimg(); <!–reset–>
    }

    function repeatimg()
    {
    imgtime.img=0;
    img();
    }

 

Checkbox & Radio July 18, 2008

Filed under: Javascript — myworldkvs @ 9:52 am
Tags: , ,

Javascript code for checking the Checkbox and Radio button whether checked or not.

  1. Script Code:

    var chk = document.form.checkboxname;  // return Node list

    for(var i=0;i<chk.length;i++)
    {

    if(chk[i].checked==true)

    {

    // do

    }

    // you can do this while reset

    chk[i].checked=false;// make checked false
    chk[i].disabled=false;  // enable if you disbled already
    }