Drupal forgot password for admin - best method to reset with PHP file. (If you have FTP Access)

You have forgot your Drupal website's password! That's horrible. But dont worry, we have a better way to reset password if you have access to your files.

If you remember / have access to the admin email, its OK you can reset by going to forgot password page and by entering your email ID for admin account.

If not, you can try this method. I have combined snippets from around the web to make it work even if you have got blocked from login.


Read Article →

Copy file from remote server url to our own server


Here is how we can Save a file from url using PHP.

<?php
$file = 'http://somewebsite.com/somefile.avi';
$newfile = 'video.avi';

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
?>


Code Explained

<?php
$file = 'http://somewebsite.com/somefile.avi';  //Store the path to remote file to variable $file.
$newfile = 'video.avi'; //Declaring  where and in what name the remote file to be saved on our server.

if (!copy($file, $newfile)) { //Checking if there any errors in copying if do
    echo "failed to copy $file...\n"; //Displaying a message in page as "Copy failed".
}
//if there is no errors, the file will be copied when the page loading ends.
?>


PHP Function used

copy()

Read Article →

Redirection using php and javascript

Here is how you can redirect your current page to a  new declared url. suing php and javascript at the same time.
If you are using this script in iframe, it will help you to open page in the parent page,and not inside the iframe.

<?php
$redirecturl='http://aslamise.blogspot.com';
echo "<script type='text/javascript'>window.open('$redirecturl', '_parent', '')</script>";
?>


Code Explained

<?php
$redirecturl='http://aslamise.blogspot.com'; //Assigning the redirect url
echo "
<script type='text/javascript'>
window.open('$redirecturl', '_parent', '')//Window.open = Opening the window in the specified address
                                                            $redirecturl= The Url we assigned just before.
                                                             _parent =opening on the main.even if the script goes inside iframe-
                                                              It will be opened in main window and not inside iframe.
</script>
";
?>

Javascript function used

window.open()

Read Article →

Extract a zip file using php


Here is how you can extract a .zip file using php on your server.
Change zipfile.zip to the name of your zip file.
Declare the path ,the contents to be extracted to. if not specified, it will be in root directory

<?php
$zip = new ZipArchive;
$res = $zip->open('zipfile.zip');
if ($res === TRUE) {
  $zip->extractTo('./');
  $zip->close();
  echo 'Success!';
} else {
  echo 'Failed';
}
?>

Code Explained


<?php
$zip = new ZipArchive; //Storing the filetype and format to variable to $zip.
$res = $zip->open('zipfile.zip');  //Trying to open .zip file.
if ($res === TRUE) { //If Opening .Zip file is success,
  $zip->extractTo('./'); //extract the contents to specifed folder.
  $zip->close(); //Closing file.
  echo 'Success!';//Dispaling text "success!" on the page.
} else { //If Opening of .zip file fails, then
  echo 'Failed';// Displaying a message "Failed". on the page.
}
?>



Read Article →

Delete a folder using php


You can delete a folder using this php script
Change the DIRECTORY to the directory you want to delete.

<?php
$dir='/DIRECTORY/';
rmdir( $dir );
?>

Code Explained


<?php
$dir='/DIRECTORY/'; //Storing the directory path to variable $dir.
rmdir( $dir ); //Removing Directory
?>


Function Used 

rmdir() 

rmdir() used to remove the directory



Read Article →

Delete files in a directory older than a specific time using php


Here is how you can delete all files in a folder those are older than a specific time,using php.

Change DIRECTORYNAME to the name of  directory which you want the contents to be deleted.
Copy and paste this to any text editor. save as .php file.upload to your server.

<?php
$DIR = './DIRECTORYNAME/';
if ($handle = opendir($DIR)) {

// This is the correct way to loop over the directory.
    while (false !== ($file = readdir($handle))) {
        if ( filemtime($DIR.$file) <= time()-60) {
           unlink($DIR.$file);
        }
    }

    closedir($handle);
}
else
{
echo "cannot open dir";
}
?>


Code Explained

<?php
$DIR = './DIRECTORYNAME/'; //Storing directory path to variable $DIR.
if ($handle = opendir($DIR)) { //Checks whether there is any errors,(read and write permission)-or
                                                 directory exist.
// This is the correct way to loop over the directory.
    while (false !== ($file = readdir($handle))) {//Looping over files till the last one.
        if ( filemtime($DIR.$file) <= time()-60) {//Checking  filemtime(file modified time) of each time and
                                                                    checks whether its lower or higher than the specified time,if it is
                                                                    lower ,then
           unlink($DIR.$file);//deleting the file.
        }
    }

    closedir($handle); //Closing the directory
}
else//if there is any error with folder, then-
{
echo "cannot open dir"; //Displaying the error message 'Cannot open dir'
}
?>

Function Used

unlink()

Unlink() used to remove the file.



Read Article →

Count files in a directory using php


If you want to count how many files are in there in a folder using php, you can use this script.
Copy This script, Change DIRECTORYNAME to the directory you want to count files, then save it as a .php file, upload to your server.

<?php
$directory = "DIRECTORYNAME";
if (glob($directory . "*.*") != false)
{
 $filecount = count(glob($directory . "*.*"));
 echo $filecount;
}
else
{
 echo 0;
}
?>

Code Explained



<?php
$directory = "DIRECTORYNAME";  //Saving directory path to variable $directory.
if (glob($directory . "*.*") != false) //checks whether there is ANY files inside. if yes-
{
 $filecount = count(glob($directory . "*.*")); counting the files using "count" function.and save result
                    to  $filecount
 echo $filecount; //displays the result .
}
else //if there was no files,
{
 echo 0; //displaying a 0.
}
?>


Function used

count();

count fuction is use to count the files.

Read Article →