Multiple file upload – PHP

<script type=”text/javascript”>

function addfileImages() {

document.getElementById(“totalfileImages”).value = Number(document.getElementById(“totalfileImages”).value) + Number(1);

if(Number(document.getElementById(“totalfileImages”).value)>Number(10)){ document.getElementById(“addButton”).style.display=”none”; document.getElementById(“totalfileImages”).value=10; }

document.getElementById(“fileImages”+document.getElementById(“totalfileImages”).value).style.display=”block”;

}

function removefileImages() {

document.getElementById(“fileImages”+document.getElementById(“totalfileImages”).value).style.display=”none”; document.getElementById(“totalfileImages”).value = Number(document.getElementById(“totalfileImages”).value) – Number(1);

if(Number(document.getElementById(“totalfileImages”).value)<Number(10)) document.getElementById(“addButton”).style.display=”block”;

}

</script>

<?php

if(isset($_POST[‘upload’])) {

extract($_POST);

for($i=1;$i<=$totalfileImages;$i++) {

if($_FILES[‘file_image’.$i][‘name’]!=””){

$ext =array_pop(explode(“.”,$_FILES[‘file_image’.$i][‘name’]));

$card_image =”uploads/”.substr(md5(uniqid(rand(),1)),0,32).”.”.$ext;

move_uploaded_file($_FILES[‘file_image’.$i][‘tmp_name’],$card_image) or die(“Could not upload!! make sure that uploads/is writable”);

chmod($card_image,0777) or die(“Could not change permission”);

$success=5;

}else{

$errorList[0]=”Upload all the files “;

} } }?>

<form action=”#” method=”post” enctype=”multipart/form-data”>

<table>

<?php if(count($errorList)>0){ ?>

<tr> <td>&nbsp;</td> <td><div style=”margin:5px; color:#FF0000;”><?php foreach($errorList as $value) echo $value.” ,”; ?></div></td> </tr>

<?php } ?>

<?php if($success==5){ ?>

<tr> <td>&nbsp;</td> <td><div style=”margin:10px; color: #006633; width:90%; font-weight:bold”>File has been uploaded</div></td> </tr>

<?php } ?>

<tr> <td valign=”top”>Select file</td> <td> <?php $maxFiles=10; ?> <div style=”width:99%”> <input type=”file” name=”file_image1″ style=”width:258px;” /></div>

<?php for($i=2;$i<=$maxFiles;$i++) { ?>

<div style=”width:99%; display:none” id=”fileImages<?= $i ?>”>   <input type=”file” name=”file_image<?= $i ?>”  /><a href=”javascript:removefileImages();”>Delete</a> </div>

<?php } ?>

<div align=”right” id=”addButton”><a href=”javascript:addfileImages()”  >Add more</a></div>

<input type=”hidden” id=”totalfileImages”  name=”totalfileImages” value=”1″ /> </td>

</tr>

<tr> <td>&nbsp;</td> <td><input name=”upload” type=”submit”  value=”   Upload   ”  /> </td> </tr>

</table>

</form>

File upload – PHP

<?php

if(isset($_POST[‘upload’])) {

if($_FILES[‘uploadFile’][‘name’]!=””) {

$ext =array_pop(explode(“.”,$_FILES[‘uploadFile’][‘name’]));

$name_img = substr(md5(uniqid(rand(),1)),0,32);

$uploadFile =”uploads/”.$name_img.”.”.$ext;

move_uploaded_file($_FILES[‘uploadFile’][‘tmp_name’],$uploadFile) or die(“Could not upload!! make sure that ../uploads/’ is writable”) ;

chmod($uploadFile,0777) or die(“Could not change permission”);

$success=5;

}else{

$errorList[]=”Select the file.”;

}

}

?>

<form action=”#” method=”post” enctype=”multipart/form-data”>

<table>

<?php if(count($errorList)>0){ ?>

<tr> <td>&nbsp;</td> <td><div style=”margin:5px; color:#FF0000;”><?php foreach($errorList as $value) echo $value.” ,”; ?></div></td> </tr>

<?php } ?> <?php if($success==5){ ?>

<tr> <td>&nbsp;</td> <td><div style=”margin:10px; color: #006633; width:90%; font-weight:bold”>File has been uploaded</div></td> </tr>

<?php } ?>

<tr>

<td>Select file</td> <td><input name=”uploadFile” type=”file”  /> 85×21</td> </tr> <tr> <td>&nbsp;</td> <td><input name=”upload” type=”submit”  value=”   Upload   ”  /> </td>

</tr>

</table>

</form>

 

switch statement – Javascript

<script type=”text/javascript”>

function displaySize(sizes){

switch (sizes) {

case “header”:sizeDim=’728 x 90′;break;

case “footer”:sizeDim=’500 x 150′;break;

case “sidebar”:sizeDim=’7263 x 181′;break;

}

document.getElementById(‘dimension’).innerHTML=sizeDim;

}

</script>
<table>

<tr>

<td>Select Position</td> <td>

<select name=”banner_position” onchange=”javascript:displaySize(this.value);”>

<option value=”>Select the position</option>

<option value=’header’  >Header</option>

<option value=’footer’ >Footer</option>

<option value=’sidebar’  >Sidebar</option>

</select>

</td>

</tr>

<tr>

<td>Upload image</td> <td> <input type=”file” name=”image” />(Size should be <span id=”dimension”></span>) </td>

</tr>

</table>

Contact us form – PHP

<?php

//PHP Code to send mail using the data from contact form

extract($_POST);

if(isset($_POST[‘send’])){

//Validation contact form fields

if(preg_replace(“/^s*$/”,””,$firstname)==”” )

$errorList[]=”Select the department name is required.”;

if(preg_replace(“/^s*$/”,””,$email)==”” )

$errorList[]=”Email is required.”;

elseif(!eregi(“^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$”, $email))

$errorList[] = “Email address is invalid.”;

if(preg_replace(“/^s*$/”,””,$message)==”” )

$errorList[]=”Message is required.”;

//If no error send  mail

if(count($errorList)==0){

$toAddres = “mail@example.com”; //Give desired emaill address to whcih mail has to be sent

$subject = “Contact from – Your Site Name”;

$headers    = “MIME-Version: 1.0n”;

$headers   .= “Content-type: text/html; charset=iso-8859-1n”;

$headers   .= “From: “.$email.”n”;

$headers   .= “Return-Path: “.$toAddres.”n”;

$headers   .= “Return-Receipt-To: “.$toAddres.”n”;

$mailMessage = ‘<table width=”687″ >

<tr><td width=”161″>Hi Admin</td><td width=”7″></td><td width=”561″></td></tr>

<tr><td width=”161″></td><td width=”7″></td><td width=”561″></td></tr>

<tr><td width=”161″>Name</td><td width=”7″></td><td width=”561″>’.$firstname.'</td></tr>

<tr><td width=”161″>Message</td><td width=”7″></td><td width=”561″>’.$message.'</td></tr>

<table>’;

@mail($toAddres,$subject, $mailMessage, $headers); $success=5; } }?>

// Contact form
<form action=”#” method=”post”>

<table>

<?php if(count($errorList)>0){ ?>

<tr>

<td>&nbsp;</td><td><div style=”margin:5px; color:#FF0000;”><?php foreach($errorList as $value) echo $value.” ,”; ?></div></td></tr>

<?php } ?>
<?php if($success==5){ ?>

<tr>

<td>&nbsp;</td> <td><div style=”margin:10px; color: #006633; width:90%; clear:none; float:left; font-weight:bold”>Your message has been sent to site admin .</div></td>

</tr>

<?php } ?>
<tr>

<td>Please enter your <strong>name</strong>:</td> <td><input name=”firstname” type=”text” value=”<?php echo $firstname ?>” /></td>

</tr>

<tr>

<td>Please enter your <strong>email address </strong>:</td> <td><input name=”email” type=”text” value=”<?php echo $email ?>” /></td>

</tr>

<tr>

<td>Please enter message:</td> <td><textarea name=”message” style=”height:50px;”><?php echo nl2br($message); ?></textarea></td>

</tr>
<tr>

<td>&nbsp;</td> <td><input type=”submit” name=”send” value=”  Send  “></td>

</tr>

</table>

WordPress query to fetch links under a given category

<?php

// ‘links’ is the slug name of the link category link

$linkeQry=”SELECT * FROM wp_links as link INNER JOIN wp_term_relationships ON (link.link_id = wp_term_relationships.object_id)

INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) AND wp_term_taxonomy.taxonomy = ‘link_category’

INNER JOIN {$wpdb->prefix}terms as c ON c.term_id=wp_term_taxonomy.term_id

WHERE c.slug=’links'”;

$linksData = $wpdb->get_results($linkeQry);

?>

<ul>

<?php foreach($linksData as $key=>$link) {  ?>

<li><a href=”<?php echo $link->link_url; ?>” target=”_blank”><?php echo $link->link_name; ?></a></li>

<?php } ?>

</ul>