Archive

Archive for December, 2009

File Type Detection in PHP

December 24th, 2009 Ivan Villareal No comments

I’m working on a project where the users can upload files, and I need to know the type of file they are uploading, a simple solution would be to check the extensions of the files, but this has many flaws, so I was looking for a reliable way to detect the type of file, and I found a PECL extension called file info this extension is enabled by default in PHP 5.3, unfortunately I’m runing PHP 5.2.10, and the prod server also has this version.

So what I did to install this on ubuntu 9.10 karmic was the following:

 

root@mini:/etc/php5/apache2# apt-get install php5-dev libmagic-dev php-pear
....
root@mini:/etc/php5/apache2# pear channel-update pear.php.net
...
root@mini:/etc/php5/apache2# pecl install Fileinfo
....
Build process completed successfully
Installing '/usr/lib/php5/20060613+lfs/fileinfo.so'
install ok: channel://pear.php.net/Fileinfo-1.0.4

 

and finally I added the extension to php.ini

extension=fileinfo.so

I restarted the apache server, and tested with this

          <!--DVFMTSC-->$fileInfo = new finfo(FILEINFO_MIME); <!--DVFMTSC--> 
$mimeType = $fileInfo->buffer(file_get_contents($uploadedFile));

 

Categories: Linux, PHP Tags: , , ,

Format a Mysql DateTime Field with Zend_Date

December 10th, 2009 Ivan Villareal No comments

Zend_Date is really time saver class, I use it in almost every project I work, It handles time_zones, and date formatting pretty easy, leaving me more time to focus on other things.

The files that I need to use Zend_Date as a standalone class are:

Date.php and the Date folder from the Zend distribution package, and this is how I use it:

< ?php
set_include_path('./lib' . PATH_SEPARATOR . get_include_path());
include_once 'Zend/Date.php';
$tugId = isset($_GET['tugId']) ? (int) $_GET['tugId'] : false;
 
function formatDate($date) {
    /**
     * List of supported timezones
     * http://php.net/manual/en/timezones.php
     */
    if ($date) {
        $fmtDate = new Zend_Date($date, Zend_Date::ISO_8601);
        $fmtDate->setTimezone('America/Los_Angeles');
        return $fmtDate->toString(Zend_Date::DATETIME);
    } else {
        return '';
    }
}
 
if (is_int($tugId)) {
 $sql = 'SELECT modified_date FROM tuggler WHERE tug_id = ' . $tugId;
 $result = $db->fetchAssoc($sql);
 $response = 'Last Record Update: '.formatDate($row['modified_date']) .'<br />';
} else {
 $response "nothing to return";
}
 
echo $response;

The response from the above would be Jul 9, 2009 11:41:20 AM but I can easily change it using one of the predefined constants, or I can make my own format.

 

Categories: Development, PHP Tags:

Mount a remote filesystem using sshfs

December 9th, 2009 Ivan Villareal No comments

I often have to upload files to several servers, and some of them doesn’t have an rsync or ftp server, so if I’m going to make a deployment, I have to use scp, or sftp to upload the files.

This works well when I need to make a quick change or test 1 file, but If I have to synchronize several files it is a very time consuming task.

So what I use in this cases is sshfs it is a pretty cool tool, to allow me to mount any filesystem so I can use any rsync on a remote server without dealing with opening ports configure, the server or do other stuff,

Gentoo is my main OS, so it was really easy to have this working, I had to reconfigure the kernel to support fuse

Symbol: FUSE_FS [=m]
Location:
  -> File systems
      -> Filesystem in Userspace support

and after this was ready I only installed sys-fs/sshfs-fuse

[I] sys-fs/sshfs-fuse
     Available versions:  1.9 ~2.1 2.2
     Installed versions:  2.2(12:33:09 PM 09/08/2009)
     Homepage:            http://fuse.sourceforge.net/sshfs.html
     Description:         Fuse-filesystem utilizing the sftp service.

when it was ready I did the following to mount the remote filesystem:

ivan@gondor ~ $ sshfs phpfix@prestant.citizenhawk.net: preasent/

After this I’m able to use the remote file system as usual.

Categories: Linux Tags: , , , ,