From Code Trash
Jump to: navigation, search

Sanitize File Names

Users often upload files which have quotes (’, “) in the filenames. Its important that you strip out these characters from the filename before saving the file. For example, a user uploads a file named o’brian.txt . If magic_quotes_gpc settings is turned on for your server, all quotes will be automatically escaped by a backslash.

This additional backslash (\) will result in file name o\‘brian.txt . You might face problems accessing this file through your FTP client. That is, you might not be able to download or delete this file using FTP.

//Sanitize the filename (See note below)
$remove_these = array(' ','`','"','\'','\\','/');
$newname = str_replace($remove_these,'',$_FILES['image']['name']);
//Make the filename unique
$newname = time().'-'.$newname;



Do not use DOCUMENT_ROOT and use the following...

I found variations in some sites where file path is different from document root.

May be i am not yet clear with what exactly document root does.

anyway here i have my workaround...

$file = __FILE__;
$root = str_replace('config\config.php','',$file);
$root = str_replace('config/config.php','',$file);
echo '<br>';
echo "root = ".$root;
echo '<br>';
echo "doc root".$_SERVER['DOCUMENT_ROOT'];

Here just get the file path of the current file and replace the filename to get document root.


Get file list from a folder

$addpath = '' // you can add it like /foldername/foldername if the files are not in the root folder
$list = scandir($docroot.$addpath.'photos/photos');
$list = array_reverse($list);
$images = $list;

The first two entries will '.' and '..' which represents current directory and parent directory. the two array pop us used to remove after reversing the array. if you want you can arrange it in any order by using various php array sort functions.

Math Spam Code

function getscode()
	$_SESSION['lo'] =  mt_rand(1,9);
	$_SESSION['ro'] =  mt_rand(1,9);
	return $_SESSION['lo'] .' + '.$_SESSION['ro'];
function checkscode($val)
	return (($_SESSION['lo'] + $_SESSION['ro'])==$val)?true:false;

Force Download

//select fn from tablename where id=$_request[id]
//assume that the file is in the junk named folder
// then the force download script will look like the following
$filename = "doc_fgfgfg/$fn";
header("Cache-Control: no-store");
header("Expires: 0");
header("Content-Type: application/octet-stream");
header("Content-disposition: attachment; filename=\"".basename($filename)."\"");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));

Check domain existence

 		$do = split(',',$domain);
			$here = dns_get_record($host.$do[$i], DNS_ANY, $authns, $addtl);
				$str.="<div class='dored'>$host$do[$i] is unavailable</div>";
				$str.="<div class='dogreen'>$host$do[$i] is available</div>";			
		if($flag)$str.="<span style='display:none'>bien</span>";

Other Resources

PHP IMAP Using Sockets
XML DOM Document in php
Find Files

Program written in php to list all files using recursion

Recursion Example

Program written in php to understand the way find files would work

Custom Date Functions

These are custom function which can be used for date calculation.

Remove chars from string using regex in php
Age Calculation
Remove new line chars carriage return and line feed crlf from a string
     ereg_replace("(\r\n|\n|\r)", "", $str);


  • as ususal

PHP Project Precautions

Replace width and height of the flash embedded code

PHP array to json string to send it to javascript using ajax

Copied from the following URL. For more information refer the following url.

Find document_root using __FILE__ instead of using $_SERVER['DOCUMENT_ROOT']

decrypting query string back into $_GET

convert php stdobject to an array

PHP Sockets - Socket Programming in PHP

PHP Find Files and Recursion

PHP DOMDocument simple dom xml

Parsing chunked encoding in php

Find max value in an associative array


$array = array(
    0 => array(
        'key1' => '123',
        'key2' => 'values we',
        'key3' => 'do not',
        'key4' => 'care about'
    1 => array(
        'key1' => '124',
        'key2' => 'values we',
        'key3' => 'do not',
        'key4' => 'care about'
    2 => array(
        'key1' => '125',
        'key2' => 'values we',
        'key3' => 'do not',
        'key4' => 'care about'

echo max(array_column($array, 'key1'));

Reference Highest value of an associative array stackoverflow