Showing posts with label php. Show all posts
Showing posts with label php. Show all posts




Many sites are using MySQL as database. It is a good practice that taking backup in regular intervals. My aim was to develop a package which deliver me database backup files [ having extension .sql] and mail me with the file as attachment.


You can download[ link ] this package from phpclasses.org[ link ]


This package, I named it as myeskuel. This package contains
1. ReadMe File : README.txt
2. Class File : mysql.class.inc
3. Configuration File: config.php
4. Invoking script : backup.php


How To Configure This
All you need to give some details into the config.php file. The configuration is divided into 3 parts:


1. BackUp folder Configuration
$backUpFolder = "backup";

Please change the folder; iff you need to change the backup directory. It is better to give complete path. The line just below it tries to make the directory if not exosts and give global (0777) permission for access.
2. Database Configuration Details
    $server['host'] = "localhost";
    $server['port'] = "";
    $server['user'] = "root";
    $server['pass'] = "pass";
    $server['database'] = "test_db";

    In this we need to give the details of the database: hostname, authenticated username, its password and the name of database. Giving port number is not necessary, if you are not sure about leave that position blank; it will take the default port number[3306].
    3. e-mail Configuration Details
    $mailer["FromMail"] = "db_manager@yoursite.com";
    $mailer["ToMail"] = "developer@yoursite.com";
    $mailer["MailBody"] = "Message Body";
    $mailer["DAS"] = false;


    Prerequisite for this facility is default php mail() support for your server and supports sending generated emails [most of the sites are having these options].


    FromMail, is the valid email address which comes in 'from header'. ToMail is considering as the trigger for mailing function. If you are leave this blank, the function for sending e-mail will be disabled, if you given a email to The MailBody contains the message that will be send along with the file as attachment.


    4. DAS – Delete After Send


    $mailer["DAS"] = false;


    This is an important parameter, if you configured as true, it will delete the file from the backup folder [created in the server] after successfully send the mail which configured to.


    EXTRA
    backup.php is the file that invokes the class and assign the variables, you can see generating a random name [on line # 38]. You can use any algorithm if you find it is not sufficient.

    Go through the class file mysql.class.inc. The functions having comments which helps you to modify and suited for you purpose. You can set a cronjob[scheduled Task] for send you the updated database backup on regular intervals. Enjoy.


    “Comments and queries are always encouraging”.


    Download: myeskuel.zip




    It is simple in MySql to query some from a table by its ID and order it using another field say "rating"
    here is the php code for that


    $data = mysql_query(SELECT * FROM (SELECT * FROM table_name ORDER BY ID DESC LIMIT 50) AS ttbl ORDER BY rating ASC;


    ttbl is a virtual table or view (I don't know what exactly it is)

    Category : , | Read More...... edit post

    One of my client gave me a huge file which contain thousands of web urls, he want that into a html file having html (< a) tags with those site addresses as links.

    eg: the input file contain
    1. thecoderin.blogspot.com < http://thecoderin.blogspot.com/>
    2. keltrondays.blogspot.com < http://keltrondays.blogspot.com/>
    3. sahabm.blogspot.com < http://sahabm.blogspot.com/>
    4. linuxlight.blogspot.com < http://linuxlight.blogspot.com>

    the php code for convert this into format is given below:


    <?

    $file_to_read = "lines.txt";
    $file_to_write = "output.txt";


    $fh = fopen("$file_to_read", "r");
    $i = 1;
    while ($line = fgets($fh)) {
    $row[$i] = $line;
    $i++;
    }
    fclose($fh);



    $fh = fopen($file_to_write, 'w') or die("can't open file");

    $a = 1;
    $max = count($row);

    while ($a <= $max) {

    $var[$a] = explode(" ",$row[$a]);

    $data[$a] = "<a href='http://".$var[$a][1]."'>".$var[$a][1]."</a>\n";

    echo $data[$a];

    /*
    fwrite($fh, $data[$a]);
    */



    $a++;
    }

    #print_r($row);
    ?>

    Category : , | Read More...... edit post

    Consider a html page which having several comments; which is not good for mobile webpages, as its size must be very low. So better rebuild the html page.

    Here a script written using php, where it removes the html comments (including the matter inside <!-- and -->

    single line code for that


    $str = preg_replace('/<!--*?-->/i', '', $str);

    Category : | Read More...... edit post

    Wondered ? Yes it is possible using proc_open command for running terminal operations. But I am not sure about Windows Machines.

    The Code goes here:


    $descriptorspec = array(
    0 => array("pipe", "r"), // stdin is a pipe that the child will read from
    1 => array("pipe", "w"), // stdout is a pipe that the child will write to
    2 => array("file", "filename.txt", "w") // stderr is a file to write to
    );
    $command = "g++ cplusplusfile.cpp";
    proc_open($command,$descriptospec,$output);

    Category : , | Read More...... edit post

    Many CMS sites having a facility to show the latest topics on site. I have created a general function for get the top 'n' posts. It is in PHP with MySql.



    code:



    < ?php
    function select_top($table, $fields, $key, $nos)
    {
    //done by : thecoderin.gmail.com
    //date: 28 feb 2009

    $count_field = count($fields);
    $query = "SELECT ";
    for($i=0; $i<$count_field -1; $i++) +++$query = $query."`".$fields[$i]."`, ";

    $query = $query."`".$fields[$count_field -1]."` ";

    $query .= "FROM $table ORDER BY $key DESC LIMIT $nos";
    // echo $query;
    $result = mysql_query($query);
    return $result;
    }
    ?>

    Category : , | Read More...... edit post

    PHP is a wonderful language which make anything possible in the web world. In some applications we need to generate a archive for binding a group of files.

    Tar (tape archive), a file format (in the form of a type of archive bitstream) is going to generate. The tar format was created in early days of Unix and standardised by POSIX. So better you try it in a linux based system.

    $file_path is the file/directory name you would like to archive
    .



    code:



    <? php
    $output_file = "TAR_FOLDER/".trim($ file_name) .".tar";
    $cmd = "tar -cf ".trim($output_ file)." -C ".trim($file_ path)."
    ".trim($file_ name);
    exec($cmd);

    ?>


    Category : , | Read More...... edit post

    Joomla is a php based open-source CMS toolkit. More you can find out from this document

    Just follow the steps:

    1.Create an article
    2. Type some intro text, we need to put in the read more divider.
    3.Click the Read More button at the bottom of the text editor.
    4.Once you have the read more separation line, add some text beneath it.
    5.Configuring the Article’s Parameters

    Now that we have an article with intro text and main text, it needs to be configured so that it has permission to show the intro text to the public, but only displays the main text to users who have registered and logged in. On the right hand side of the Article Editor there are several parameter tabs. Under the first tab [Parameters – Article] there is an option labeled Access Level. By default it is set to public. Click the drop box and set it to Registered.

    6. Click Save to save the article and exit to the Article Manager.

    7. Select Main Menu From Menus tab.
    8. Click on the Home Menu type
    9. On Right Side
    Parameters(Component)->Show Unauthorised Links; put the value as Yes.
    10.Click on Save.

    This article is for Joomla 1.5.x, you can if you want try it with older version, then please check this document

    Category : , | Read More...... edit post

    Category

    Followers

    FeedCount

    Latest Posts

    thecoderinc

    Subscribe Now