Fileinfo

From DreamHost
Jump to: navigation, search
This article or section may require a cleanup.
We are hoping to create articles that meet certain standards. Please discuss this issue on the talk page. Editing help is available.

About Fileinfo

The functions in this module try to guess the content type and encoding of a file by looking for certain magic byte sequences at specific positions within the file. While this is not a bullet proof approach the heuristics used do a very good job. --Taken directly from php.net. Needs re-wording

Installing Fileinfo

The instructions provided in this article or section are considered advanced.

You are expected to be knowledgeable in the UNIX shell.
Support for these instructions is not available from DreamHost tech support.
Server changes may cause this to break. Be prepared to troubleshoot this yourself if this happens.
We seriously aren't kidding about this.

Please Note: This installation method (including the script) is currently *NOT* complete and verification as to whether or not the Fileinfo extension even works using the installation script below is not certain. Please use the Discussion Page to help further develop this guide and the script below. This article will remain a stub until a fully functional method to install and get the Fileinfo extension working is discovered.

UPDATE

New instructions for installing FileInfo, just follow the steps outlined in the PHP.ini section to set your domain up on PHP 5.3, then create a phprc file with the following line:

extension="fileinfo.so"

The installation of Fileinfo no longer requires Installing_PHP5.
If you are unable to do so, then you may not be able to use Fileinfo on your DreamHost account.

Below is the old non-working install script for the Fileinfo PHP module.
Please make sure to run 'dos2unix fileinfo_ext.sh' from the shell if you use a Windows-based editor to create this file.

fileinfo_ext.sh

#!/bin/sh
set -e

# Version 0.3, 2007-07-13
#
# - Initial Release

#### User Configuration Options
# Temporary source directory
SRCDIR=${HOME}/source
# Download temporary DIST files to which directory?
DISTDIR=${HOME}/dist
# Delete contents of DISTDIR after installation? (Default: No)
DISTDEL="No"
# Install Suhosin to which directory?
# Note: This *MUST* be set to your PHP5 installation directory!
INSTALLDIR=${HOME}/php5
# Nice Level for Processes. (Depreciated)
# Higher is nicer, lower is less nice and could get your install process killed!
NICE=19

## Program Version Configuration
# Don't touch unless you know what you're doing!
AUTOCONF="autoconf-2.61"
AUTOMAKE="automake-1.10"
FINFO="Fileinfo-1.0.4"
# What features do you want enabled?
FINFOFEATURES="--prefix=${INSTALLDIR}"

#### END User Configuration Options

########## DO NOT MODIFY BELOW ##########
sleep 1s

# DO NOT MODIFY THIS LINE OR IT *WILL* BREAK YOUR INSTALL
MAGIC="file_4.21.orig"

# Push the install dir's bin directory into the path
export PATH=${INSTALLDIR}/bin:$PATH

# Clear and/or create the source directory.
if [ -d ${SRCDIR} ]; then
		  echo "Source directory already exists! Cleaning it..."
		  rm -rf $SRCDIR/*
else
		  echo "Creating source directory..."
		  mkdir -p ${SRCDIR}
fi
# Create the dist files directory if it doesn't exist
# optionally cleaning it if it does exist already.
if [ -d ${DISTDIR} ]; then
	echo ""; echo "Distribution directory already exists!"; echo "Clean it?"
	if [ ${DISTDEL} == "Yes" ]
	then
		echo ""; echo "Yes!"; echo "Cleaning now..."; echo ""
		rm -rf $DISTDIR/*
	else
		echo ""; echo "No!"; echo "Leaving the distribution directory intact."; echo ""
	fi
else
		  echo "Creating distribution directory..."
		  mkdir -p ${DISTDIR}
fi
# Make sure the extensions directory exists.
if [ -d ${INSTALLDIR}/lib/php/extensions ]; then
		  echo "lib/php/extensions folder already exists! Doing nothing..."
else
		  mkdir -p ${INSTALLDIR}/lib/php/extensions
fi

# Detect how many processors the system has (for more optimal compliation).
cores=2   # the number of cores/procs to use when building
if [ $cores -a $cores -gt 1 ]; then
		  j="-j$cores "
fi
		  OS=`uname -s`
if [ "Darwin" = $OS ]; then
		  sed=gnused
		  makefile=makefile.macosx
else
		  makefile=makefile.linux_x86_ppc_alpha
		  sed=sed
fi

for i in $sed wget; do
		  $i --version >/dev/null 2>&1
done

## Check if packages already exist and get packages the ones that don't.
cd ${DISTDIR}
# Do not abort on errors.
set +e
# Wget options
WGETOPT="-t1 -T10 -w5 -q -c"

# Do some of our own error checking here too.
if [ -a ${DISTDIR}/${AUTOCONF}.tar.gz ]; then
		  echo "Skipping wget of ${AUTOCONF}.tar.gz"
else
		  wget $WGETOPT ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/distfiles/${AUTOCONF}.tar.gz
		  # If primary mirror fails, use the alternative mirror.
		  if [ -a ${DISTDIR}/${AUTOCONF}.tar.gz ]; then
			echo "Got ${AUTOCONF}.tar.gz"
		  else
				wget $WGETOPT ftp://ftp.gnu.org/gnu/autoconf/${AUTOCONF}.tar.gz
				# Check to make sure the alternative mirror worked.
				if [ -a ${DISTDIR}/${AUTOCONF}.tar.gz ]; then
					echo "Got ${AUTOCONF}.tar.gz"
				else
					echo "Failed to get ${AUTOCONF}.tar.gz. Aborting install!"
					exit 0
				fi
		  fi
fi
if [ -a ${DISTDIR}/${AUTOMAKE}.tar.bz2 ]; then
		  echo Skipping wget of ${AUTOMAKE}.tar.bz2
else
		  wget $WGETOPT ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/distfiles/${AUTOMAKE}.tar.bz2
		  # If primary mirror fails, use the alternative mirror.
		  if [ -a ${DISTDIR}/${AUTOMAKE}.tar.bz2 ]; then
			echo "Got ${AUTOMAKE}.tar.bz2"
		  else
				wget $WGETOPT ftp://ftp.gnu.org/gnu/automake/${AUTOMAKE}.tar.bz2
				# Check to make sure the alternative mirror worked.
				if [ -a ${DISTDIR}/${AUTOMAKE}.tar.bz2 ]; then
					echo "Got ${AUTOMAKE}.tar.bz2"
				else
					echo "Failed to get ${AUTOMAKE}.tar.bz2. Aborting install!"
					exit 0
				fi
		  fi
fi
if [ -a ${DISTDIR}/${MAGIC}.tar.gz ]; then
		  echo "Skipping wget of ${MAGIC}.tar.gz"
else
		  wget $WGETOPT http://ftp.debian.org/debian/pool/main/f/file/${MAGIC}.tar.gz
		  # If primary mirror fails, use the alternative mirror.
		  if [ -a ${DISTDIR}/${MAGIC}.tar.gz ]; then
			echo "Got ${MAGIC}.tar.gz"
		  else
				wget $WGETOPT http://debian.cruisix.net/debian/pool/main/f/file/${MAGIC}.tar.gz
				wget $WGETOPT http://ftp.linux.org.tr/ubuntu/pool/main/f/file/${MAGIC}.tar.gz
				# Check to make sure the alternative mirror worked.
				if [ -a ${DISTDIR}/${MAGIC}.tar.gz ]; then
					echo "Got ${MAGIC}.tar.gz"
				else
					echo "Failed to get ${MAGIC}.tar.gz. Aborting install!"
					exit 0
				fi
		  fi
fi
if [ -a ${DISTDIR}/${FINFO}.tgz ]; then
		  echo "Skipping wget of ${FINFO}.tgz"
else
		  wget $WGETOPT ftp://ftp.ucsb.edu/pub/mirrors/linux/gentoo/distfiles/${FINFO}.tgz
		  # If primary mirror fails, use the alternative mirror.
		  if [ -a ${DISTDIR}/${FINFO}.tgz ]; then
			echo "Got ${FINFO}.tgz"
		  else
				wget $WGETOPT http://pecl.php.net/get/${FINFO}.tgz
				# Check to make sure the alternative mirror worked.
				if [ -a ${DISTDIR}/${FINFO}.tgz ]; then
					echo "Got ${FINFO}.tgz"
				else
					echo "Failed to get ${FINFO}.tgz. Aborting install!"
					exit 0
				fi
		  fi
fi
set -e

# Extract the source files into the source directory.
cd ${SRCDIR}
echo "Extracting ${AUTOCONF}..."
tar xzf ${DISTDIR}/${AUTOCONF}.tar.gz > /dev/null
echo "Done."
echo "Extracting ${AUTOMAKE}..."
tar xjf ${DISTDIR}/${AUTOMAKE}.tar.bz2 > /dev/null
echo "Done."
echo "Extracting ${MAGIC}..."
tar xzf ${DISTDIR}/${MAGIC}.tar.gz > /dev/null
echo "Done."
echo "Extracting ${FINFO}..."
tar xzf ${DISTDIR}/${FINFO}.tgz > /dev/null
echo "Done."

# This part is ugly, as the modifiction done here is *not* official nor should it be supported.
# Documentation on creating this file should be made and this method removed in future builds.
if [ -a ${DISTDIR}/finfo/config.m4 ]; then
		  echo "Skipping copying and patching of config.m4"
else
		  if [ -d ${DISTDIR}/finfo ]; then
		  	echo "finfo directory exists! Skipping."
		  else
		  	echo "Creating ${DISTDIR}/finfo directory..."
		  	mkdir -p ${DISTDIR}/finfo
		  fi
		  cd ${DISTDIR}/finfo
		  cp ${SRCDIR}/${FINFO}/config.m4 .
		  sed "s/SEARCH_PATH=\"/SEARCH_PATH=\"\$HOME\/source\/file /g" config.m4 > config.m4
		  sed "s/MAGIC_MIME_DIRS=\"/MAGIC_MIME_DIRS=\"\$HOME\/source\/file /g" config.m4 > config.m4
		  # No secondary mirror currently exists, nor should there be one once
		  # this script is properly setup.
		  if [ -a ${DISTDIR}/finfo/config.m4 ]; then
			echo "Got config.m4 patched ok!"
		  else
			echo "Failed to copy config.m4. Aborting install!"
			exit 0
		  fi
fi

# Required exports
export PATH=${SRCDIR}/bin:$PATH
export PHP_PREFIX=${INSTALLDIR}/bin

## Compile deps and install FileInfo
#AUTOCONF
cd ${SRCDIR}/${AUTOCONF}
./configure --prefix=${SRCDIR}
# make clean
nice -n ${NICE} make
make install

#AUTOMAKE
cd ${SRCDIR}/${AUTOMAKE}
./configure --prefix=${SRCDIR}
# make clean
nice -n ${NICE} make
make install

#MAGIC
cd ${SRCDIR}/file-4.21
./configure --prefix=${SRCDIR}/file
# make clean
nice -n ${NICE} make
make install

#FILEINFO
cd ${SRCDIR}/${FINFO}
cp ${DISTDIR}/finfo/config.m4 ${SRCDIR}/${FINFO}/config.m4
$PHP_PREFIX/phpize
./configure ${FINFOFEATURES}
# make clean
nice -n ${NICE} make

# Install Fileinfo now by copying the lib file over to the PHP extension dir.
cp modules/fileinfo.so ${INSTALLDIR}/lib/php/extensions/fileinfo.so

# Post install clean-up.
#rm -rf $SRCDIR
if [ ${DISTDEL} == "Yes" ]; then
	rm -rf $DISTDIR
elif [ ${DISTDEL} == "No" ]; then
	echo "Your DISTDIR will not be cleaned."
else
	echo "Unknown DISTDEL option! Cleaning your DISTDIR by default."
fi

## End of Install
echo "Installation completed!" `date +%r`
echo ""
echo "Please note that your SOURCE directory has NOT been cleaned during this beta installation for debugging purposes."
echo "You will need to clean it manually if you wish to re-install this extension."

#EOF


php.ini modifications

Locate the following line(s) in your php.ini file:

; Directory in which the loadable extensions (modules) reside.
extension_dir = "./"

Modify the extension_dir line to look like this:

; Directory in which the loadable extensions (modules) reside.
extension_dir = "~/php5/lib/php/extensions"


Now add the following near the very end of your current php.ini file.

[fileinfo]
extension="fileinfo.so"

Config.m4 Modifications

Some further information about why this file needs to be modified should be added here. The config.m4 file, located in the Fileinfo-1.0.4.tgz package, needs to be modified in order to point to a local (user's directory) install of the 'libmagic' libraries (which would be had from the file_4.21.orig.tar.gz package, once installed). A modified config.m4 file is currently provided by the Fileinfo install script; however, it truly should be created, or rather modified, by the user themselves.

Below is a differential of the changes between the original config.m4 file and what a modified config.m4 file should look like.

line 8
-   SEARCH_PATH="/usr/local /usr /usr/share/file"
+   SEARCH_PATH="$HOME/source/file"

line 50
-   MAGIC_MIME_DIRS="/usr/local/share/file /usr/share/file /usr/share/misc/file /etc /usr/share/misc"
+   MAGIC_MIME_DIRS="$HOME/source/file"

Workarounds

So far the best (assumed) method to get the Fileinfo script to work properly, would be to ask DreamHost Support to install the 'libmagic-dev' debian package on your server. Then follow the normal instructions for installing Fileinfo, rather than use the above install script.

Alternatively, using PHP, you can just use the PHP exec() or similar functions to execute any Windows or Unix file utility that at least gets similar results. The following example gets the mime type of your file using Unix's 'file' utility:

exec("/usr/bin/file -i -b $path_to_your_file");

See: exec PHP function

See: Unix 'file' utility

External Links