<?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>CSS-TricksCSS-Tricks</title>
	<atom:link href="http://css-tricks.com/php-snippets-feed/" rel="self" type="application/rss+xml" />
	<link>http://css-tricks.com</link>
	<description>Tips, Tricks, and Techniques on using Cascading Style Sheets.</description>
	<lastBuildDate>Fri, 24 May 2013 13:07:49 +0000</lastBuildDate>
	<language></language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>

	<generator>http://wordpress.org/?v=3.5.1</generator>

	
		<item>
			<title>HTTP or HTTPS</title>
			<link>http://css-tricks.com/snippets/php/http-or-https/</link>
			<comments>http://css-tricks.com/snippets/php/http-or-https/#comments</comments>
			<pubDate>Fri, 07 Sep 2012 23:11:37 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=18098</guid>

			<description><![CDATA[<code class="language-markup">if (!empty($_SERVER['HTTPS']) &#38;&#38; $_SERVER['HTTPS'] !== 'off'
    &#124;&#124; $_SERVER['SERVER_PORT'] == 443) {

  // HTTPS

} else {

  // HTTP

}&#8230;</code>]]></description>

			<content:encoded><![CDATA[<pre rel="PHP"><code class="language-markup">if (!empty($_SERVER['HTTPS']) &amp;&amp; $_SERVER['HTTPS'] !== 'off'
    || $_SERVER['SERVER_PORT'] == 443) {

  // HTTPS

} else {

  // HTTP

}</code></pre>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/php/http-or-https/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>

						
		</item>

	
		<item>
			<title>Get Width/Height of Image</title>
			<link>http://css-tricks.com/snippets/php/get-widthheight-of-image/</link>
			<comments>http://css-tricks.com/snippets/php/get-widthheight-of-image/#comments</comments>
			<pubDate>Sat, 18 Aug 2012 17:19:22 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=17780</guid>

			<description><![CDATA[<p>If all you have for an image is the URL, you can still find the dimensions:</p>
<code>&#60;?php

  list($width, $height, $type, $attr) = getimagesize("url/to/image.jpg");

  echo "Image width " . $width;
  echo "Image height " . $height;
  echo "Image type " . $type;
  echo "Attribute " . $attr;

?&#62;&#8230;</code>]]></description>

			<content:encoded><![CDATA[<p>If all you have for an image is the URL, you can still find the dimensions:</p>
<pre rel="PHP" class="lang-php"><code>&lt;?php

  list($width, $height, $type, $attr) = getimagesize("url/to/image.jpg");

  echo "Image width " . $width;
  echo "Image height " . $height;
  echo "Image type " . $type;
  echo "Attribute " . $attr;

?&gt;</code></pre>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/php/get-widthheight-of-image/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>

						
		</item>

	
		<item>
			<title>Import CSV into MySQL</title>
			<link>http://css-tricks.com/snippets/php/import-csv-into-mysql/</link>
			<comments>http://css-tricks.com/snippets/php/import-csv-into-mysql/#comments</comments>
			<pubDate>Sun, 05 Aug 2012 14:51:53 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=17655</guid>

			<description><![CDATA[<code>&#60;?php

$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername ="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";

/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for &#8230;</code>]]></description>

			<content:encoded><![CDATA[<pre rel="PHP" class="lang-php"><code>&lt;?php

$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername ="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";

/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/

/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql &amp;&amp; chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "output.sql";
/********************************/

if (!file_exists($csvfile)) {
        echo "File not found. Make sure you specified the correct path.\n";
        exit;
}

$file = fopen($csvfile,"r");

if (!$file) {
        echo "Error opening data file.\n";
        exit;
}

$size = filesize($csvfile);

if (!$size) {
        echo "File is empty.\n";
        exit;
}

$csvcontent = fread($file,$size);

fclose($file);

$con = @mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
@mysql_select_db($databasename) or die(mysql_error());

$lines = 0;
$queries = "";
$linearray = array();

foreach(split($lineseparator,$csvcontent) as $line) {

        $lines++;

        $line = trim($line," \t");

        $line = str_replace("\r","",$line);

        /************************************
        This line escapes the special character. remove it if entries are already escaped in the csv file
        ************************************/
        $line = str_replace("'","\'",$line);
        /*************************************/

        $linearray = explode($fieldseparator,$line);

        $linemysql = implode("','",$linearray);

        if($addauto)
                $query = "insert into $databasetable values('','$linemysql');";
        else
                $query = "insert into $databasetable values('$linemysql');";

        $queries .= $query . "\n";

        @mysql_query($query);
}

@mysql_close($con);

if ($save) {

        if (!is_writable($outputfile)) {
                echo "File is not writable, check permissions.\n";
        }

        else {
                $file2 = fopen($outputfile,"w");

                if(!$file2) {
                        echo "Error writing to the output file.\n";
                }
                else {
                        fwrite($file2,$queries);
                        fclose($file2);
                }
        }

}

echo "Found a total of $lines records in this csv file.\n";

?&gt;</code></pre>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/php/import-csv-into-mysql/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>

						
		</item>

	
		<item>
			<title>Generate Expiring Amazon S3 Link</title>
			<link>http://css-tricks.com/snippets/php/generate-expiring-amazon-s3-link/</link>
			<comments>http://css-tricks.com/snippets/php/generate-expiring-amazon-s3-link/#comments</comments>
			<pubDate>Tue, 03 Jul 2012 13:39:21 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=17382</guid>

			<description><![CDATA[<p>You don't have to make files on Amazon S3 public (they aren't by default). But you can generate special keys to allow access to private files. These keys are passed through the URL and can be made to expire. </p>
<code>&#60;?php 

  if(!function_exists('el_crypto_hmacSHA1')){
    /**
    * Calculate the HMAC SHA1 hash of a string.
    *
    * @param string $key The key to hash against
    * @param string $data The data to hash
    * @param int $blocksize Optional blocksize
    * @return string HMAC SHA1
    &#8230;</code>]]></description>

			<content:encoded><![CDATA[<p>You don't have to make files on Amazon S3 public (they aren't by default). But you can generate special keys to allow access to private files. These keys are passed through the URL and can be made to expire. </p>
<pre rel="PHP" class="lang-php"><code>&lt;?php 

  if(!function_exists('el_crypto_hmacSHA1')){
    /**
    * Calculate the HMAC SHA1 hash of a string.
    *
    * @param string $key The key to hash against
    * @param string $data The data to hash
    * @param int $blocksize Optional blocksize
    * @return string HMAC SHA1
    */
    function el_crypto_hmacSHA1($key, $data, $blocksize = 64) {
        if (strlen($key) &gt; $blocksize) $key = pack('H*', sha1($key));
        $key = str_pad($key, $blocksize, chr(0x00));
        $ipad = str_repeat(chr(0x36), $blocksize);
        $opad = str_repeat(chr(0x5c), $blocksize);
        $hmac = pack( 'H*', sha1(
        ($key ^ $opad) . pack( 'H*', sha1(
          ($key ^ $ipad) . $data
        ))
      ));
        return base64_encode($hmac);
    }
  }

  if(!function_exists('el_s3_getTemporaryLink')){
    /**
    * Create temporary URLs to your protected Amazon S3 files.
    *
    * @param string $accessKey Your Amazon S3 access key
    * @param string $secretKey Your Amazon S3 secret key
    * @param string $bucket The bucket (bucket.s3.amazonaws.com)
    * @param string $path The target file path
    * @param int $expires In minutes
    * @return string Temporary Amazon S3 URL
    * @see http://awsdocs.s3.amazonaws.com/S3/20060301/s3-dg-20060301.pdf
    */
    
    function el_s3_getTemporaryLink($accessKey, $secretKey, $bucket, $path, $expires = 5) {
      // Calculate expiry time
      $expires = time() + intval(floatval($expires) * 60);
      // Fix the path; encode and sanitize
      $path = str_replace('%2F', '/', rawurlencode($path = ltrim($path, '/')));
      // Path for signature starts with the bucket
      $signpath = '/'. $bucket .'/'. $path;
      // S3 friendly string to sign
      $signsz = implode("\n", $pieces = array('GET', null, null, $expires, $signpath));
      // Calculate the hash
      $signature = el_crypto_hmacSHA1($secretKey, $signsz);
      // Glue the URL ...
      $url = sprintf('http://%s.s3.amazonaws.com/%s', $bucket, $path);
      // ... to the query string ...
      $qs = http_build_query($pieces = array(
        'AWSAccessKeyId' =&gt; $accessKey,
        'Expires' =&gt; $expires,
        'Signature' =&gt; $signature,
      ));
      // ... and return the URL!
      return $url.'?'.$qs;
    }
  }

?&gt;</code></pre>
<h4>Usage</h4>
<pre rel="PHP" class="lang-php"><code>&lt;?php echo el_s3_getTemporaryLink('your-access-key', 'your-secret-key', 'bucket-name', '/path/to/file.mov'); ?&gt;</code></pre>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/php/generate-expiring-amazon-s3-link/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>

						
		</item>

	
		<item>
			<title>Get Current Page URL</title>
			<link>http://css-tricks.com/snippets/php/get-current-page-url/</link>
			<comments>http://css-tricks.com/snippets/php/get-current-page-url/#comments</comments>
			<pubDate>Mon, 18 Jun 2012 13:08:31 +0000</pubDate>
			<dc:creator>Chris Coyier</dc:creator>
				<guid isPermaLink="false">http://css-tricks.com/?page_id=17223</guid>

			<description><![CDATA[<code>function getUrl() {
  $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
  $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
  $url .= $_SERVER["REQUEST_URI"];
  return $url;
}&#8230;</code>]]></description>

			<content:encoded><![CDATA[<pre rel="PHP" class="lang-html"><code>function getUrl() {
  $url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
  $url .= ( $_SERVER["SERVER_PORT"] !== 80 ) ? ":".$_SERVER["SERVER_PORT"] : "";
  $url .= $_SERVER["REQUEST_URI"];
  return $url;
}</code></pre>
]]></content:encoded>

			<wfw:commentRss>http://css-tricks.com/snippets/php/get-current-page-url/feed/</wfw:commentRss>
			<slash:comments>6</slash:comments>

						
		</item>

	
</channel>

</rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk (User agent is rejected)
Content Delivery Network via cdn.css-tricks.com

 Served from: css-tricks.com @ 2013-05-25 17:13:26 by W3 Total Cache -->