Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Tuesday, March 6, 2012

Efficient file storage for user uploaded files

While disk space is cheap, dealing with large numbers of files can be tedious, and in some cases unnecessary.  Before considering file naming conventions, consider the uniqueness of your files.  At work, we allow our customers to upload logos.  It's possible for multiple customers to upload the same logo.  The last thing we wanted was to maintain multiple copies of the same logo on the server.

We decided our best approach would be to name the files based on the contents of the file.  If the contents changed, so did the filename.

$filename = md5(file_get_contents($pathToFile));

The MD5 hashed filename is what was linked to the user logos.  This turned out to be a fantastic solution for our needs.

What naming conventions do you use for user uploaded files?  This method seems like it would work well for any uploaded files because nothing would ever be the same.  Do you agree or disagree?  Why?

Sunday, July 12, 2009

Valid RSS 2.0 Generator Class

I've been searching Google and couldn't find anything except documentation. So I'm posting this here in hopes that it will help someone else from having to go through a few hours of Trial and Error getting their feed to validate. This is tested only to pass the W3.org RSS 2.0 Validator.

I initially programmed it with required key values and things utilizing our custom api, but removed all of that for a stripped down basic version. If anyone has any updates/improvements to this feed or has any classes for other kinds of feeds I'd definitely be interested in hearing about them.

Post a comment if you use this and find it useful.

<?php
class FeedRss {
 
 var $feed = "";
 
 /*===========================================*/
 /* Public Functions
 /*===========================================*/
 public function create ($paramList) {
     $paramList = $this->cleanParamList($paramList);    
 
     $this->feed .= "<?xml version='1.0' encoding='iso-8859-1'?>\n";
     $this->feed .= "    <rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>\n";
     $this->feed .= "        <channel>\n";
     $this->feed .= "            <atom:link href='".$paramList['location']."' rel='self' type='application/rss+xml' />\n";
     $this->feed .= "            <title>".$paramList['title']."</title>\n";
  
  if (array_key_exists("copyright", $paramList)) {
       $this->feed .= "            <copyright>".$paramList['copyright']."</copyright>\n";
  }
  
  $this->feed .= "            <link>".$paramList['link']."</link>\n";
  $this->feed .= "            <description>".$paramList['description']."</description>\n";
  
  if (is_numeric($paramList["updated"])) {
   $this->feed .= "            <lastbuilddate>".date('D, d M Y H:i:s O', $paramList['updated'])."</lastBuildDate>\n";
  } else {
   $this->feed .= "            <lastbuilddate>".$paramList['updated']."</lastBuildDate>\n";
  }
  
  $this->feed .= "            <language>".$paramList['language']."</language>\n";
  
  if (array_key_exists("image", $paramList)) {
   $imageInput = array_merge($paramList["image"], array(
    "title" => $paramList["title"]
   ));
   $this->addImage($imageInput);
  }
 }
 
 public function addItem ($paramList) {
  $paramList = $this->cleanParamList($paramList);    
 
  $this->feed .= "        <item>\n";
  $this->feed .= "            <title><![CDATA[".$paramList['title']."]]></title>\n";
  $this->feed .= "            <link>".$paramList['link']."</link>\n";
  $this->feed .= "            <description><![CDATA[".$paramList['description']."]]></description>\n";
  
  if (array_key_exists("author", $paramList)) {
   $this->feed .= "            <author>".$paramList['author']."</author>\n";
  }
  
  if (array_key_exists("guid", $paramList)) {
   $this->feed .= "            <guid>".$paramList['guid']."</guid>\n";
  }
  
  if (is_numeric($paramList["updated"])) {
   $this->feed .= "            <pubdate>".date('D, d M Y H:i:s O', $paramList['updated'])."</pubDate>\n";
  } else {
   $this->feed .= "            <pubdate>".$paramList['updated']."</pubDate>\n";
  }
  
  if (array_key_exists("category", $paramList)) {
   $this->feed .= "            <category>".$paramList['category']."</category>\n";
  }
  
  $this->feed .= "        </item>\n";
 }
 
 public function get () {
  $this->feed .= "        </channel>\n";
  $this->feed .= "    </rss>";
  
  return $this->feed;
 }
 
 /*===========================================*/
 /* Private Functions
 /*===========================================*/
 
 private function addImage ($paramList) {
  $this->feed .= "            <image>\n";
  $this->feed .= "                <url>".$paramList["url"]."</url>\n";
  $this->feed .= "                <width>".$paramList["width"]."</width>\n";
  $this->feed .= "                <height>".$paramList["height"]."</height>\n";
  $this->feed .= "                <title>".$paramList["title"]."</title>\n";
  $this->feed .= "                <link>".$paramList["link"]."</link>\n";
  $this->feed .= "            </image>\n";
 }    
 private function cleanParamList ($paramList) {
  $ignoreParam = array("updated", "description");
  
  $replaceList = array(
   "&" => "&amp;",
   "–" => "&ndash;",
   "—" => "&mdash;",
   "¡" => "&iexcl;",
   "¿" => "&iquest;",
   "«" => "&raquo;",
   "<" => "&lt;",
   ">" => "&gt;",
   "$" => "&#36;",
   "»" => "&laquo;",
   "¢" => "&cent;",
   "©" => "&copy;",
   "÷" => "&divide;",
   "ยต" => "&micro;",
   "·" => "&middot;",
   "¶" => "&para;",
   "±" => "&plusmn;",
   "€" => "&euro;",
   "£" => "&pound;",
   "®" => "&reg;",
   "§" => "&sect;",
   "¥" => "&yen;",
   "’" => "'",
   "“" => "\"",
   "”" => "\""
  );
  
  foreach ($paramList as $key => $value) {
   if (!in_array($key, $ignoreParam)) {
    $paramList[$key] = str_replace(array_keys($replaceList), array_values($replaceList), $str);
   }
  }
  
  return $paramList;
 }
}
?>


Usage:
//if writing to a file via cron or any other method, comment out this header
header("Content-Type: application/xml; charset=ISO-8859-1");

$feed->create(array(
 "title" => "Domain.com News",
 "copyright" => "Copyright (c) ".date("Y")." Site Inc. All rights reserved.",
 "link" => "http://www.domain.com",
 "location" => "http://feed.domain.com/feed.xml",
 "description" => "All of the latest news",
 "language" => "en-us",
 "image" => array(
 "url" => "http://www.domain.com/image/branding/rss-logo.jpg",
 "height" => 50,
 "width" => 50,
 "title" => "RSS feed from Domain.com",
 "link" => "http://www.domain.com"
)
));

//add as many items as you'd like
$feed->addItem(array(
 "title" => $news["title"],
 "link" => "http://www.domain.com/index.php?sec=news&act=view&id=".$news["id"],
 "guid" => "http://www.domain.com/index.php?sec=news&act=view&id=".$news["id"],
 "description" => $tempNewsItem["preview_cache"],
 "author" => "news@domain.com (author)", //for proper validation you should have an email AND an author's name of some sort
 "category" => "Computer Hardware",
 "updated" => $news["posted"]
));

//this can also be used to write to a file
echo $feed->get(); 

Saturday, July 11, 2009

Success Messages

In almost all of my web sites I have setup forms on our site so that upon submitting successfully, they would either show the success message at the top of the page you were just at or show only a success message on the page and nothing else. While this wasn't the best practice, I couldn't think of a better route to take. My partner recently presented a new idea. Instead of doing it that way and requiring more user input as to what the next step would be, he suggested that we use session variables to store the success message and forward the user to a page they would likely want to be on. So when sending an email, rather than loading a page that just says "Email Sent!", we forward them back to their inbox and show the success message there.
When doing this in PHP, if you use a header() forward your session variable will not save unless you force PHP to write the session. To do this, you would use session_write_close().
$_SESSION["success"] = "Your email has been sent"];
session_write_close(); //without this, the session variable will not be saved before header() executes

header("Location: http://www.domain.com/my-inbox/");