Directory create,copy files etc using PHP command.


This tutorial will teach you how to play with directory using PHP.

PHP Code to make folder

mkdir(‘Destination’.”/”.’Folder Name’);

PHP code to copy files from one to another folder

$d = dir( $source );
while ( FALSE !== ( $entry = $d->read() ) ) {
if ( $entry == ‘.’ || $entry == ‘..’ ) {
continue;
}
$Entry = $source . $entry;
copy( $Entry, $target . $entry );
}
$d->close();

CakePHP code to show images from folder

<?php

$dir = $destination.$theme[‘theme_name’].”/”;
$imagelink = ‘/theme_folder/’.$theme[‘theme_name’].”/”;
$pic_types = array(“jpg”, “jpeg”, “gif”);
$folder = opendir($dir);
while ($file = readdir ($folder)) {
$image = $imagelink.$file;
if(in_array(substr(strtolower($file), strrpos($file,”.”)+ 1),$pic_types)){
if( $file ==  $theme[‘screen_shot’] ){}
else if( $file ==  $theme[‘screen_shot_thumb’] ) {}
else if( strstr($file , ‘header_’) ) {}
else if( strstr($file , ‘footer_’) ) {}
else {
echo “<li>”.$this->Html->link(
$this->Html->image(“$image”,array(‘alt’=>’No image’,’width’=>’160′,’height’=>’80’)), “javascript:void(0)”, array(‘escape’ => false, ‘onclick’ => “openImage(‘$file’,$themeID)”)
).”</li>”;
}

}
}
closedir($folder);

?>

PHP code to delete files with directory:

<?
$mydir = “/path/to/dir/”;
$d = dir($mydir);
while($entry = $d->read()) {
if ($entry!= “.” && $entry!= “..”) {
unlink($entry);
}
}
$d->close();
rmdir($mydir);
?>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s