How get all the PDF files from directory and sub directory


For check pdf files in directory and sub directory

First we have to read folder and we will get directory and file name from folder.

then we will check is directory or not.

if we get directory then we again call the same function for get sub directory and file from current directory

and we will separate pdf file name from file extension.

Following code will help you for get pdf file list from mentioned directory name or we can also get all files from directory

$directory = 'typo3/fileadmin';

function expandDirectories($base_dir) {

    $directories = array();

    foreach(scandir($base_dir) as $file) {
		
        if($file == '.' || $file == '..') continue;

        $dir = $base_dir.DIRECTORY_SEPARATOR.$file;

        if(is_dir($dir)) {
            $directories = array_merge($directories, expandDirectories($dir));
        }else{
            if(strstr($dir,".pdf")){
                $directories []= $dir;
            }
        }
    }
    return $directories;
}

$directories = expandDirectories($directory);

print_r($directories);

You will get list of pdf file with directory name.

i hope it will work for you 🙂 njoy 😉

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

Generate ZIP file using php


 
Following post will helps you to create a ZIP file using PHP.

We can zip more than one file, Following script will help you to generate zip file and download zip file

<?php

$arr = array("jainish.png","jainish1.png");

if(extension_loaded('zip'))
{
    // Checking ZIP extension is available
    
    $zip = new ZipArchive(); // Load zip library
    
    $zip_name = "jainish-".time().".zip"; // Zip name
    
    if($zip->open($zip_name, ZIPARCHIVE::CREATE)!==TRUE)
    {
        // Opening zip file to load files
        $error .= "* Sorry ZIP creation failed at this time";
    }

    $i=1;
    
    foreach($arr as $file)
    {
        $zip->addFile($file,$i."-".basename($file)); // Adding files into zip
        $i++;
    }
    
    $zip->close();
    
    if(file_exists($zip_name))
    {
        // push to download the zip
        header('Content-type: application/zip');
        header('Content-Disposition: attachment; filename="'.$zip_name.'"');
        readfile($zip_name);
        // remove zip file is exists in temp path
        unlink($zip_name);
    }
    
}else{
    $error .= "* You dont have ZIP extension";
}

?>

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

ZipArchive is not working in Extbase + typo3


 
When we create a new zips “$zip = new ZipArchive();”

However, we get always the following error message:

Fatal error: Class ‘TYPO3 \ .. \ Domain \ Repository \ ZipArchive’ not found in … / Classes / Domain / Repository / XXXRepository.php on line 143.

Zip works nicely on the server. A simple PHP script which zips a file running properly in server.

In Extbase we have to add backward slash [ \ ] infront of class name.

Following script should do the trick.

$zip = new \ZipArchive();

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

How to trim white spaces of array values in php


 
Many time we want to trim array value dynamically. But we always go with foreach and trim that value.

If we are not aware from “array_map” function then we used simply foreach function and used trim function.

But its better to use following function. It will directy trim all value of array.

$arr = array('  Jainish ',' S  ','            Senjaliya ');
		
$trimmed_array  =  array_map('trim',$arr);

print_r($trimmed_array);

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

Dropdown box to have the values for the next 30 days in PHP


 
Selection box to get value for next 30 days.

Following PHP script should do the trick. IT will get next 30 days list.

for($j = 0; $j < 30; $j++) {

    // Get the current year, month, and day
   
    $year  = date('y');
    $month = date('m');
    $day   = date('d') + $j;
	
    // Make sure we do not have too many days for the current month...if we do, subtract the two
    
    if($day > date('t')) {
	
        $day -= date('t');
        
        // We have increased a month too, by the way
        $month += 1;
		
        // Make sure we compensate for a new year
        if($month > 12) { $month = 1; $year++; }
    }
	
    // Now make a timestamp of the current information
    $stamp = mktime(0,0,0,$month,$day,$year);
    
    // Substitue this with your dropdown code
    print date('D M d, Y', $stamp);
	echo "<br>";
} 

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

How to redirect old url to new url using htaccess, php and meta


 


 
If you move your site without loosing your search engine ranking and incoming links then following script will be helping you

Your OLD URL is : http://www.hariomrubber.com/request-a-quote.html
and you want to redirect it on new url like
New Rewrite URL: http://www.hariomrubber.com/contact-us.html

then you have to use below script in your .htaccess file
Below script is a rewrite URL htaccess

RewriteCond %{REQUEST_URI} ^/request-a-quote.html$
RewriteRule .* /contact-us.html [L,R=301]

if you open old url then it will be redirect to new url.

Or, if you simply want to redirect all URLs from the old domain to a specific page on the new domain
then add this to the root .htaccess file on the old domain:

RedirectMatch 301 / http://www.hariomrubber.com/about-us.html

Redirect from one file type to another

If you want to change the extension of your web pages, Like .php to .html and file names remaining the same then add below script to your .htaccess:

RedirectMatch 301 /(.*)\.php http://hariomrubber.com/$1.html

Removing file extension via .htaccess

If you have the following URLs for your website:
http://www.hariomrubber.com/about-us.html

However, you would like to hide file extensions from the end users, and allow them to access to the files using the following URLs:
http://www.hariomrubber.com/about-us

then you have to use following in your .htaccess file

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Replace html with your file extension, eg: html, php, asp

Alternate redirect methods via meta tag

Redirect via tag in the document. below meta tag add after your head tag in your html

<head><meta http-equiv="refresh" content="3; url=http://hariomrubber.com/">

Alternate redirect methods via PHP

To redirect via PHP, add following code in your file:

     header ('HTTP/1.1 301 Moved Permanently');
     header( "http://hariomrubber.com/" );

Will be add more blog about htaccess file.

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

Creating downloadable CSV files using PHP OR PHP create csv file and force download


 
How to download csv file in php

CSV (comma-separated values) is the most widely supported format for transferring tabular data between applications. The ability to export data in CSV format is a useful feature for many programs, and is becoming increasingly common in web applications. This page explains how to use PHP to create CSV files, and how to ensure that your visitor’s browser offers to download the file instead of displaying it.

The following code assumes that the data to be exported

$content  = "Dhara,Senjaliya";
$content .= "\n";
$content .= "Jainish,Senjaliya";
$content .= "\n\n";
$content .= "Successfully download";		

$filename = 'CSV_FILE_NAME.csv';

$mimeType = 'application/octet-stream';
header('Content-Type: application/vnd.ms-excel');
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename="' . $filename . '"');

if (strstr($_SERVER['HTTP_USER_AGENT'],'MSIE'))
{
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
} else {
    header('Pragma: no-cache');
}

echo "$content"; 
die;

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

 


 

How to read stdClass object in php


 
How would I read this array (“stdClass Object”)

Object is not an array, but rather, well, an Object. So use the -> operator to access its properties:

If you currently have the following object:- [If you print your data and it will look like below]

stdClass Object
(
    [name] => Jainish Senjaliya
    [contact] => Array
        (
            [0] => stdClass Object
                (
		    [id] => 143
                    [email] => jainish.online@gmail.com
                    [URL] => http://www.hariomrubber.com
                    [phone] => 99******93
                )
        )
) 

For example, to get the name, you would do:

echo $data->name;
Output : Jainish Senjaliya

It contains a property which itself is an array of additional objects. For example, to get the URL of id 143, you would do:

echo $data->contact[0]->URL;

Output : http://www.hariomrubber.com

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

 


 

How to read SimpleXML Object in PHP


 
How can I properly parse a SimpleXML Object in PHP?

It is really simple to access attributes using array form. However, you must convert them to strings or ints if you plan on passing the values to functions.

You have to cast simpleXML Object to a string.

$value = (string) $xml->code[0]->lat;

If you know that the value of the XML element is a float number (latitude, longitude, distance), you can use (float)

$value = (float) $xml->code[0]->lat;

Also, (int) for integer number:

$value = (int) $xml->code[0]->distance;

You can more read about simplexmlelement.

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

How to check url exits or not in PHP


How to check file exist in given URL – PHP.

FOr check url exist or not you have to use below function for it.

you can also check for xml path exits or not.


	$url = "www.hariomrubber.com/sitemap.xml";
	
	if($this->is_url_exist($url))
	{
		echo "URL is exists";
	}else{
		echo "URL is not exists";
	}

	function is_url_exist($url)
	{
		$ch = curl_init($url);    
		curl_setopt($ch, CURLOPT_NOBODY, true);
		curl_exec($ch);
		$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	
		if($code == 200){
			$status = true;
		}else{
			$status = false;
		}
		curl_close($ch);
		return $status;
	}
	

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya