darkshadow Posted February 1, 2009 Share Posted February 1, 2009 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 ) { } ?> Link to comment https://forums.phpfreaks.com/topic/143346-solved-how-do-i-create-symlinks-instead-of-copying-a-file/ Share on other sites More sharing options...
corbin Posted February 1, 2009 Share Posted February 1, 2009 http://php.net/symlink or: exec('ln -s "/path/to/target" "/path/to/link"'); (The quotes are only necessary if there is a space in the path.) Link to comment https://forums.phpfreaks.com/topic/143346-solved-how-do-i-create-symlinks-instead-of-copying-a-file/#findComment-751830 Share on other sites More sharing options...
darkshadow Posted February 1, 2009 Author Share Posted February 1, 2009 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 ] ); Link to comment https://forums.phpfreaks.com/topic/143346-solved-how-do-i-create-symlinks-instead-of-copying-a-file/#findComment-751841 Share on other sites More sharing options...
darkshadow Posted February 1, 2009 Author Share Posted February 1, 2009 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. Link to comment https://forums.phpfreaks.com/topic/143346-solved-how-do-i-create-symlinks-instead-of-copying-a-file/#findComment-751846 Share on other sites More sharing options...
corbin Posted February 1, 2009 Share Posted February 1, 2009 Link to comment https://forums.phpfreaks.com/topic/143346-solved-how-do-i-create-symlinks-instead-of-copying-a-file/#findComment-751849 Share on other sites More sharing options...
darkshadow Posted February 1, 2009 Author Share Posted February 1, 2009 Now how do I find the edit button on this forum to change the topic name to include [sOLVED] I must be blind. Link to comment https://forums.phpfreaks.com/topic/143346-solved-how-do-i-create-symlinks-instead-of-copying-a-file/#findComment-751851 Share on other sites More sharing options...
trq Posted February 1, 2009 Share Posted February 1, 2009 Now how do I find the edit button on this forum to change the topic name to include [sOLVED] I must be blind. The solved button is in the bottom left of the thread. I'll get it for you this time. Link to comment https://forums.phpfreaks.com/topic/143346-solved-how-do-i-create-symlinks-instead-of-copying-a-file/#findComment-751854 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.