Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

shantanu oak http://oksoft.blogspot.com

« Newer Snippets
Older Snippets »
Showing 11-20 of 25 total

Display all files in a directory

<?
/**
* Change the path to your folder.
* This must be the full path from the root of your
* web space. If you're not sure what it is, ask your host.
*
* Name this file index.php and place in the directory.
*/
    // Define the full path to your folder from root
    $path = "/home/content/s/h/a/shaileshr21/html/download";
 
    // Open the folder
    $dir_handle = @opendir($path) or die("Unable to open $path");
 
    // Loop through the files
    while ($file = readdir($dir_handle)) {
 
    if($file == "." || $file == ".." || $file == "index.php" )
 
        continue;
        echo "<a href=\"$file\">$file</a><br />";
 
    }
    // Close
    closedir($dir_handle);
?> 

Alternating Row Colors

// tutorial from http://www.webreference.com/programming/php_color/

<?php  
// TEST1: FOR - Control  
for ($k = 0; $k < 10000000; $k++) {  
$class = (1) ? "bg1" : "bg2";  
}  

// TEST2: FOR - Remainder division  
for ($k = 0; $k < 10000000; $k++) {  
$class = ($k % 2 == 1) ?"bg1" : "bg2";  
}  

// TEST3: FOR - Negation  
$b = true;  
for ($k = 0; $k < 10000000; $k++) {  
$class = ($b) ? "bg1" : "bg2";  
$b = !$b;  
}  

// TEST4: WHILE - Control  
$k = 0;  
while ($k < 10000000) {  
$class = (1) ? "bg1" : "bg2";  
$k++;  
}  

// TEST5: WHILE - Remainder division  
$k = 0;  
while ($k < 10000000) {  
$class = ($k % 2 == 1) ?"bg1" : "bg2";  
$k++;  
}  

// TEST6: WHILE - Negation  
$k = 0;  
$b = true;  
while ($k < 10000000) {  
$class = ($b) ? "bg1" : "bg2";  
$b = !$b;  
$k++;  
}  
?>  


Validate email and domain

// Vlidate email and domain name
<?php

function validate_email($email){

$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";

if(eregi($exp,$email)){
	if(checkdnsrr(array_pop(explode("@",$email)),"MX")){
		print("$email is ok.<br>");
			}else{
			print("$email is ok. But domain is not.<br>");
			}
				}else{
				print("$email is not ok.<br>");
				}   
}

validate_email("shantanu.ok");
validate_email("shantanu.ok@gmail.com");
validate_email("shantanu.ok@fsjaldkfjlsfjsljflsfjsldk.com");

?>

Remove primary links

// remove primary links
// in the module - page.tpl.php 
// comment the three lines as shown...
<!-- Navigation Level 1 -->
<?php #if (isset($primary_links)) : ?>
<?php #print theme('links', $primary_links, array('class' => 'nav1')) ?>
<?php #endif; ?> 1

Change the word username in the form


// user.module -  function user_login_block() change username and password words

// form.inc - function theme_form_element($element, $value)  t('!title:132

login destination

// description of your code here

// login destination module enabled
  global $user; $myid = $user->uid;
return ($user->uid == 1) ? 'admin/'.$myid : 'user/'.$myid.'/edit/Address' ;

Remove the | sign in the title

// Remove the | sign in the title
// phptemplate.engine line 189 change it to :

$head_title = array(strip_tags(drupal_get_title())); 

Remove the "request new password" link

// Remove the "request new password" link

// user.module 
// Just comment (or remove) the following line in user_login_block function:
// $items[] = l(t('Request new password'), 'user/password', array('title' => t('Request new password via e-mail.')));

Disable RSS logo in Drupal

// To disable RSS logos both in the address bar and on pages in Drupal 5.1 :

// Edit theme.inc to disable feed icons

function theme_feed_icon($url) {
//  if ($image = theme('image', 'misc/feed.png', t('Syndicate content'), t('Syndicate content'))) {
//    return '<a href="'. check_url($url) .'" class="feed-icon">'. $image. '</a>';
//  }
}

// AND also edit common.inc

function drupal_add_feed($url = NULL, $title = '') {
  static $stored_feed_links = array();

/*  if (!is_null($url)) {
    $stored_feed_links[$url] = theme('feed_icon', $url);

    drupal_add_link(array('rel' => 'alternate',
                          'type' => 'application/rss+xml',
                          'title' => $title,
                          'href' => $url));
  }*/
  return $stored_feed_links;
}



FTP to get a file from within PHP

$conn_id = ftp_connect("www.yoursite.com");
$login_result = ftp_login($conn_id, "username", "password");

if ((!$conn_id) || (!$login_result)) {
echo "FTP connection has failed!";
exit;
} else {
echo "Connected";
}

// get the file
$local = fopen("local.txt","w");
$result = ftp_fget($conn_id, $local,"httpdocs/trlog.txt", FTP_BINARY);

// check upload status
if (!$result) {
echo "FTP download has failed!";
} else {
echo "Downloaded ";
}

// close the FTP stream
ftp_close($conn_id);
« Newer Snippets
Older Snippets »
Showing 11-20 of 25 total