getting an image of a random file

Discussion in 'PHP' started by zodiac, May 1, 2007.

  1. #1
    <?php
    $filedir = '/home/me/public_html/domain/swf/'; 
    $newfile = '/home/me/public_html/domain/swffolder/something.swf'; 
    $imgdir = '/home/me/public_html/domain/swf/';
    $newimg = '/home/me/public_html/domain/imgfolder/something.swf'; 
    function getRandomFile($start_dir)
            {
            /*
            returns the name of one random file from within a directory
            */
            
            chdir($start_dir);
            $dir = opendir('.');
            while (($myfile = readdir($dir)) !==false)
                    {
                    if ($myfile != '.' && $myfile != '..' && is_file($myfile) && $myfile != 'resource.frk')
                            {
                            $files[] = $myfile;
                            }
                    }
            closedir($dir);
            chdir('../');
            srand ((float) microtime() * 10000000);
            $file = array_rand($files);
            return $files[$file];
            }
    		
    $filesize = 80000000;
    while ($filesize > 400000) { //change this for file size
    
    	$file = getRandomFile($filedir);
    	$randfile = $filedir . $file ;
    	$filesize = filesize($randfile);
    	echo $randfile . ': ' . $filesize . ' bytes' . '<br>';
    	
    }
    	
    if (!copy($randfile, $newfile)) {
       echo "failed to copy $file...\n";
    }
    
    // outputs e.g.  somefile.txt: 1024 bytes
    echo 'FINAL:' . $randfile . ': ' . $filesize . ' bytes';
    ?> 
    
    PHP:
    i added the $imgdir and $newimg lines.
    what this script does is,it copys a random swf file out of the swf folder and puts it in the swffolder.also renaming it.
    i also like it to grab the image file,which is named the same,but may be PNG or JPG and do the same like the swf file.
     
    zodiac, May 1, 2007 IP