Personal Blog of Richard K. Baldwin

RSS
Jan 6

File upload problems with CodeIgniter

I’ve been exploring CodeIgniter for a recent PHP project and I must say that I like what I see. With any framework there is a learning curve but CI is very easy to get up and running. I also like the fact that CI really encourages the of MVC for projects. If you have done a little PHP and want to explore the world of MVC CI is a great choice. 

Anyway, after added an upload from to my project and testing it out with a few images I starting receiving the “The filetype you are attempting to upload is not allowed” error when attempting to upload a csv file. After digging around for a bit I found that CI uses a mimes.php file the application/config directory that controls which mime types are allowed for certain code. For csv they were about 10 different ones in the array. Since the error was so specific I figured my file must have the wrong mime type even though I saved it from Excel as a .csv. I used the bit of code below in my upload function in order to see my mime type.

$config[‘*’] = ‘csv’;

$data = array(‘upload_data’ => $this->upload->data());

$mimetype = $data[‘upload_data’][‘file_type’];

echo $mimetype;

After uploading my file I found that the mime type was text/plain and not csv. I added the text/plain option to my mimes.php csv array and whala!! Success! Hopefully this will help someone on the W (short for web).

The target directory is not empty error using CruiseControl

I recently came across the following error while using CruiseControl to build a .NET project “The target directory is not empty, and does not appear to contain a previously compiled application. Please delete it manually, or choose a different target”. At first this stumped me because I didn’t realize that on each build CruiseControl was deleting the existing project files and checking out new copies from our SVN repository. Once a coworker clued me in I was able to browse to the development directory and see a few folders that remained after the delete was performed. This is what was causing the error during the build.

After closer inspection it appears that these files were being used by a different process on the server and therefore couldn’t be deleted. In the end stopping IIS allowed the files to be deleted and CruiseControl was able to build the project without any problems.

Two things to take away from this. First, if CruiseControl is giving you the “directory is not empty” error then the problem is probably permissions related. Secondly, don’t be afraid to ask for help!

Conditionally show a Drupal login form on any page

I was recently asked to add a Drupal log-in form to a specific page in a website. However, the trick was to display the form if the user wasn’t logged in and to show an already logged in message otherwise. By default Drupal creates a login form block that can be accessed via the drupal_get_form method. This is handy because you can place this form call anywhere in your code and even wrap it in conditional logic. I used the Drupal $user variable to check if the user is logged in.

In the end the code snippet was pretty simple but I did use the PHP alternative syntax to make it a little easier to read (at least to me). Maybe that is just my ColdFusion background bleeding into my PHP code. The snippet is below:

<?php if (!$user->uid): ?>
    <p id=”loggedInMessage”> Please Log in:</p>
    <?php print drupal_get_form(‘user_login_block’); ?>
<?php else: ?>
    <p id=”notLoggedInMessage”> You are currently logged in.</p>
<?php endif; ?>  

SharePoint 2010 “Layout changes are disabled for this zone” error

Are you getting a non-descriptive error page in SharePoint 2010 when you try to edit and save a page? Are you having trouble adding a webpart to the page content field or another field on your page. Perhaps you are receiving the following error: “Layout changes are disabled for this zone”. Maybe you have dug through your log files and you’ve found this error: “System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.WebPartPages.WikiPageWebPartSaver. SaveWebPartsInRichText(SPWebPartManager wpmgr)”.

If so, the solution may be a quick and easy fix. Search your custom master page and your page layout to ensure you haven’t added an additional form tag to the markup. For me the code provided by the designer had the simple search form below in the markup. I had not reached the point of wiring up the search form yet so I didn’t notice it at all.

<form action=”search.aspx” method=”get”>

<input type=”text” name=”query” title=”Enter Keywords” placeholder=”Enter Keywords” class=”searchBox” id=”query” />          

<input type=”submit” title=”Search” value=”Search” class=”submit” />

</form>

After I removed the form tag and changed the submit button to a regular button the issue was resolved. It would be nice if SharePoint threw an error similar to “Hey Dummy, you have a form inside a form” but unfortunately it doesn’t. Hopefully you find this tip useful!

Faking a DNS entry using your hosts file

Have you ever wanted to check a newly launched website for bugs or broken links without waiting for the updated DNS entry to propagate to your DNS server. If so, you can modify the hosts file on your computer which allows you to bypass the DNS server and go straight to the IP address of your choice. This can be really handy when you have added new host names for an IP address and you would like to test them out via a browser. Here are the steps below for faking a DNS entry on your local machine (Windows).

  • First locate the directory that contains your hosts file (C:\Windows\System32\drivers\etc) on most Windows machines
  • Next open Notepad (or editor of choice) as an administrator. To do this just right click on the program and choose Run as administrator.
  • Now add an entry at the bottom of the file for the domain name you would like to fake and the IP address of the website server (e.g. 74.125.159.99    www.mysite.com)
  • Save the hosts file
  • Open Command Prompt
  • Enter ipconfig /flushdns
  • Now try browsing to the domain name you entered above (you may also need to clear your browser’s cache)

Of course, you will want to remove this from your hosts file after the domain name is working for everyone else but you should have 30 minutes or more to test the site before anyone else sees it.

Drupal migration and 404 errors

Have you recently transfer a Drupal site to a new server but now you are receiving the dreaded Not Found errors? Here is an example:

Not Found

The requested URL /about was not found on this server.

Apache/2.2.12 (Ubuntu) Server at mywebsite.com Port 80

Make sure that you have copied over your .htaccess file with the correct php/Drupal settings. Often we do not want the same .htaccess settings on one server that we have on another (development versus production for example) but Drupal does need the basic settings. Hopefully this will save someone some time in the migration process.



Oct 4

SharePoint 2010 use master page and page layout to add a body class

Are you creating a SharePoint 2010 publishing site and would like to change the body class attribute from one page layout to another. Out of all of the OOTB SharePoint content placeholders there isn’t one that handles the class attribute on the body tag. Quite often from page to page or section to section on a website the designers that I work with like to have a body class specific to that page or that section of the site. This allows them to create css that targets just that page, which keeps the css rules simpler and avoids unwanted cascading.

Read More

I’m amazed at how many teens on Facebook have 1000+ friends but they can’t find anyone willing to take their picture.

- Richard Baldwin

SharePoint 2010 “Code blocks are not allowed in this file” error

If you have run into the “Code blocks are not allowed in this file” error in SharePoint 2010 then you are probably trying to add code to a master page or page layout. To allow code blocks just add the page to the PageParserPath section in your web.config. One gotcha is that if you are doing this on an extended site you will need add your page to the PageParserPath for both the main site and the extended site. Best practice is to put all the code you can in a code behind page but if you need to add it directly to a master page or page layout then this tip if for you. Read more here.