Jump to content

[SOLVED] How do I create symlinks instead of copying a file


darkshadow

Recommended Posts

First off I have absolutely no php expertise and this is not my script but I need to know how to change it.

 

I recently mirrored www.skins.be and got this php script to filter out the best resolution of each photo, but since the site is 30GB and the output of the filter is 5.6GB I would like to save room by having the filter create symlinks instead of copying the files. I am running Linux and run the php with php5-cli

 

 

#!/usr/bin/php

<?php # requires PHP5+,cli

$sPath = 'Downloaded/wallpapers.skins.be/';
$sNewPath = 'Sorted/';

function println( $sString ) { print $sString . "\r\n"; }
function printfln( $sString /* [, args] */ ) { $aArgv = func_get_args(); array_shift( $aArgv ); call_user_func_array('printf', array_merge( (array) $sString, $aArgv ) ); }

try
{
$aItems = array();

# Try to find the files and see which file is the biggest

$oObject = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $sPath ), RecursiveIteratorIterator::SELF_FIRST );
foreach( $oObject as $sName => $oItem )
{
	preg_match( '/([a-z-]*)-([^x]+)x([^-]+)-([^.]+).jpg/i' , $oItem -> getFilename( ), $aMatches );
	if( count( $aMatches ) > 0 )
	{
		list( $sFileName, $sCelebName, $iWidth, $iHeight, $iImageId ) = $aMatches;

		if( isset( $aItems[ $sCelebName ][ $iImageId ] ) )
			if( $aItems[ $sCelebName ][ $iImageId ][ 0 ] > $iWidth || $aItems[ $sCelebName ][ $iImageId ][ 1 ] > $iHeight )
				continue;

		$aItems[ $sCelebName ][ $iImageId ] = array( $iWidth, $iHeight, $sFileName );
	}
}

# Now that we have the list, try to sort them, in a new directory

foreach( $aItems AS $sCelebName => $aSubItems )
{
	foreach( $aSubItems AS $iId => $aData )
	{
		$sDirectory = $sNewPath . '/' . $sCelebName;
		if( ! file_exists( $sDirectory ) )
			mkdir( $sDirectory, 0700, true );

		copy( $sPath . '/' . $sCelebName . '/' . $aData[ 2 ], $sDirectory . '/' . $aData[ 2 ] );

		println( $aData[ 2 ] );
	}
}

/*


*/

// print_r( $aItems );
}
catch( Exception $e )
{

}

?>

All my attempts to modify this line with that exec line, (which I assume is the one I need to), end up not working and giving errors. I assume since that seems to just run the command in a console but my arguments don't work in the ln command.

 

        copy( $sPath . '/' . $sCelebName . '/' . $aData[ 2 ], $sDirectory . '/' . $aData[ 2 ] );

Ok, thanks I finally got it by reading more of that manual. Instead of the exec command I changed "copy" to

"symlink", which worked except I got over 22,000 broken symlinks. In the end I changed the paths to full paths which only works with my username and file locations but that is fine as it works. I no longer waste 5.6GB just 3.7MB for the 22,000 symlinks and directories.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.