<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP &#8211; TUTORIALS PAGE</title>
	<atom:link href="https://tutorialspage.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>https://tutorialspage.com</link>
	<description>Free tutorials and daily notes</description>
	<lastBuildDate>Sat, 30 Oct 2021 22:44:31 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	
<site xmlns="com-wordpress:feed-additions:1">105742871</site>	<item>
		<title>How to use PHP to insert content after or before HTML tag</title>
		<link>https://tutorialspage.com/how-to-use-php-to-insert-content-after-or-before-x-th-html-element/</link>
					<comments>https://tutorialspage.com/how-to-use-php-to-insert-content-after-or-before-x-th-html-element/#respond</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Fri, 02 Feb 2018 12:56:38 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=1536</guid>

					<description><![CDATA[The problem is quite simple&#8230;you just want to count a specific tag on a page and insert some content. After or before it&#8217;s just a matter of perspective. So let&#8217;s say you want an ad, for example, to be inserted, after the fourth H2 tag. So let&#8217;s go and insert an ad code or any other &#8230; <a href="https://tutorialspage.com/how-to-use-php-to-insert-content-after-or-before-x-th-html-element/" class="more-link">Continue reading<span class="screen-reader-text"> "How to use PHP to insert content after or before HTML tag"</span></a>]]></description>
										<content:encoded><![CDATA[

The problem is quite simple&#8230;you just want to count a specific tag on a page and insert some content. After or before it&#8217;s just a matter of perspective. So let&#8217;s say you want an ad, for example, to be inserted, after the fourth H2 tag.

 

So let&#8217;s go and insert an ad code or any other content block after some HTML tag. Here we have a simple PHP function:

  <span id="more-1536"></span>  
<pre class="urvanov-syntax-highlighter-plain-tag">function interBlock($content, $ad=null, $element=&quot;&amp;lt;/h3&amp;gt;&quot;, $afterElement=3, $minElements=5) {
    // block to be inserted here
    if (!$ad) $ad ='&amp;lt;h1&amp;gt;Your block here&amp;lt;/h1&amp;gt;';

    // split content
    $parts = explode($element, $content);

    // count element occurrences
    $count = count($parts);

    // check if the minimum required elements are found
    if(($count-1)&amp;lt;$minElements) return $content;
    
    $output='';
    for($i=0; $i&amp;lt;$count; $i++) {
        // this is the core part that puts all the content together
       $output .= $parts[$i-1] . $element .  (($i==$afterElement) ? $ad : ''); // this will insert after</pre>
 

&#8230;and you can simply call it like this:

 
<pre class="urvanov-syntax-highlighter-plain-tag">echo interBlock($my_content, $myAd, '&amp;lt;/h2&amp;gt;', 4, 8);</pre>
 

This will place,  $myAd in $myContent, after the fourth occurrence of the closing tag &lt;/h2&gt;  if there are at least 8 occurrences of the mentioned tag. That was simple, was it? Now let&#8217;s get a little bit more complicated.

 
<h2 class="wp-block-heading">Insert block before HTML tag occurrence</h2>
 

And how about if you need to add the content, before the fourth title? Let&#8217;s do that. You will only have to change the tag to the HTML opening tag, change the order where all the content is printed.

 
<pre class="urvanov-syntax-highlighter-plain-tag">$output .=  (($i==$afterElement) ? $ad : '') .($i==1 ? $parts[0]:'') . $element . $parts[$i]; //this win insert before</pre>
 
<h3 class="wp-block-heading">Insert block before or after an alternative tag</h3>
 

Now let&#8217;s imagine we want to insert an ad after the third title on the page. But if we don&#8217;t have enough titles on the page we just want to place an ad after the fourth paragraph. That is a reasonable scenario, isn&#8217;t it?

 

So let&#8217;s modify our function. Instead of returning the content when the minimum number of elements is not found, we will just search for the $alternativeTag as we did in the first place.

 
<pre class="urvanov-syntax-highlighter-plain-tag">// check if the minimum required elements are found
    if(($count-1)&amp;lt;$minElements) {
        $parts = explode($altElement, $content); // check for the alternative tag
        $count = count($parts);
        if(($count-1)&amp;lt;$minElements) return $content; // you can give up here and just return the original content
        $element = $altElement; // continue by using alternative tag instead of the primary one
    }</pre>
 

At this point, you can just let the function continue because you are using the alternative tag.

 
<h2 class="wp-block-heading">And the final mixed function</h2>
 

This is how ower final PHP function will look like. You can use it to add content blocks after a certain number of HTML tag occurrences.

 
<pre class="urvanov-syntax-highlighter-plain-tag">function interBlock($content, $ad=null, $element=&quot;&amp;lt;h3&amp;gt;&quot;, $altElement='&amp;lt;h3&amp;gt;', $afterElement=1, $minElements=5) {
    // Ser your block to be inserted here
    if (!$ad) $ad ='&amp;lt;h1&amp;gt;Your block here&amp;lt;/h1&amp;gt;';

    // split content
    $parts = explode($element, $content);

    // count element ocurrencies 
    $count = count($parts);

    // check if the minimum required elements are found
    if(($count-1)&amp;lt;$minElements) {
        $parts = explode($altElement, $content); // check for the alternative tag
        $count = count($parts);
        if(($count-1)&amp;lt;$minElements) return $content; // you can give up here and just return the original content
        $element = $altElement; // continue by using alternative tag instead of the primary one
    }
    
    $output='';
    for($i=1; $i&amp;lt;$count; $i++) {
        // this is the core part that puts all the content together
        //$output .= $parts[$i-1] . $element .  (($i==$afterElement) ? $ad : ''); // this win insert after
        $output .=  ($i==1 ? $parts[0]:'') . (($i==$afterElement) ? $ad : '')  . $element . $parts[$i]; //this win insert before
    }
    return $output;
}</pre>
 

&#8230;.and this is the way you call it to insert a block before the fourth H2 tag. If you do not have 8 H2 it will use the H3 tag.

 
<pre class="urvanov-syntax-highlighter-plain-tag">echo interBlock($my_content, $myAd, '&amp;lt;h2&amp;gt;', '&amp;lt;h3&amp;gt;', 4, 8);</pre>
 

Since almost all the params have a default value into the function definition, you can just adjust that to your needs and just call the function with the required $my_content  var.

 
<pre class="urvanov-syntax-highlighter-plain-tag">echo interBlock($my_content);</pre>
 

Note:

 

Of course, there are other scenarios and you can build a  robust function or even class to achieve a more dynamic usage. For example, you can use an array of elements to split the content, you can add an if to switch between after and before depending on the tag structure, etc.

 

Feel free to do that, adapt it to your needs.

]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/how-to-use-php-to-insert-content-after-or-before-x-th-html-element/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1536</post-id>	</item>
		<item>
		<title>Simple OAuth2 authorization code grant example using PHP and cURL</title>
		<link>https://tutorialspage.com/simple-oauth2-example-using-php-curl/</link>
					<comments>https://tutorialspage.com/simple-oauth2-example-using-php-curl/#respond</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Mon, 18 Dec 2017 12:02:34 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=1516</guid>

					<description><![CDATA[The authorization code grant methods, should be very familiar if you’ve ever signed into an application using your Facebook or Google account. The flow is quite simple. The application redirects the user to the authorization server &#62;&#62; the user will then be asked to log in to the authorization server and &#62;&#62; approve access to his &#8230; <a href="https://tutorialspage.com/simple-oauth2-example-using-php-curl/" class="more-link">Continue reading<span class="screen-reader-text"> "Simple OAuth2 authorization code grant example using PHP and cURL"</span></a>]]></description>
										<content:encoded><![CDATA[<p>The authorization code grant methods, should be very familiar if you’ve ever signed into an application using your Facebook or Google account.</p>
<p>The flow is quite simple. The application redirects the user to the authorization server &gt;&gt; the user will then be asked to log in to the authorization server and &gt;&gt; approve access to his data. And if the user approves the application  &gt;&gt; he will be redirected back to the application.<span id="more-1516"></span></p>
<p>So let&#8217;s start preparing the authorization code grant example.</p>
<p>We are using PHP v5.6.32 and cURL enabled extension on a Windows localhost machine. If you are using XAMPP you normally just have to uncomment this line to have cURL enabled.</p><pre class="urvanov-syntax-highlighter-plain-tag">;extension=php_curl.dll</pre><p>you can find it in 
			<span id="urvanov-syntax-highlighter-69d4d10a5f837473872378" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-i">xampp</span><span class="crayon-sy">\</span><span class="crayon-i">apache</span><span class="crayon-sy">\</span><span class="crayon-i">bin</span><span class="crayon-sy">\</span><span class="crayon-i">php</span><span class="crayon-sy">.</span><span class="crayon-i">ini</span></span></span>  or 
			<span id="urvanov-syntax-highlighter-69d4d10a5f83a298018705" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-i">xampp</span><span class="crayon-sy">\</span><span class="crayon-i">php</span><span class="crayon-sy">\</span><span class="crayon-i">php</span><span class="crayon-sy">.</span><span class="crayon-i">ini</span></span></span> , depending on your XAMPP version and then restart Apache service.</p>
<p>So first you will need to collect your data and prepare some vars:</p><pre class="urvanov-syntax-highlighter-plain-tag">define("CALLBACK_URL", "http://localhost/oauth2client.php");
define("AUTH_URL", "https://example.com/oauth2/authorize");
define("ACCESS_TOKEN_URL", "https://example/oauth2/token");
define("CLIENT_ID", "1yLCsmAfDF49nGmJLgDbHvB6bSca");
define("CLIENT_SECRET", "g2OKQ9isj2pcaextQdjx5xW3KoAa");
define("SCOPE", ""); // optional</pre><p>Now build your first URL to call. Normally you will want to call this URL in a popup window like old school times. You can also try a modern modal and use an iFrame but that didn&#8217;t work for me.</p><pre class="urvanov-syntax-highlighter-plain-tag">$url = AUTH_URL."?"
   ."response_type=code"
   ."&amp;client_id=". urlencode(CLIENT_ID)
   ."&amp;scope=". urlencode(SCOPE)
   ."&amp;redirect_uri=". urlencode(CALLBACK_URL)
;</pre><p>After the user enters his credentials and gives application access the Identity provider will redirect to the CALLBACK_URL with a &#8220;code&#8221;.</p>
<p><img data-recalc-dims="1" fetchpriority="high" decoding="async" class="aligncenter wp-image-1520 size-full" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2017/12/oauth-wso2.png?resize=328%2C153&#038;ssl=1" alt="" width="328" height="153" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2017/12/oauth-wso2.png?w=328&amp;ssl=1 328w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2017/12/oauth-wso2.png?resize=300%2C140&amp;ssl=1 300w" sizes="(max-width: 328px) 85vw, 328px" /></p>
<p>You will see the code in the URL, so this is the GET method.</p><pre class="urvanov-syntax-highlighter-plain-tag">http://localhost/oauth2client.php?code=be0d0cb3-63ac-394a-a2a7-7365ddbbab7d&amp;session_state=59de172acce92f220baff0433ebe629ea4981e544e406ba718ad76ea122bbb69.fPJIegSopJKfshsjNrxq9A</pre><p>So just use GET to retrieve the code param. Like this: 
			<span id="urvanov-syntax-highlighter-69d4d10a5f848991789044" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">$code</span><span class="crayon-h"> </span><span class="crayon-o">=</span><span class="crayon-h"> </span><span class="crayon-v">$_GET</span><span class="crayon-sy">[</span><span class="crayon-s">'code'</span><span class="crayon-sy">]</span><span class="crayon-sy">;</span></span></span></p>
<p>This code is only valid for a short period of time and you will have to exchange the code for a token so you can make API calls. Here is the curl POST for receiving the token key:</p>
<p>&nbsp;</p><pre class="urvanov-syntax-highlighter-plain-tag">function getToken(){
  $curl = curl_init();

  $params = array(
    CURLOPT_URL =&gt;  ACCESS_TOKEN_URL."?"
    				."code=".$code
    				."&amp;grant_type=authorization_code"
    				."&amp;client_id=". CLIENT_ID
    				."&amp;client_secret=". CLIENT_SECRET
    				."&amp;redirect_uri=". CALLBACK_URL,
    CURLOPT_RETURNTRANSFER =&gt; true,
    CURLOPT_MAXREDIRS =&gt; 10,
    CURLOPT_TIMEOUT =&gt; 30,
    CURLOPT_HTTP_VERSION =&gt; CURL_HTTP_VERSION_1_1,
    CURLOPT_CUSTOMREQUEST =&gt; "POST",
    CURLOPT_NOBODY =&gt; false, 
    CURLOPT_HTTPHEADER =&gt; array(
      "cache-control: no-cache",
      "content-type: application/x-www-form-urlencoded",
      "accept: *",
      "accept-encoding: gzip, deflate",
    ),
  );

  curl_setopt_array($curl, $params);

  $response = curl_exec($curl);
  $err = curl_error($curl);

  curl_close($curl);

  if ($err) {
    echo "cURL Error #01: " . $err;
  } else {
    $response = json_decode($response, true);    
    if(array_key_exists("access_token", $response)) return $response;
    if(array_key_exists("error", $response)) echo $response["error_description"];
    echo "cURL Error #02: Something went wrong! Please contact admin.";
  }
}</pre><p>If everything goes fine, you should get a JSON response similar to this one:</p><pre class="urvanov-syntax-highlighter-plain-tag">{
  "access_token": "bd9abc28-f91c-313f-a953-32eba3db159x",
  "refresh_token": "730785c3-447b-31c2-9280-129dee19f789",
  "scope": "openid",
  "id_token": "very-long-id-token-here",
  "token_type": "Bearer",
  "expires_in": 35159
}</pre><p>You can now use this token to make API calls and retrieve the info you&#8217;re after. You normally do a request to your endpoint using the token like this:</p><pre class="urvanov-syntax-highlighter-plain-tag">CURLOPT_HTTPHEADER =&gt; array(
   "authorization: Bearer ".TOKEN,
   "cache-control: no-cache",
)</pre><p>Just a small note here: token is normally valid for a short-to-medium period of time. When the token expires you can use the refresh token to request a new token. The refresh token should always be kept a secret on the server side, but this is subject of a new tutorial.</p>
<p>Still here?? Let me know your experience and maybe I can help you out there.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/simple-oauth2-example-using-php-curl/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1516</post-id>	</item>
		<item>
		<title>PHP logical operators true false</title>
		<link>https://tutorialspage.com/php-logical-operators-true-false/</link>
					<comments>https://tutorialspage.com/php-logical-operators-true-false/#respond</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Tue, 13 Dec 2016 10:33:25 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=940</guid>

					<description><![CDATA[The [crayon-69d4d10a5f99f344553581-i/]  and [crayon-69d4d10a5f9a5456717433-i/]  logical operators seem to have more general recognition and background usage, although the [crayon-69d4d10a5f9a9852100753-i/]  and [crayon-69d4d10a5f9ac615508718-i/]  form of the same logical operators is taking more and more acceptation for the sake of readability. So here is a list for the results when evaluating the expressions in PHP. I am sure that this &#8230; <a href="https://tutorialspage.com/php-logical-operators-true-false/" class="more-link">Continue reading<span class="screen-reader-text"> "PHP logical operators true false"</span></a>]]></description>
										<content:encoded><![CDATA[<p>The 
			<span id="urvanov-syntax-highlighter-69d4d10a5f99f344553581" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">&amp;&amp;</span></span></span>  and 
			<span id="urvanov-syntax-highlighter-69d4d10a5f9a5456717433" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">||</span></span></span>  <strong>logical operators</strong> seem to have more general recognition and background usage, although the 
			<span id="urvanov-syntax-highlighter-69d4d10a5f9a9852100753" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-st">and</span></span></span>  and 
			<span id="urvanov-syntax-highlighter-69d4d10a5f9ac615508718" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-st">or</span></span></span>  form of the same logical operators is taking more and <strong>more acceptation for the sake of readability</strong>.</p>
<p>So here is a list for the results when evaluating the expressions in <strong>PHP</strong>. I am sure that this can be applied to a bunch of other languages. (I think it is a little bit superficial to say that it apply to all programming language since I only know some of them).<span id="more-940"></span></p>
<p><strong>Logical operator 
			<span id="urvanov-syntax-highlighter-69d4d10a5f9b0104124122" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-st">AND</span></span></span>  (
			<span id="urvanov-syntax-highlighter-69d4d10a5f9b3703524721" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">&amp;&amp;</span></span></span> )</strong></p><pre class="urvanov-syntax-highlighter-plain-tag">false and false =&gt; false

false and true =&gt; false

true and false =&gt; false

true and true =&gt; true</pre><p>&nbsp;</p>
<p><strong>Logical operator 
			<span id="urvanov-syntax-highlighter-69d4d10a5f9ba878334972" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-st">OR</span></span></span>  (
			<span id="urvanov-syntax-highlighter-69d4d10a5f9bd202510386" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">||</span></span></span> )</strong></p><pre class="urvanov-syntax-highlighter-plain-tag">false or false =&gt; false

false or true =&gt; true

true or false =&gt; true

true or true =&gt; true</pre><p>&nbsp;</p>
<p><strong>Logical operator 
			<span id="urvanov-syntax-highlighter-69d4d10a5f9c3902331339" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-st">NOT</span></span></span>  (
			<span id="urvanov-syntax-highlighter-69d4d10a5f9c7872700767" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">!</span></span></span> )</strong></p><pre class="urvanov-syntax-highlighter-plain-tag">!false =&gt; true

!true =&gt; false</pre><p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/php-logical-operators-true-false/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">940</post-id>	</item>
		<item>
		<title>WordPress is asking for FTP credentials to update plugins, FINALLY RESOLVED</title>
		<link>https://tutorialspage.com/how-to/</link>
					<comments>https://tutorialspage.com/how-to/#respond</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Sun, 13 Nov 2016 11:11:30 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=1050</guid>

					<description><![CDATA[I was dealing with this problem for a while and did not know how to give WordPress permission to write to his folders when updating plugins or themes. On the other hand this was forcing me to chmod[crayon-69d4d10a5fb15532398469-i/]  folder with 777 permissions. I am not a heavy image uploader, so when I was in need &#8230; <a href="https://tutorialspage.com/how-to/" class="more-link">Continue reading<span class="screen-reader-text"> "WordPress is asking for FTP credentials to update plugins, FINALLY RESOLVED"</span></a>]]></description>
										<content:encoded><![CDATA[<p>I was dealing with this problem for a while and did not know how to give WordPress permission to write to his folders when updating plugins or themes.</p>
<p>On the other hand this was forcing me to chmod
			<span id="urvanov-syntax-highlighter-69d4d10a5fb15532398469" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">wp</span><span class="crayon-o">-</span><span class="crayon-v">content</span><span class="crayon-o">/</span><span class="crayon-v">uploads</span></span></span>  folder with 777 permissions. I am not a heavy image uploader, so when I was in need of updating I was just chmod to 777 and then, after uploading my stuff, get back to 755.</p>
<p>I am not a Linux lover, geek, guru (or how they call it) so I had no clue about why this happened on my VPS or how to fix that. But as a respectable &#8220;self-directed learner&#8221; as I am, I had to dig into the situation.<span id="more-1050"></span></p>
<p>So this was my task list:</p>
<ol>
<li>Make a list of errors and possible relationship between them &#8211; others then the obvious permissions issue</li>
<li>Gather some documentation on this and understand why it is happening in the first place</li>
<li>See how other solved the errors and how can I apply it to my case.</li>
<li> Actually solve it on my Linux installation with Parallels Plesk  &#8211; and not die trying.</li>
</ol>
<h3>This are my issues</h3>
<ol>
<li>WordPress asks for FTP username and password while it&#8217;s uploading/upgrading a plugin, a theme or WP himself.</li>
<li>The 777 permissions are needed when I upload images (or any other media  files) to my uploads directory.</li>
<li>My mortgage is really expensive. (Later I have found out that this has nothing to do with the Apache permissions problem).</li>
</ol>
<p>I was unable to upload images nor any other files using the WordPress Media Uploader. Could not upload plugins or themes via the WordPress automatic updater.</p>
<p>You don&#8217;t have to be a genius to know that images are uploaded via PHP and PHP is running on apache, so apache does not have permission to upload files. I seems that apache is a service and also a user in Linux. So after all, this it is a simple user permission.</p>
<blockquote><p>Bottom line: Apache can not write to my WordPress folders</p></blockquote>
<h3>This is why all this happened in the first place</h3>
<p>When mod_php is used to execute PHP scripts on a web server it does not have permission to write to the file system if it does not own it. I uploaded files via FTP. So the files were assigned to the ftp user and the Apache user does not have permission to edit them. This is really a common problem when working with a CMS like WordPress.</p>
<p>On the other hand, this also happen if a php process creates a file. This can not be later edited by the FTP user.</p>
<p>&nbsp;</p>
<p>I had to<strong> set up PHP to run as FastCGI</strong> as it allows FTP and PHP scripts equal access, meaning file upload and edit functionality within the CMS works like a charm without any other special file permission configuration.</p>
<blockquote><p><strong>set up PHP to run as FastCGI</strong></p></blockquote>
<p>To do this in <strong>Plesk</strong> you will have to go to your domain <em>Hosting Settings</em> under the <em>Websites &amp; Domains</em> tab.</p>
<p>Under the Web scripting and statistics secction you will se the PHP support option and you will have to change the Run PHP as part to the FastCGI Option.</p>
<p><img data-recalc-dims="1" decoding="async" class="alignnone size-full wp-image-1303" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/12/Image-11.png?resize=708%2C208&#038;ssl=1" alt="" width="708" height="208" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/12/Image-11.png?w=708&amp;ssl=1 708w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/12/Image-11.png?resize=300%2C88&amp;ssl=1 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></p>
<p>Here is a complete explanation of all options available here with the pros and con that each one of them has a cording to the official Plesk Documentation and Help Portal. (<a href="https://docs.plesk.com/en-US/12.5/administrator-guide/web-hosting/php-management/php-handler-types.75145/">https://docs.plesk.com/en-US/12.5/administrator-guide/web-hosting/php-management/php-handler-types.75145/</a>)</p>
<div class="table-responsive">
<table class="table table-bordered table-hover table-striped">
<tbody>
<tr>
<th>Handler type</th>
<th>Performance</th>
<th>Memory usage</th>
<th>Handler details</th>
</tr>
<tr>
<td>
<p class="tablebodytext"><strong>Apache module</strong></p>
</td>
<td>
<p class="tablebodytext">High</p>
</td>
<td>
<p class="tablebodytext">Low</p>
</td>
<td>This handler is only available in Plesk for Linux. It is the least secure option as all PHP scripts are executed on behalf of the apache user. This means that all files created by PHP scripts of any plan subscriber have the same owner (apache) and the same permission set. Thus, it is possible for a user to affect the files of another user or some important system files. You can avoid some security issues by turning the PHP safe_mode option on. This disables a number of PHP functions that have potential security risks. This may lead to inoperability of some web apps. The safe_mode option is considered to be obsolete and was removed in PHP 5.4.</td>
</tr>
<tr>
<td>
<p class="tablebodytext"><strong>ISAPI extension</strong></p>
</td>
<td>
<p class="tablebodytext">High</p>
</td>
<td>
<p class="tablebodytext">Low</p>
</td>
<td>
<p class="tablebodytext">This handler is only available in Plesk for Windows. The ISAPI extension can provide site isolation if a dedicated IIS application pool is switched on for subscriptions. Site isolation means that the sites of different customers run their scripts independently. Thus, an error in one PHP script does not affect the work of other scripts. In addition, PHP scripts run on behalf of a system user associated with a hosting account. The ISAPI extension handler is not supported starting from PHP 5.3</p>
</td>
</tr>
<tr>
<td>
<p class="tablebodytext"><strong>CGI application</strong></p>
</td>
<td>
<p class="tablebodytext">Low</p>
</td>
<td>
<p class="tablebodytext">Low</p>
</td>
<td>
<p class="tablebodytext">The CGI handler provides PHP script execution on behalf of a system user associated with a hosting account. On Linux, this behavior is possible only when the suEXEC module of the Apache web server is on (default option). In other cases, all PHP scripts are executed on behalf of the apache user. By default, the CGI handler is unavailable to Plesk customers.</p>
</td>
</tr>
<tr>
<td>
<p class="tablebodytext"><strong>FastCGI application</strong></p>
</td>
<td>
<p class="tablebodytext">High</p>
</td>
<td>
<p class="tablebodytext">High</p>
</td>
<td>
<p class="tablebodytext">The FastCGI handler runs PHP scripts on behalf of a system user associated with a hosting account.</p>
</td>
</tr>
<tr>
<td>
<p class="tablebodytext"><strong>PHP-FPM application</strong></p>
</td>
<td>
<p class="tablebodytext">High</p>
</td>
<td>
<p class="tablebodytext">Low</p>
</td>
<td>
<p class="tablebodytext">This handler is only available in Plesk for Linux. The PHP-FPM is an advanced version of FastCGI which offers significant benefits for highly loaded web applications.</p>
</td>
</tr>
</tbody>
</table>
</div>
<p>&nbsp;</p>
<p>This may also help. There is a special directive for wordpress for not to ask to FTP credentials. For this you can add the following line to your 
			<span id="urvanov-syntax-highlighter-69d4d10a5fb1c096644806" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">wp</span><span class="crayon-o">-</span><span class="crayon-v">config</span><span class="crayon-sy">.</span><span class="crayon-v">php</span></span></span> after 
			<span id="urvanov-syntax-highlighter-69d4d10a5fb24276502704" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">define</span><span class="crayon-sy">(</span><span class="crayon-s">'WP_DEBUG'</span><span class="crayon-sy">,</span><span class="crayon-h"> </span><span class="crayon-t">false</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></span></span> line.</p><pre class="urvanov-syntax-highlighter-plain-tag">define('FS_METHOD', direct);</pre><p>Hope this helped you trow all the process. Please let me know if you think I have omitted something, or how it worked on your specific server configuration.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/how-to/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1050</post-id>	</item>
		<item>
		<title>Benchmarking on the glob() &#8220;dinosaur&#8221; and readdir() PHP functions</title>
		<link>https://tutorialspage.com/benchmarking-on-the-glob-and-readdir-php-functions/</link>
					<comments>https://tutorialspage.com/benchmarking-on-the-glob-and-readdir-php-functions/#comments</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Thu, 18 Feb 2016 17:14:22 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=1094</guid>

					<description><![CDATA[Well..it&#8217;s true. The PHP [crayon-69d4d10a5fc69935861427-i/]  function is a big dinosaur, memory eater and speed blow upper (if that can be said). So let&#8217;s test the [crayon-69d4d10a5fc70565091589-i/]  function against some other alternatives. I have recently had to detect duplicated/missing files comparing two directories in a set of 80.000 files and I used the PHP [crayon-69d4d10a5fc74969904682-i/] function. 80k is &#8230; <a href="https://tutorialspage.com/benchmarking-on-the-glob-and-readdir-php-functions/" class="more-link">Continue reading<span class="screen-reader-text"> "Benchmarking on the glob() &#8220;dinosaur&#8221; and readdir() PHP functions"</span></a>]]></description>
										<content:encoded><![CDATA[<p>Well..it&#8217;s true. The PHP 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc69935861427" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">glob</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span>  function is a big dinosaur, memory eater and speed blow upper (if that can be said). So let&#8217;s test the 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc70565091589" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">glob</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span>  function against some other alternatives.</p>
<p>I have recently had to <a title="How to compare two directories for missing files using PHP" href="https://tutorialspage.com/how-to-compare-two-directories-for-missing-files/">detect duplicated/missing files comparing two directories</a> in a set of 80.000 files and I used the PHP 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc74969904682" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">glob</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> function. 80k is a reasonable number for a local machine. But later I had to deal with 800k and that is really a lot if you don&#8217;t do things the right way. So I had to try some other alternatives.</p>
<p>I had to do some benchmarking in the area and I used a set of 25.000 images and another folder with 10.000 random duplicates. I think this is a reasonable number to see any differences in the final result.</p>
<p><span id="more-1094"></span></p>
<h2>Here are my approaches in my intent of testing the duplicate file detection</h2>
<p>So for first test I runned the function as it is. You can see the entire function n my previous article:</p><pre class="urvanov-syntax-highlighter-plain-tag">$files = glob( $dir[1]."/*".$ext);</pre><p><img data-recalc-dims="1" decoding="async" class="alignnone size-full wp-image-1108" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/php-glob-function-testing.png?resize=600%2C402&#038;ssl=1" alt="php glob function testing" width="600" height="402" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/php-glob-function-testing.png?w=600&amp;ssl=1 600w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/php-glob-function-testing.png?resize=300%2C201&amp;ssl=1 300w" sizes="(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></p>
<p><em>Processing took 6.76 seconds and 5 mb of memory.</em></p>
<p>Second I used the 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc7a617762641" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">GLOB_NOSORT</span></span></span> . This can make a significant improvement if you do not care about the file order. 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc7e998423157" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">GLOB_NOSORT</span></span></span> param returns files as they appear in the directory which means no sorting. When this flag is not used, the path names are sorted alphabetically</p><pre class="urvanov-syntax-highlighter-plain-tag">$files = glob( $dir[1]."/*".$ext, GLOB_NOSORT );</pre><p><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-full wp-image-1110" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/php-glob-function-testing-globnosort.png?resize=600%2C407&#038;ssl=1" alt="php glob function testing no sort" width="600" height="407" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/php-glob-function-testing-globnosort.png?w=600&amp;ssl=1 600w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/php-glob-function-testing-globnosort.png?resize=300%2C204&amp;ssl=1 300w" sizes="auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></p>
<p><em>Processing took 6.37 seconds 5 mb of memory.</em></p>
<p>I have done this tests using the function I described in the previous article and the concept is quite simple. Buid an array of all files in folder <em>dir1</em> and see if the files exists in <em>dir2.</em> Note that time may vary depending of the CPU usage at the moment of the test an that it is exponential depending of the files quantity. You can take a look at the function <a href="https://tutorialspage.com/how-to-compare-two-directories-for-missing-files/">here</a>. I had commented out tbe part that renames or delete the files and felt just the on-screen messaging.</p>
<p>Of course I can always search 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc84358651341" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">dir2</span></span></span> instead of 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc88950912062" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">dir1</span></span></span> . It is a good point if you know what folder will have less files, you can choose to start from there. It is very logical that if the folder will have less file, the function will spend less time to build the array in memory. But this is not of my interest right now in the process that I am doing, since 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc8b821612991" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">dir2</span></span></span> will have more and more images as opposite as 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc8e510453285" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">dir1</span></span></span> .</p>
<p>So for now I don&#8217;t think there is much I can do. 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc91585695452" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">glob</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span>  function builds an array with all files that are in the specified folder and it has to do it no matter what.</p>
<p>It could be a lot faster if it didn&#8217;t try to build the array and just check for the duplicated files on the fly. This could be a good approach, so let&#8217;s look in the PHP arsenal and see what else we can find.</p>
<h2>How to use readdir() to search for duplicate files in distinct folders</h2>
<p>The PHP 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc95054471370" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">readdir</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span> </span></span> function returns the name of the next entry in a given directory. The entries are returned in the order in which they are stored by the filesystem. This seems to be very similar to 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc98701523455" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">glob</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> , but it doesn&#8217;t store the filenames in memory as an array.</p>
<p>So let&#8217;s build a little function using 
			<span id="urvanov-syntax-highlighter-69d4d10a5fc9b507156469" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">readdir</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> <strong> </strong>to search through the folders for duplicated files.</p><pre class="urvanov-syntax-highlighter-plain-tag">function simple_compare_folders($dir)
{

    echo "&lt;pre&gt;";

    if ($handle = opendir($dir))
    {
        while (false !== ($file_name = readdir($handle)))
        {
                        
            echo "$file_name&lt;br /&gt;";

        }

        closedir($handle);
    }

    echo "&lt;/pre&gt;";

}</pre><p>And let&#8217;s do some benchmark on it <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f600.png" alt="😀" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-full wp-image-1113" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/readdir-benchmark-test.png?resize=596%2C408&#038;ssl=1" alt="readdir-benchmark-test" width="596" height="408" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/readdir-benchmark-test.png?w=596&amp;ssl=1 596w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/readdir-benchmark-test.png?resize=300%2C205&amp;ssl=1 300w" sizes="auto, (max-width: 596px) 85vw, 596px" /></p>
<p>Waw&#8230;god damned&#8230;this is awesome. If you blink you won&#8217;t see it. This is f*** real speed. But hey, this only prints the files, and not comparing them to come up with the duplicated ones. Well, let&#8217;s add the fancy stuff from our first function and see how it does. Hands on work:</p><pre class="urvanov-syntax-highlighter-plain-tag">/**
 * @param array $dir: this is an array with your custom paths
 * @param string $ext: File extension
 * @param boleean $action: flase, move of delete. If false no action will be taken and files will be printed on screen.
 * @param boleean $output: If false, no message will be output to screen.
 * @return string
 */
function compare_two_directories_readdir($dir, $ext="jpg", $action = false, $output=true)
{

if($output) echo "&lt;pre&gt;I found this duplicate files:&lt;br /&gt;";

    if ($handle = opendir($dir[1]))
    {
        while (false !== ($file_name = readdir($handle)))
        {
            $ext = strtolower(end(explode('.', $file_name)));

            // Check for extension
            if ($ext == $ext_ck) 
            {

                // check if file exists in the second directory
                if(!file_exists($dir[2]."/".$file_name))
                {
                      
                            
                    if($output) echo "$file_name";

                    if($action == 'move') {
                        rename($file, $dir[3]."/".$file_name); // move the image to folder 3. 
                        if($output) echo " &lt;span style='color:green'&gt;moved&lt;/span&gt; to ".basename($dir[3])."&lt;br /&gt;";
                    }
                    elseif ($action == 'delete')  {
                        unlink($file); // just delete the image
                        if($output) echo " &lt;span style='color:red'&gt;deleted&lt;/span&gt;&lt;br /&gt;";
                    }
                    else {
                        if($output)  echo  '&lt;br /&gt;'; 
                    }
                    
                }
                
            }
        }

        closedir($handle);
    }

    if($output) echo "&lt;/pre&gt;";

}</pre><p>And here it is. Nice and clean. Let&#8217;s do some benchmark on it and hope for the best.</p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-full wp-image-1114" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/readdir-benchmark-testing2.png?resize=588%2C403&#038;ssl=1" alt="readdir-benchmark-testing" width="588" height="403" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/readdir-benchmark-testing2.png?w=588&amp;ssl=1 588w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/readdir-benchmark-testing2.png?resize=300%2C206&amp;ssl=1 300w" sizes="auto, (max-width: 588px) 85vw, 588px" /></p>
<p>2.72 seconds &#8230;well how about that? I saved more than half of the time just using 
			<span id="urvanov-syntax-highlighter-69d4d10a5fca5044675308" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">readdir</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> function. That&#8217;s more like it. And take a look at the <strong>memory usage! It dropped down from 5MB to 256Kb</strong>, isn&#8217;t that great or what? Try to apply this algorithm to 800k instead ok 25k of files&#8230;This is really a significant improvement.</p>
<p>At this point I think there is no need to do further testing on this 2 functions although I agree that depending on the case, there is not possible to user readdir() and that glob() could be your only approach.</p>
<blockquote><p>Conclusion: <em>readdir() </em>&#8230; you rock!!</p></blockquote>
<p>We have speed up the process a lot but I would like to try some other things and see if there are ways to make the code a little bit faster. After a little more testing and after introducing some break points I have noticed that the slower part, is the one that checking  if  the 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcac214605449" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">file_exists</span></span></span> in the second folder and it eats up more than 2 seconds. This is like 90% of time is used here. How can we speed this up?</p>
<h2>How to speed up the PHP <em>file_exists()</em> function &#8230;dead-end?</h2>
<p>This is quite a rough road. There are lots of people saying that there is not much you can do about it. But some say that thinks speed up if you use 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcaf249985032" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">is_file</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span>  instead of
			<span id="urvanov-syntax-highlighter-69d4d10a5fcb3975091890" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">file_exists</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> . Well maybe on millions of files or on different folder structure. I&#8217;ve seen no change on my 25k set of JPEG images.</p>
<p>I have also tried using 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcb6020994738" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">clearstatcache</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span>  with no better result in my case.</p>
<p>But then I found this so called 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcb9715027975" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">stream_resolve_include_path</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> . In the <a href="http://php.net/manual/en/function.stream-resolve-include-path.php" target="_blank">PHP manual</a> they say it <em>resolve the filename against the include path according to the same rules as 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcbc266060464" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">fopen</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span><span class="crayon-o">/</span><span class="crayon-v">include</span></span></span> </em>. The value returned by 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcc0044383066" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">stream_resolve_include_path</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> is a <em><span class="type">string</span> containing the resolved absolute filename, or </em>
			<span id="urvanov-syntax-highlighter-69d4d10a5fcc3918007923" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-t">FALSE</span></span></span> <em>on failure</em>.</p>
<blockquote><p>Could  <strong>stream_resolve_include_path() function </strong>, be the holy grail?</p></blockquote>
<p>So everything should work just fine. In case it finds the file, will return a string. A string is always evaluated to 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcc6855832310" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-t">TRUE</span></span></span>  by the if statement, so we&#8217;re good on this. An in case the file is not found will return 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcc9378135637" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-t">FALSE</span></span></span>  witch is OK.</p>
<p>Let&#8217;s do some benchmark testing with 
			<span id="urvanov-syntax-highlighter-69d4d10a5fccd800311385" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">stream_resolve_include_path</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span> and see if we can replace 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcd0455786077" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">file_exists</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span>  with it.</p><pre class="urvanov-syntax-highlighter-plain-tag">// just replace this next line
if(file_exists(($dir[2]."/".$file_name))


// with this next line. Simple as that
if(stream_resolve_include_path ($dir[2]."/".$file_name))</pre><p>And here is the result of testing.</p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-full wp-image-1118" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/stream_resolve_include_path_benchmark.png?resize=590%2C403&#038;ssl=1" alt="stream_resolve_include_path_benchmark" width="590" height="403" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/stream_resolve_include_path_benchmark.png?w=590&amp;ssl=1 590w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/stream_resolve_include_path_benchmark.png?resize=300%2C205&amp;ssl=1 300w" sizes="auto, (max-width: 590px) 85vw, 590px" /></p>
<p>I think there is really no need to tell you that this is quite it for now. We have compared 25k of files against other 10k and all this in <strong>1.35 seconds</strong>. This really is speed. Did I told you that images are not quite small? There are <strong>2.61GB of images to compare against other 1.08GB.</strong></p>
<blockquote><p><strong>1.35 seconds</strong> to compare more than 3GB of files is really a good time. It is 6 time faster than the gob() and file_exists() functions.</p></blockquote>
<p>I have to say that I run this on a local machine, and it is more important to me to get the job done then get a lower loading time. So seconds are ok for me and I am not really interested in microseconds.</p>
<p>File check comparison benchmark testing &#8211; the final loop</p>
<p>OK, just for the record and to see some big data processed, I will do some benchmark only on this 3 PHP functions ( <em>is_file()</em>, <em>file_exists()</em> and <em>stream_resolve_include_path()</em> ) and test them on a <strong>1 million loop</strong>. I will first test with an <strong>existent file</strong> and then with a <strong>file that doesn&#8217;t exists</strong>.</p>
<p>Here is my little function that does the testing on this:</p><pre class="urvanov-syntax-highlighter-plain-tag">function do_benchmark( $file ) {

    $funcs = array('is_file','file_exists', 'stream_resolve_include_path');

    $run = 1000000;

    $stats = '| ';

    foreach ( $funcs as $func ) {

        $go = microtime(true);

        for ( $i = 0; $i &lt; $run; $i++ ) $func($file);            
    
        $stats .= "$func = " . number_format( (microtime(true) - $go), 5) . " | ";

        clearstatcache();
    }

    return $stats;
}

$ok_file = dirname(__FILE__)."/icon.gif";
$no_file = dirname(__FILE__)."/none.png";

echo "&lt;pre&gt;" . do_benchmark($ok_file) . "\n" . do_benchmark($no_file) . "&lt;/pre&gt;";</pre><p>And here are the results on this: It is really surprising. I think this speaks for itself</p><pre class="urvanov-syntax-highlighter-plain-tag">// this is when file is in place
| is_file =  0.39600 | file_exists = 28.20900 | stream_resolve_include_path =  1.46400 | 

// this is when file is not found
| is_file = 32.74600 | file_exists = 31.56300 | stream_resolve_include_path = 31.15000 |</pre><p>This explain why my previous test returned better results using 
			<span id="urvanov-syntax-highlighter-69d4d10a5fcde028570393" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 11px !important; line-height: 25px !important;font-size: 11px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 11px !important; line-height: 25px !important;font-size: 11px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">stream_resolve_include_path</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span>. The part that I do not understand is why 
			<span id="urvanov-syntax-highlighter-69d4d10a5fce1748107197" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-e">is_file</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span></span></span>  did not make the code faster in the first place. For this test a used a really small image and then I repeat the test using a large image, but it seems that the file size does not influence the final result.</p>
<p>So what do you think? If you know about<span style="line-height: 1.75;"> some other alternatives on this just let me know. I would really die to test them out.</span></p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/benchmarking-on-the-glob-and-readdir-php-functions/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1094</post-id>	</item>
		<item>
		<title>How to compare two directories for missing files using PHP</title>
		<link>https://tutorialspage.com/how-to-compare-two-directories-for-missing-files/</link>
					<comments>https://tutorialspage.com/how-to-compare-two-directories-for-missing-files/#comments</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Tue, 16 Feb 2016 14:21:15 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=1072</guid>

					<description><![CDATA[Detecting the missing files when comparing two directories can be a tricky job to do. So this is my scenario: I am trying to pass some image throw a small piece of software for batch processing and I always get timeout due to the big quantity of images. There are around 80.000 images that I &#8230; <a href="https://tutorialspage.com/how-to-compare-two-directories-for-missing-files/" class="more-link">Continue reading<span class="screen-reader-text"> "How to compare two directories for missing files using PHP"</span></a>]]></description>
										<content:encoded><![CDATA[<p><strong>Detecting the missing files when comparing two directories</strong> can be a tricky job to do. So this is my scenario:</p>
<p>I am trying to pass some image throw a small piece of software for <strong>batch processing</strong> and I always get timeout due to the big quantity of images. There are around 80.000 images that I am trying to process and the software get stuck to (let say) 10.000 images so I have to start all over again and I have no idea which are the missing files.</p>
<p>So my solution is to take out the images that have been processed from the folder and feed the program with the images that have not been processed.</p>
<blockquote><p>So to find out this I have to <strong>compare the 2 directories for duplicated files. </strong>In other words<strong> &#8220;detect the missing files&#8221;</strong></p></blockquote>
<p><span id="more-1072"></span><br />
<strong>Comparing two directories for missing file</strong> is really an easy task. You just have to iterate through the first directory and see if the same file exists in the second. If the file exists, it means it has been processed, so I will move it to a third folder (I prefer this, just in case) or I can just delete it. This way I can detect the files that have been processed and leave untouched the files that have not yet processed.</p>
<h2>Detect missing files while compare 2 folders &#8211; the php function</h2>
<p>So to get the job done, I came up with this function:</p><pre class="urvanov-syntax-highlighter-plain-tag">/**
 * @param array $dir: this is an array with your custom paths
 * @param string $ext: File extension
 * @param boolean $rename: If false, it will delete the file.
 * @param boolean $output: If false, no message will be output to screen.
 * @return string
 */
function compare_two_directories($dir, $ext=".jpg", $move=true, $output=true){

    $files = glob( $dir[1]."/*".$ext );

    $count = 0;

    if($output) echo "&lt;pre&gt;I found this duplicate files:&lt;br /&gt;";

    foreach ($files as $file) {
        
        $file_name = basename($file);

        // check if file exists in the second directory
        if(file_exists($dir[2]."/".$file_name)){
                      
            if($output) echo "$file_name";

            if($move) {
                rename($file, $dir[3]."/".$file_name); // move the image to folder 3. 
                if($output) echo " &lt;span style='color:green'&gt;moved&lt;/span&gt; to ".basename($dir[3])."&lt;br /&gt;";
            } else {
                unlink($file); // just delete the image
                if($output) echo " &lt;span style='color:red'&gt;deleted&lt;/span&gt;&lt;br /&gt;";
            }

            $count++;
        }
        
    }

    if($output) echo "&lt;/pre&gt;";

    return "Done processing and found &lt;span style='color:green'&gt;$count&lt;/span&gt; duplicated &lt;span style='color:red; font-weight:bold;'&gt;$ext&lt;/span&gt; files ";
}</pre><p></p>
<h3>Use function to compare files inside directories like this</h3>
<p>You can call it like this. I like to include also the time that was used, just for statistics purpose, but you can omit it.</p><pre class="urvanov-syntax-highlighter-plain-tag">// this is the path to your script file and your directories are relative to it.
define("MY_PATH", dirname(__FILE__)); 
// define("MY_PATH", "/var/www/my/custom/path/to/my/directories");

// set your custom paths
$dir[1] = MY_PATH."/dir1";
$dir[2] = MY_PATH."/dir2";
$dir[3] = $dir[1]."_processed"; // please note the folder "dir1_processed" must exist if you want to move files to it

// call the function and get the job done
echo compare_two_directories($dir);

// this next line is an example for .png images, DELETE files and output messages will be sent to screen
// echo compare_two_directories($dir, ".png", true, false);</pre><p>But if you only need to list the different files, just comment the 
			<span id="urvanov-syntax-highlighter-69d4d10a60021323998855" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">rename</span></span></span> function like this and file that are missing will only by displayed on screen.</p><pre class="urvanov-syntax-highlighter-plain-tag">// rename($file, $dir[3]."/".$file_name);</pre><p></p>
<h3>Get more stats when comparing directories for duplicated files</h3>
<p>I like to also know the processing time just for statistics purpose. So to do that you can just wrap the upper code like this:</p><pre class="urvanov-syntax-highlighter-plain-tag">$time = microtime(true); // Gets microseconds

// the code here

echo "&lt;br /&gt;Processing took &lt;span style='color:blue'&gt;".round( (microtime(true) - $time), 2).'&lt;/span&gt; seconds';</pre><p>Note that all directories must be on the same level as the script file if you want your script to work out of the box. But if this is not your case, feel free to edit it so it adapts to your specific file structure or your server configuration.</p>
<h3>See memory usage</h3>
<p>Speaking about server configuration, you will normally need a lot of memory if you have to compare lots of files. I normally do this kind of jobs on a local machine using XAMPP, but you can also do it on your normal server. You can take a peek at your memory usage by using this little function:</p><pre class="urvanov-syntax-highlighter-plain-tag">function get_memory()
{
    $size = memory_get_peak_usage (true);
    $unit = array('b','kb','mb','gb','tb','pb');
    return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}

echo "&lt;br /&gt;".get_memory()." of memory were used wile processing" ;</pre><p>Just place this at the end of your file to see your memory usage when comparing the 2 folders</p>
<p>Get &#8220;compare directories for missing files&#8221; script</p>
<p>You can download a full working copy of this script from <a href="https://gist.github.com/catalin586/427a6850f7f934d62586" target="_blank">the Github repository</a> and compare your directories for missing files. Here is a screenshot of it working. I agree with you that it needs some more style <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="aligncenter wp-image-1090 size-full" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/compare-directories-for-missing-files-in-php.png?resize=500%2C307&#038;ssl=1" alt="compare directories for missing files in php" width="500" height="307" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/compare-directories-for-missing-files-in-php.png?w=500&amp;ssl=1 500w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/compare-directories-for-missing-files-in-php.png?resize=300%2C184&amp;ssl=1 300w" sizes="auto, (max-width: 500px) 85vw, 500px" /></p>
<p>There is also a second choice in which you can store both directories in 2 distinct arrays and then just compare the two arrays. If there is a match, then move the files to a third folder or delete them. I really did not test the two alternatives, but I think the first one is faster than the second because it doesn&#8217;t need to iterate the second directory. But I could be wrong since it has to do lots of single-file checks.</p>
<p>Please let me know if you try this second approach and with one worked best for you.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/how-to-compare-two-directories-for-missing-files/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1072</post-id>	</item>
		<item>
		<title>Change default 1M max upload filesize limit for WordPress Mutisite</title>
		<link>https://tutorialspage.com/change-default-1m-max-upload-filesize-limit-for-wordpress-mutisite/</link>
					<comments>https://tutorialspage.com/change-default-1m-max-upload-filesize-limit-for-wordpress-mutisite/#respond</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Thu, 11 Feb 2016 09:33:45 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=1055</guid>

					<description><![CDATA[If you are like me playing around with the WordPress Multisite feature and you&#8217;ve run into the max allowed upload filesize limit of 1M, you are in the right place to fix it. There is a not quite intuitive (for me) option in the Super admin dashboard that allows you to change this. Please note &#8230; <a href="https://tutorialspage.com/change-default-1m-max-upload-filesize-limit-for-wordpress-mutisite/" class="more-link">Continue reading<span class="screen-reader-text"> "Change default 1M max upload filesize limit for WordPress Mutisite"</span></a>]]></description>
										<content:encoded><![CDATA[<p>If you are like me playing around with the WordPress Multisite feature and you&#8217;ve run into the <strong>max allowed upload filesize limit of 1M</strong>, you are in the right place to fix it.</p>
<p>There is a not quite intuitive (for me) option in the Super admin dashboard that allows you to change this. Please note that this does not overwrite your apache or php.ini settings if this last one is smaller. Here you can apply the rule of the most restrictive.</p>
<p>So here are the steps to achieve this:</p>
<p>Here are the steps to change the Maximum upload file size within WP admin:<span id="more-1055"></span></p>
<ul>
<li>Click Network Admin, at the top-left of your WordPress dashboard.</li>
<li>In the main menu on your left side, just hover over Settings and then click network Settings.</li>
<li>Scroll to the bottom and almost at the end of the list you will find the Max upload file size. You must specify a value in KB.</li>
<li>Apply Changes and you are good to go.</li>
</ul>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-full wp-image-1056" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/Wordpress-max-uploaded-file-size.png?resize=560%2C157&#038;ssl=1" alt="Wordpress max uploaded file size" width="560" height="157" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/Wordpress-max-uploaded-file-size.png?w=560&amp;ssl=1 560w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/Wordpress-max-uploaded-file-size.png?resize=300%2C84&amp;ssl=1 300w" sizes="auto, (max-width: 560px) 85vw, 560px" /></p>
<p>Like I said, if your value is bigger than the one you have specified in your 
			<span id="urvanov-syntax-highlighter-69d4d10a601b6279811869" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">/</span><span class="crayon-v">etc</span><span class="crayon-o">/</span><span class="crayon-v">php</span><span class="crayon-sy">.</span><span class="crayon-v">ini</span></span></span>  or in your 
			<span id="urvanov-syntax-highlighter-69d4d10a601bd510994262" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">htaccess</span></span></span> , it will not been overwritten. You will have to further tweak your server settings to achieve your desired maximum upload file value in your WordPress multisite installation.</p>
<p>This are some value that may help you to get the job done:</p><pre class="urvanov-syntax-highlighter-plain-tag">post_max_size = 10M

upload_max_filesize = 10M

memory_limit = 64M

max_execution_time = 250

max_input_time = 250</pre><p>On the other hand WordPress itself has another settings in the 
			<span id="urvanov-syntax-highlighter-69d4d10a601c4324811144" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">wp</span><span class="crayon-o">-</span><span class="crayon-v">config</span><span class="crayon-sy">.</span><span class="crayon-v">php</span></span></span>  file that may be tweaked.</p><pre class="urvanov-syntax-highlighter-plain-tag">define('WP_MEMORY_LIMIT', '10M');</pre><p>So hope this helps you. I will soon post a specific tutorial about how to change the settings via your ini file, htaccess, or even directly in your php scripts. You can subscribe to receive it in your mailbox.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/change-default-1m-max-upload-filesize-limit-for-wordpress-mutisite/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1055</post-id>	</item>
		<item>
		<title>The easy way to migrate WordPress using search and replace database</title>
		<link>https://tutorialspage.com/the-easy-way-to-migrate-wordpress-using-search-and-replace-serialized-database-entries/</link>
					<comments>https://tutorialspage.com/the-easy-way-to-migrate-wordpress-using-search-and-replace-serialized-database-entries/#comments</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Wed, 03 Feb 2016 15:36:02 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[WordPress]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=955</guid>

					<description><![CDATA[So you want to migrate a WordPress database and you came into the problem that when you load the new installation directory you are being redirected to the old domain. You probably tried to manually replace in your PhpMyAdmin in the [crayon-69d4d10a602c2552089977-i/]  table the two entries that normally exist, but still no success. Maybe your website &#8230; <a href="https://tutorialspage.com/the-easy-way-to-migrate-wordpress-using-search-and-replace-serialized-database-entries/" class="more-link">Continue reading<span class="screen-reader-text"> "The easy way to migrate WordPress using search and replace database"</span></a>]]></description>
										<content:encoded><![CDATA[<p>So you want to <strong>migrate a WordPress database</strong> and you came into the problem that when you load the new installation directory you are being redirected to the old domain. You probably tried to manually replace in your PhpMyAdmin in the 
			<span id="urvanov-syntax-highlighter-69d4d10a602c2552089977" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-i">wp_options</span></span></span>  table the two entries that normally exist, but still no success. Maybe your website is loading but there are lots of errors in there.</p>
<p>This is because WordPress and lots of plugins store data and full URI in the database using wp_serialize function. Here is a little example about what I am talking about:</p><pre class="urvanov-syntax-highlighter-plain-tag">a:6:s:4:"role";s:5:"admin";s:17:"includes_globally";s:2:"on";s:18:"pages_for_includes";s:0:"";s:12:"js_to_footer";s:2:"on";s:15:"show_dev_export";s:3:"off";s:11:"enable_logs";s:3:"off";}</pre><p>If you just do a search and replace you will break all this fields and data can not be recovered.</p>
<p><span id="more-955"></span></p>
<p>WordPress itself in his Codex Archive about <a href="https://codex.wordpress.org/Moving_WordPress" target="_blank">moving WordPress installation</a> files and database recommends that you manually change the URL in the WordPress Control backend.</p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-full wp-image-958" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/change-url-in-wordpress-backend.png?resize=665%2C322&#038;ssl=1" alt="change url in wordpress backend" width="665" height="322" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/change-url-in-wordpress-backend.png?w=665&amp;ssl=1 665w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/change-url-in-wordpress-backend.png?resize=300%2C145&amp;ssl=1 300w" sizes="auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></p>
<p>Doing this, will give you a 404 error page on the old domain, but after you move the files and database on the new server, your WordPress installation should work.</p>
<p>Please note that this process leaves you with lots of database entries containing the old domain URL. Entries like posts, data stored by themes and plugins will not be modified. On the other hand you can do a search and replace on your entire database to change the URLs, but this can cause major issues with serialized data because lots of themes plugins and widgets store values along with their length. This can be a big headache.</p>
<p>I am sure there are lots of tools out there, but today I will be talking about this <a href="https://interconnectit.com/products/search-and-replace-for-wordpress-databases/" target="_blank">PHP Search/Replace tool</a>. Note that this tool is not specially designed for WordPress migration, but for any database search and replace process. It works great with WordPress installation and like you can see it has a very friendly interface. After you finish searching and replacing the database you can use the build in cleanup process or delete the files manually.</p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-957 alignnone" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/wodpress-search-and-replace-database.png?resize=673%2C611&#038;ssl=1" alt="wodpress search and replace database" width="673" height="611" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/wodpress-search-and-replace-database.png?w=673&amp;ssl=1 673w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/wodpress-search-and-replace-database.png?resize=300%2C272&amp;ssl=1 300w" sizes="auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></p>
<p>Please note that I am not related in any way with this guys. I am just happy to use their tool and post about it.</p>
<p>To run the search/replace script you will have to download it and place a copy on your website root folder. So your files will be something like this:</p><pre class="urvanov-syntax-highlighter-plain-tag">/your-custom-folder-for-search-replace-script
/wp-admin
/wp-content
/wp-includes</pre><p>It is very important to place the script in his own folder because when it finishes the process it will attempt (give you the option of course) to delete himself. This will delete all files on the same level so if you place all the files directly on the root you will end up deleting all WP installation files. If you do not understand this just ask a geek for a little advice.</p>
<p>Remember that if you had any kind of permalinks rules it is very possible that they will not work properly after the WP migration. So you must disable 
			<span id="urvanov-syntax-highlighter-69d4d10a602cf332468678" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">htaccess</span></span></span>  and reconfigure permalinks.</p>
<p>Resetting the permalinks is quite easy and can be done in two ways:</p>
<ul>
<li>The first method is done in the WordPress dashboard. Just go to your WordPress dashboard, click on <em>Settings</em> and go to<em> Permalinks.</em></li>
</ul>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone size-full wp-image-959" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/reset-wordpress-permalinks.png?resize=628%2C507&#038;ssl=1" alt="reset wordpress permalinks" width="628" height="507" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/reset-wordpress-permalinks.png?w=628&amp;ssl=1 628w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/reset-wordpress-permalinks.png?resize=300%2C242&amp;ssl=1 300w" sizes="auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px" /></p>
<p>Just click <em>Save Settings </em>and you should be good to go. If this does not work try to change to a different option, save and then change back to your desired permalink structure. This will force WordPress to rewrite the 
			<span id="urvanov-syntax-highlighter-69d4d10a602d3731586261" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">htaccess</span></span></span>  file.</p>
<ul>
<li>The second option is to manually edit the database. You will have to search your database 
			<span id="urvanov-syntax-highlighter-69d4d10a602d6545026695" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">wp_options</span></span></span> table for a special entry called 
			<span id="urvanov-syntax-highlighter-69d4d10a602d9048087121" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-v">permalink_structure</span></span></span> . After you found it just edit it there.</li>
</ul>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="size-full wp-image-967 alignnone" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/03/manualy-edit-permalink-for-wordpress.png?resize=590%2C76&#038;ssl=1" alt="manualy edit permalink for wordpress" width="590" height="76" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/03/manualy-edit-permalink-for-wordpress.png?w=590&amp;ssl=1 590w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/03/manualy-edit-permalink-for-wordpress.png?resize=300%2C39&amp;ssl=1 300w" sizes="auto, (max-width: 590px) 85vw, 590px" /></p>
<p>Note that if you edit your settings via database or even if you are using the dashboard but WordPress does not have permission to write on file. You will have to manually edit your 
			<span id="urvanov-syntax-highlighter-69d4d10a602dd876128070" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-sy">.</span><span class="crayon-v">htaccess</span></span></span></p><pre class="urvanov-syntax-highlighter-plain-tag"># BEGIN WordPress
&lt;IfModule mod_rewrite.c&gt;
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
&lt;/IfModule&gt;

# END WordPress</pre><p>If all this did not make your site to work properly, try to do a <a href="https://www.google.es/webhp?sourceid=chrome-instant&amp;rlz=1C1CHMO_esES665ES665&amp;ion=1&amp;espv=2&amp;ie=UTF-8#q=force%20refresh" target="_blank">force refresh</a>, clear cache or just use a different browser <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f609.png" alt="😉" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/the-easy-way-to-migrate-wordpress-using-search-and-replace-serialized-database-entries/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">955</post-id>	</item>
		<item>
		<title>PHP check if mail() function is enabled on your server</title>
		<link>https://tutorialspage.com/php-check-if-mail-function-is-enabled-on-your-server/</link>
					<comments>https://tutorialspage.com/php-check-if-mail-function-is-enabled-on-your-server/#respond</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Mon, 01 Feb 2016 11:35:23 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=942</guid>

					<description><![CDATA[To check if mail function is enabled on your apache server you can try one of the following: Check your php.ini like this: [crayon-69d4d10a6046a367312250/] You should search for this in the list sendmail_path that has the default [crayon-69d4d10a60471169432081-i/] You can also try to manual set it to this value by changing the php.ini file. To do this go to [crayon-69d4d10a60474453889336-i/] and uncomment &#8230; <a href="https://tutorialspage.com/php-check-if-mail-function-is-enabled-on-your-server/" class="more-link">Continue reading<span class="screen-reader-text"> "PHP check if mail() function is enabled on your server"</span></a>]]></description>
										<content:encoded><![CDATA[<p>To <strong>check if mail function is enabled on your apache</strong> server you can try one of the following:</p>
<p>Check your php.ini like this:</p><pre class="urvanov-syntax-highlighter-plain-tag">&lt;?php
   phpinfo();
?&gt;</pre><p>You should search for this in the list <em><strong>sendmail_path</strong> </em>that has the default 
			<span id="urvanov-syntax-highlighter-69d4d10a60471169432081" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-i">value</span> <span class="crayon-o">/</span><span class="crayon-i">usr</span><span class="crayon-o">/</span><span class="crayon-i">sbin</span><span class="crayon-o">/</span><span class="crayon-i">sendmail</span><span class="crayon-h"> </span><span class="crayon-o">-</span><span class="crayon-i">t</span><span class="crayon-h"> </span><span class="crayon-o">-</span><span class="crayon-i">i</span></span></span></p>
<p>You can also try to manual set it to this value by changing the php.ini file. To do this go to 
			<span id="urvanov-syntax-highlighter-69d4d10a60474453889336" class="urvanov-syntax-highlighter-syntax urvanov-syntax-highlighter-syntax-inline  crayon-theme-monokai crayon-theme-monokai-inline urvanov-syntax-highlighter-font-courier-new" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important;"><span class="crayon-pre urvanov-syntax-highlighter-code" style="font-size: 12px !important; line-height: 25px !important;font-size: 12px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;"><span class="crayon-o">/</span><span class="crayon-v">etc</span><span class="crayon-o">/</span><span class="crayon-v">php5</span><span class="crayon-o">/</span><span class="crayon-v">apache2</span><span class="crayon-o">/</span><span class="crayon-v">php</span><span class="crayon-sy">.</span><span class="crayon-v">ini</span></span></span> and uncomment the <em><strong>sendmail_path </strong></em>line like this</p>
<p><span id="more-942"></span></p><pre class="urvanov-syntax-highlighter-plain-tag">; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
sendmail_path = "/usr/sbin/sendmail -t -i"</pre><p>If it did not work for you, try to find out if the function exists in the first place.</p><pre class="urvanov-syntax-highlighter-plain-tag">&lt;?php
if ( function_exists( 'mail' ) )
{
    echo 'mail() is available';
}
else
{
    echo 'mail() has been disabled';
}
?&gt;</pre><p>And finally you can test what the <strong>php mail()</strong> function returns.</p><pre class="urvanov-syntax-highlighter-plain-tag">&lt;?php
    /*
     * Enable error reporting
     */
    ini_set( 'display_errors', 1 );
    error_reporting( E_ALL );

    /*
     * Setup email addresses and change it to your own
     */
    $from = "your_email@yourdomain.com";
    $to = "send_to@testdomain.com";
    $subject = "Simple test for mail function";
    $message = "This is a test to check if php mail function sends out the email";
    $headers = "From:" . $from;

    /*
     * Test php mail function to see if it returns "true" or "false"
     * Remember that if mail returns true does not guarantee
     * that you will also receive the email
     */
    if(mail($to,$subject,$message, $headers))
    {
    	echo "Test email send.";
    } 
    else 
    {
    	echo "Failed to send.";
    }
?&gt;</pre><p>But more important than this is if the email arrives to your inbox (or Spam folder).</p>
<p>I have also find myself in need to check the mail function a <strong>WordPress</strong> platform. You can do this on WordPress by installing <a href="https://wordpress.org/plugins/check-email/" target="_blank">Check Email plugin</a>. Although it has not been updated lately it still works and it does the job that it was installed for.</p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone wp-image-945 size-full" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/check-mail.png?resize=572%2C401&#038;ssl=1" alt="check mail" width="572" height="401" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/check-mail.png?w=572&amp;ssl=1 572w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/02/check-mail.png?resize=300%2C210&amp;ssl=1 300w" sizes="auto, (max-width: 572px) 85vw, 572px" /></p>
<p>Like I said, <strong>a positive result does not mean that the email will be received</strong>. There are much more things to check here.</p>
<p>Hope this helped you. If not please let me know how you finally solve it.</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/php-check-if-mail-function-is-enabled-on-your-server/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">942</post-id>	</item>
		<item>
		<title>Complete case study: watermark detection, remove and place a new watermark- Part II</title>
		<link>https://tutorialspage.com/complete-case-study-watermark-detection-remove-and-place-a-new-watermark-part-ii/</link>
					<comments>https://tutorialspage.com/complete-case-study-watermark-detection-remove-and-place-a-new-watermark-part-ii/#comments</comments>
		
		<dc:creator><![CDATA[cata]]></dc:creator>
		<pubDate>Wed, 27 Jan 2016 13:26:33 +0000</pubDate>
				<category><![CDATA[Case study]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">http://tutorialspage.com/?p=851</guid>

					<description><![CDATA[Like I said in the previous article &#8220;Complete case study: watermark replacement &#8211; part I&#8221; about replacing an old or outdated  watermark, this task is not quite trivial but more likely boring and very time-consuming. Anyway&#8230;let&#8217;s continue. I have now separated all images by: logo in the center multiple logos all over. I will try to &#8230; <a href="https://tutorialspage.com/complete-case-study-watermark-detection-remove-and-place-a-new-watermark-part-ii/" class="more-link">Continue reading<span class="screen-reader-text"> "Complete case study: watermark detection, remove and place a new watermark- Part II"</span></a>]]></description>
										<content:encoded><![CDATA[<p>Like I said in the previous article &#8220;<a href="https://tutorialspage.com/complete-case-study-watermark-detection-remove-and-place-a-new-watermark-part-i/" rel="bookmark">Complete case study: watermark replacement &#8211; part I</a>&#8221; about replacing an old or outdated  watermark, this task is not quite trivial but more likely boring and very time-consuming.</p>
<p>Anyway&#8230;let&#8217;s continue. I have now separated all images by:</p>
<ul>
<li>logo in the center</li>
<li>multiple logos all over.</li>
</ul>
<p>I will try to process the images that have the logo in the center. In this group I have little bit over 40.000 images.<span id="more-851"></span></p>
<p>I will use for this purpose <a href="http://www.theinpaint.com/batch-inpaint.html">BatchInpaint </a>which is capable of removing objects of the image and refill the spot after that.</p>
<p><img data-recalc-dims="1" loading="lazy" decoding="async" class="alignnone wp-image-908 size-large" src="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/01/screenshot4.jpg?resize=840%2C525&#038;ssl=1" alt="screenshot4" width="840" height="525" srcset="https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/01/screenshot4.jpg?resize=1024%2C640&amp;ssl=1 1024w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/01/screenshot4.jpg?resize=300%2C188&amp;ssl=1 300w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/01/screenshot4.jpg?resize=768%2C480&amp;ssl=1 768w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/01/screenshot4.jpg?resize=1200%2C750&amp;ssl=1 1200w, https://i0.wp.com/tutorialspage.com/wp-content/uploads/2016/01/screenshot4.jpg?w=1280&amp;ssl=1 1280w" sizes="auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px" /></p>
<p><small>**Please note that I am not an affiliate of this software or related in any way with it.</small></p>
<p>Anyway, this little piece of software needs all images to have the same size, so I will have to resize all image using some criteria like original size and aspect ratio and group them by the cropped size for further batch processing.</p>
<p>I used a custom php script and the Phil Brown&#8217;s <a href="https://gist.github.com/philBrown/880506">ImageManipulator </a>class.</p><pre class="urvanov-syntax-highlighter-plain-tag">$dirs = scandir($path_to_your_file);

foreach ($dirs as $dir) {
    if ($dir === '.' or $dir === '..') continue;

    if (is_dir($path . '/' . $dir)) {

        $uri = $path . '/' . $dir;

        $files = glob( $uri."/*".IMAGE_EXT );
        
        print_r($files);

    }
}</pre><p>You can use this to get a nice list of your files which then you can process using the image manipulator class depending of your needs.</p>
<p>I finally get 15 groups of different size of image. So I started creating the layer for the logo to be removed in BatchInpaint.</p>
<p>This took me around 2 hours to complete the 15 size layers and remove the watermark object.</p>
<p>The batch process did a great job, but there were some imperfections. This is no problem since I will put the new watermark on top of the imperfection that BatchInpaint left.</p>
<p>A 50% transparency will do just fine. I used FotoSizer to apply the new watermark but there are lots of software around the web including so.e great classes that you can use.</p>
<p>Now that I have removed all watermark on my images and placed the new one, I have to roll back the folder structure and the image naming. This is easy task. Just get the ID from the database and search for the image with the same name.  When you find the image just use php <a href="http://php.net/manual/es/function.rename.php"><em>rename</em> </a>built in function. It will create the image using the original file and folder naming. Remember that I stored the folder structure when I first move the images to one folder.</p>
<p>Next, I used a free php class called <a href="https://github.com/Dariusp/php-simple-watermark">php-simple-watermark</a>to place a semi transparent watermark over all images.</p>
<p>I first used FotoSizer, the profesional edition to do the job. But did not do a good job since the images were different size and orientation. The watermark was always the same size and on some images it was was wider then the image itself.</p>
<p>I looked for an php class that solve this issue in an elegant way and the &#8220;php class&#8221; did it out of the box. The watermark was resize proportionally to the image width. I really liked it.</p>
<p>rollback folders paragraph</p>
<p>After all this I realize that size in kb of all my images has doubled. I really don&#8217;t know why and when this happened, but I more interested in finding a solution to compress all my images then searching for the step that made them explode in size.</p>
<p>I end up testing ImageMagick, but I first glace I did not find the way to do it. So I returned to my good all FotoSizer. I selected &#8220;Original Size&#8221; for the image size and decreased quality to 90% (you will really not notice any difference in this and you could also try 90% if you concern about space or loading time). I then selected &#8220;Same output&#8221; and overwrite  the original images (you can make a backup os select a different output folder if you are just testing). This how I reduced by half the image size in kb.</p>
<p>All good now. Time to deliver the images. I hope you like this article. I know there are some better ways to achieve the same result, but this were the best I could find at the moment and given the short time to deliver.</p>
<p>You think you can remove the watermark and replace it in a better and faster manner. Please let me know how, in a comment.</p>
<p><strong><em>Later edit:</em></strong> <em>Now that I have remove the watermark and delivered the images, the client let&#8217;s me know that he has another set of 50.000 images to process. So here we go again <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></em></p>
]]></content:encoded>
					
					<wfw:commentRss>https://tutorialspage.com/complete-case-study-watermark-detection-remove-and-place-a-new-watermark-part-ii/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">851</post-id>	</item>
	</channel>
</rss>
