Showing posts with label working with files. Show all posts
Showing posts with label working with files. Show all posts

Safe Upload

This class is made by Ahmed Selim Refaat Ahmed and can be used to process files uploaded via Web forms.

It checks a given uploaded file and performs several checks before processing it like: checking the file name against a black list of banned names, check if the file already exists in a target directory, check whether the file size is not zero nor exceeds and given limit, check the MIME type against a list of accepted types, check the file name extension against a list of accepted extensions.

Then the class processes the file by moving it to a target directory. If it succeeds, the class returns an array with several details about the file.

The package can be downloaded here (3 kb).

Import and export data in databases to XML files

This class is meant to import and export data stored in SQL databases to XML files using the ADODB library.

It provides a function to export the data of SQL query result set to XML file.

It also provides another function that parses XML files that define the data of fields to be inserted as new rows of a table also specified in the XML files.

You can download these files (the class and the examples) from phpclass.org or here (8kb)

WebSite Backup + Database mySQL Backup

These classes are made by ThoR® aka Kender functioning to back up files and MySQL database. You can download it here (6kb).

Below are the instructions of use and sample of how to use it:

INSTRUCTIONS

/********************************
BACKUP OF FILES
*********************************/
$mkBackup = new BackUp;
this initialize the class

$mkBackup->WhatBackup(PATH_TO_SAVE);
this set which folder will be compressed
Relative path. Full path should work. No final "/"

$mkBackup->WhereBackup(MOVE_COMPRESSED_FOLDER_HERE);
this set in which folder the compressed archive will be saved
Relative path. Full path should work. No final "/"

$mkBackup->FileName(NAME_OF_ARCHIVE);
this set the name to give to archive
you can create name on-the-fly, using date(); or whatever you want

$mkBackup->Debug();
if you want to display all the infos about compression
This will display all vars set before: What Backup, Where, and name of archive
Nothing more...

/********************************
BACKUP OF DATABASE

Securing Logo/Image

Sometime, you may want to secure a logo (image) on your user-interface to avoid some replacement.
In that case we can encode the image to a text-base code. To do that we need img2php class which can be downloaded from phpclasses.org or you can download it here (12.7kb including samples) 

Following are the example of steps after extracting the package:
1. write this script
<?php
require_once("img2php.class.php");
$t = new img2php;
$t->generate("images/"); //place image(s) in a subfolder
?>

2. run the script and the result should be similiar to this:
<?php
        
/**
 * gonximage class : Generated based on directory (images/)
 * 
 * @package 
 * @author Ben Yacoub Hatem <hatem@php.net>
 * @copyright Copyright (c) 2004
 * @version $Id$ - 2009-03-11 11:03:42 - gonximage.class.php
 * @access public
 **/
class gonximage{
    /**
     * Constructor
     * @access protected
     */
    function gonximage(){
        
    }
    
    /**
     * Return image based on it name
     * @access public
     * @return void 
     **/
    function getimage($img){
        switch($img){

            case "orchid_jpg": 
                gonximage::orchid_jpg();
            break;

        } // switch
    }
    function orchid_jpg() 
    {
        header("Content-type: image/jpg");
        header("Content-length: 3980");
        echo base64_decode(
'/9j/4AAQSkZJRgABAgAAAQABAAD/4QDmRXhpZgAASUkqAAgAAAAFABIBAwAB'.
'AAAAAQAAADEBAgAcAAAASgAAADIBAgAUAAAAZgAAABMCAwABAAAAAQAAAGmH'.
'BAABAAAAegAAAAAAAABBQ0QgU3lzdGVtcyBEaWdpdGFsIEltYWdpbmcAMjAw'.
'OTowMToxMiAyMjoyOTo0MwAFAACQBwAEAAAAMDIyMJCSAgAEAAAANDE4AAKg'.
'BAABAAAAjAAAAAOgBAABAAAAtAAAAAWgBAABAAAAvAAAAAAAAAACAAEAAgAE'.
'AAAAUjk4AAIABwAEAAAAMDEwMAAAAAAAAAAA/8AAEQgAtACMAwEhAAIRAQMR'.
'Af/bAIQAEw0OEQ4MExEPERUUExcdMB8dGhodOyotIzBGPkpJRT5EQ05XcF9O'.
'UmpUQ0RhhGJqc3d9fn1LXYmTiHmScHt9eAEfISEsJixWLy9Wtnlneba2tra2'.
'tra2tra2tra2tra2tra2tra2tra2tra2tra2tra2tra2tra2tra2tra2tra2'.
'/8QAfQAAAwEBAQEAAAAAAAAAAAAAAgMEAQUABhAAAgEDAwMDAwIFAgcAAAAA'.
'AQIRAAMhBBIxIkFRE2FxBTKBUpEUI0KhwWLwJDNTgrHR4QEBAQEBAAAAAAAA'.
'AAAAAAAAAQACAxEBAQACAwEBAQAAAAAAAAAAAAERMQIhQRJRcf/aAAwDAQAC'.
'EQMRAD8A+Z27Z2z+9TvMnP70EEZFVWDkoNxLHAUSTUlbaXWEqnpNJHcgUGo0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dF3wiExVN656FlNwXeE7D+1BSX7'.
'j7/SJxgmexivWrxtXGK5BEc4qQLbuL28RMznihu2SssIAOY8VAmK8BUm96YA'.
'IpIOK0ChOvZtyioZJCzAHFUekFX7o+eaIU1zYDltznuDgVFeGcCY5pgpXYCi'.
'FxhZNqRsZtxxk+KQ2yAjBzkLmnrF9LW5TL3SOcAYmgkXnS4zsBBZifx2pPya'.
'k2TtPtS97EQWJFIZXu1CaOaaaiXRJlh81J1NGGF9yC2QRg5NPF1P6gXnHMCg'.
'prlxVB2ruY9+AtSXAWPME5OaQHvt4oWEZPekPAYnFX2Rt06uYiyrEgfqbAFC'.
'c6OkV7kQKk8eDQqmc1J51igqQ0HWKZUSxWrUlqsRicHBqstssEqqz8UETWw9'.
'gseR4qJ7am0W7gUolz1/igbO4eKgJAN0e1Ux/wALYXtcutu94gCkJHADECh7'.
'VJ5hFBuM80J5uaGpDT7hRVF//9k='.
'');
    }


}
?> 

3. copy and save the result above (for instance encode.class.php)
4. write a script to show the image (show.php):
<?php
include "encode.class.php";
$img = new gonximage();
$imgfile = "orchid_jpg";
$img->getimage($imgfile);
 ?>


Creating PDF Files

Here we create pdf files from php scripts. We use ezpdf class. You can download it here (557kb)!! in which I bundled with some files of examples. I gave comments on those examples to understand some functions.
Below are some captures of the example scripts

For examples 1, 2a,and 2b we try to play with fonts and style like this:


For example 3a,3b, and 3c we practice with table like this:


For example 4, 5, and 6 we practice with image, setup page, and make columns:

Generating excel (xls) files

Here we generate excel files from php script. To begin we will need some main excel classes as following:
1. BIFFwriter.php (for writing BIFF (binary file format)
2. Format.php (for generating excel XF records)
3. OLEwriter.php (OLE stream for spreadsheet)
4. Parser.php (for parsing excel formula)
5. Workbook.php (for generating excel workbook)
6. Worksheet.php (for generating excel worksheet)

You may want to see some examples in which you can download with all the six files above. You can download them all here (48kb) !!.

There are also five example files included:
1. example1.php (creating first example in generating excel document and comments are also included)
2. example2.php (set and modify fonts and styles)
3. example3.php (set columns and rows)
4. example4.php (set page)
5. example5.php (apply formula)

Generating RTF Files (cont)

Instead of using RTFGen, we may also use another class namely rtfgenerator in which we can download from phpclasses.org or you can download it here (5kb)!!

After downloading, extract to your folder inside your web server and execute the index.html file and then write your sentences in the textarea provided and press the generate rtf button. That simple.

Generating RTF Files

If we want to generate rtf files or may want to generate reports based on frt files we may need working with rtfgenerator class in which we can download from www.phpclasses.com. To see or learn how it works you may take look at some examples of scripts in which include the rtfgenerator files.
You can download it here (21 kb) !!

there are 3 files of examples:
1. example1.php
2. example2.php
3. example3.php
and 3 other files of rtf generator:
1. frtf.php containing main class to generate rtf files
2. param.inc.php used to set configuration of the class
3. rtfkeywords.inc.php containing all the keywords defined by RTF Specification version 1.7

Creating a simple poll

We are going to create a simple poll that users may select what programming laguage they prefer (ASP or PHP)

First of all prepare four files as below:

1. 'asp.txt' and then insert the number 0 in it as initial value. 
2. 'php.txt' and then type the number 0 in it as initial value. 
Note:  Do not forget to insert the value 0 in the 'asp.txt' and 'php.txt'. This is very important so that the script works
3. create an image file 'poll.jpg' with size 100x10px. This is to make the percentage of polling. 
This is also important to make our poll eye-catching. 
4. 'index.php' and type in it the script below 

<title> Simple Polling </ title>
<style type="text/css">
<! --
. style1 (font-size: 24px)
. style2 (color: # 0000FF)
->
</style>
<form name="form1" method="post" action="index.php?content=polling">
<p>
Polling <label> <span class="style1"> </span> </label>
</p>
<p>
<label> Which web programming language do you prefer? </label>
</p>
<p>
<label>
<input type="radio" name="vote" value="php">
PHP </label>
<br>
<label>
<input type="radio" name="vote" value="asp">
ASP </label>
</p>
<p>
<label>
<input type="submit" name="Submit" value="Submit">
</label>
<br>
</p>
</form>
<?php
if (@$_GET['content'])
{
$vote = $_POST['vote'];
if ($vote == "") // if no choice yet
{
echo "You have not <font color=red> fill surveys </font>";
exit;
}
if ($vote == "php") // add 1 for  php
{
$open = fopen("php.txt", "r");
$read = fgets($open, 65535);
fclose ($open);
$open = fopen("php.txt", "w");
$read++;
fwrite ($open, $read);
fclose ($open);
}
if ($vote == "asp") // add 1 for asp
{
$open = fopen ("asp.txt", "r");
$read = fgets ($open, 65535);
fclose($open);
$open = fopen("asp.txt", "w");
$read++;
fwrite($open, $read);
fclose($open);
}
$open_php = fopen("php.txt", "r");
$readphp = fgets($open_php, 65535); // read the value for php
fclose($open_php);
$open_asp = fopen ("asp.txt", "r");
$readasp = fgets($open_asp, 65535); // read the value for asp
fclose($open_asp);
$total_user = $readphp + $readasp; // the number of voters
$persentase_php = ($readphp / $total_user) * 100;
$persentase_asp = ($readasp / $total_user * 100);
echo "Total voters: $total_user <br>";
echo "PHP:"; printf("% 1.0f", "$persentase_php"); echo "% <img src=poll.jpg width =$persentase_php  $height = 10 /> $readphp voters <br> ";
echo "ASP"; printf("% 1.0f", "$persentase_asp"); echo "% <img src = poll.jpg width =$persentase_asp  $height = 10 /> $readasp voters <br> ";
}
?>

Creating a file based log system

If we have a website we would certainly want to know the impressions of our website visitors. 

In addition to knowing about information of visitors, a log file is also useful to help analyse the security.

I will go directly to explain the log system file in PHP script: 

<?php 
// checking the bowser's type 
$agent = $_SERVER ['HTTP_USER_AGENT']; 

// * checking where the script is executed from -- GET (URL) 
$uri = $_SERVER ['REQUEST_URI']; 

// * checking visitor's IP 
$ip = $_SERVER ['REMOTE_ADDR']; 

/ * checking where the script is referred from 
$ref = $_SERVER ['HTTP_REFERER']; 

// * checking visitor's proxy
$original = $_SERVER ['HTTP_X_FORWARDED_FOR']; 

// * checking visitor's connection 
$via = $_SERVER ['HTTP_VIA']; 

// * variable date 
$dtime = date ('r'); 

// * note if  visitors use transparent Proxy 
// * Then the $_SERVER ['HTTP_X_FORWARDED_FOR'] will display the visitor's IP 
// * otherwise $_SERVER [ 'REMOTE_ADDR'] will display the Proxy 
// * For clear about Proxy i will explain in another tutorial 

// * This is a description of variable entry_line: 
$entry_line = "Time: $dtime | Original IP: $ip | Browser: $agent | URL: $uri | referrer: $ref | Proxy: $original | Connection: $via 
";   // * <- Attention! This must be a new line or you press enter to create a new line 

/ * "fopen ()" function for opening files, "a" is the most important.!, 
/ * This works if the file "trace.txt" does not exist in the server then PHP will create 
$fp = fopen ( "trace.txt", "a"); 

/ /* "fputs ()" function for writing into the log file 
fputs ($fp, $entry_line); 

// * "fclose ()" function to close the file 
fclose ($fp); 

?> 

This file based log system using php function is pretty useful instead of using mysql connection