Location: PHPKode > projects > ZompLog > admin/upload_files.php
<?php

	if(!$_SESSION["loggedIn"]){
	echo "You are not authorized to acces this page";
	exit();
	}
  
    $image_dir = '../upload/';
	
	if($_POST['thumbnail']){
	$thumb_width = $settings['img_width'];
	}
	else
	{
	$thumb_width = $settings['img_fullwidth'];
	}
	
	$large_width = $settings['img_fullwidth'];
	
	$file_prefix = date("Ymd").'_'.rand(100,999).'_';
    
    // upload dir
    $destination = $image_dir;
        
    if(isset($_FILES))
        {
            // initialize error var for processing
            $error = array();     
            
            // acceptable files
            // if array is blank then all file types will be accepted
            $filetypes = array(
                        //'ai' => 'application/postscript',
                        //'bin' => 'application/octet-stream',
                        //'bmp' => 'image/x-ms-bmp',
                        //'css' => 'text/css',
                        'csv' => 'text/plain',
                        'doc' => 'application/msword',
                        'dot' => 'application/msword',
                        'eps' => 'application/postscript',
                        'gif' => 'image/gif',
                        'gz' => 'application/x-gzip',
                        'htm' => 'text/html',
                        'html' => 'text/html',
                        //'ico' => 'image/x-icon',
						'flv' => 'video/x-flv',
						'flv' => 'application/octet-stream',
                        'jpg' => 'image/jpeg',
                        'jpe' => 'image/jpeg',
                        'jpeg' => 'image/jpeg',
						'jpeg' => 'image/pjpeg',
                        //'js' => 'text/javascript',
                        'mov' => 'video/quicktime',
                        'mp3' => 'audio/mpeg',
                        'mp4' => 'video/mp4',                        
                        'mpeg' => 'video/mpeg',
                        'mpg' => 'video/mpeg',
                        'pdf' => 'application/pdf',
                        'png' => 'image/x-png',
                        'pot' => 'application/vnd.ms-powerpoint',
                        'pps' => 'application/vnd.ms-powerpoint',
                        'ppt' => 'application/vnd.ms-powerpoint',
                        //'qt' => 'video/quicktime',
                        'ra' => 'audio/x-pn-realaudio',
                        'ram' => 'audio/x-pn-realaudio',
                        'rtf' => 'application/rtf',
                        'swf' => 'application/x-shockwave-flash',
                        'tar' => 'application/x-tar',
                        'tgz' => 'application/x-compressed',
                        //'tif' => 'image/tiff',
                        //'tiff' => 'image/tiff',
                        'txt' => 'text/plain',
						'wma' => 'audio/x-ms-wma',
						'wmv' => 'video/x-ms-wmv',
                        'xls' => 'application/vnd.ms-excel',
                        'zip' => 'application/zip'
                    );
                    
            // function to check for accpetable file type
            function okFileType($type)
                {
				global $messages;
                    // if filetypes array is empty then let everything through
                    if(count($GLOBALS['filetypes']) < 1)
                        {
                            return true;
                        }
                    // if no match is made to a valid file types array then kick it back
					// also a hack against strange 'audio/mpeg' errors
                    elseif(!in_array($type,$GLOBALS['filetypes']) && $type != 'audio/mpeg')
                        {
                            $GLOBALS['error'][] = $type.' is not an acceptable file type. '.
                                                  $type.' has been ignored.';
							$messages[] = $type.' is not an acceptable file type. '.
                                                  $type.' has been ignored.';					  
                            return false;
                        }
                    // else - let the file through
                    else
                        {                        
                            return true;
                        }
                }
            
			
			
            // function to check and move file
            function processFile($file,$dir_name)
                {    
				global $file_prefix, $messages;
                    // set full path/name of file to be moved
                    $upload_file = $GLOBALS['destination'].'temp_'.$file_prefix.$file['name']; // here we're adding the prefix "temp_"
                    
                    if(file_exists($upload_file))
                        {
                            $GLOBALS['error'][] = $file['name'].' - Filename exists - please change your image filename';
							$messages[] = $file['name'].' - Filename exists - please change your image filename';
                            return false;
                        }
                    
                    if(!move_uploaded_file($file['tmp_name'], $upload_file)) 
                        {
                            // failed to move file
                            $GLOBALS['error'][] = 'File Upload Failed on '.$file['name'].' - Please try again';
							$messages[] = 'File Upload Failed on '.$file['name'].' - Please try again. Did you set the right permissions on "thumbs" and "upload"?';
                            return false;
                        } 
                    else 
                        {
                            // upload OK - change file permissions



							chmod($upload_file, 0777);    //This was 0755

							// this fix by davinci solves a gd memory limit issue
							if (substr(ini_get('memory_limit'),0,-1)<32) 
							{ 
								ini_set("memory_limit","32M");
									if (substr(ini_get('memory_limit'),0,-1)<32) 
									{ 
									$imageinfo=getimagesize($upload_file);
									if (($imageinfo[0]>1000) || ($imageinfo[1]>1000)) 
											{
										unlink($upload_file);
										$GLOBALS['error'][] = 'Image height or width is >1000px, and your server does not have enough memory to handle this - Please try again';
										$messages[] = 'Image height or width is >1000px, and your server does not have enough memory to handle this - Please try again';
										return false; 
										} 
									}
							}
	
                            return true;
                        }    
                }
				
				
				
				
				
			// thumbnail function - only if file is jpg
function create_thumbnail($infile,$outfile,$maxw,$maxh,$stretch = FALSE) {
	clearstatcache();
	if (!is_file($infile)) {
		trigger_error("Cannot open file: $infile",E_USER_WARNING);
		return FALSE;
	}
	if (is_file($outfile)) {
	    trigger_error("Output file already exists: $outfile",E_USER_WARNING);
		return FALSE;
	}

	$functions = array(
		'image/png' => 'ImageCreateFromPng',
		'image/jpeg' => 'ImageCreateFromJpeg',
	);

	// Add GIF support if GD was compiled with it
	if (function_exists('ImageCreateFromGif')) { $functions['image/gif'] = 'ImageCreateFromGif'; }

	$size = getimagesize($infile);

	// Check if mime type is listed above
	if (!$function = $functions[$size['mime']]) {
	    trigger_error("MIME Type unsupported: {$size['mime']}",E_USER_WARNING);
		return FALSE;
	}

	// Open source image
	if (!$source_img = $function($infile)) {
	    trigger_error("Unable to open source file: $infile",E_USER_WARNING);
		return FALSE;
	}

	$save_function = "image" . strtolower(substr(strrchr($size['mime'],'/'),1));

	// Scale dimensions
	list($neww,$newh) = scale_dimensions($size[0],$size[1],$maxw,$maxh,$stretch);

	// Create new image
	$new_img = imagecreatetruecolor($neww,$newh);

	// Copy and resize image
	imagecopyresampled($new_img,$source_img,0,0,0,0,$neww,$newh,$size[0],$size[1]);

	// Save output file
	if ($save_function == 'imagejpeg') {
	    // Change the JPEG quality here
	    if (!$save_function($new_img,$outfile,90)) {
	        trigger_error("Unable to save output image",E_USER_WARNING);
	        return FALSE;
	    }
	} else {
	    if (!$save_function($new_img,$outfile)) {
	        trigger_error("Unable to save output image",E_USER_WARNING);
	        return FALSE;
	    }
	}

	// Cleanup
	imagedestroy($source_img);
	imagedestroy($new_img);

	return TRUE;
}
// Scales dimensions
function scale_dimensions($w,$h,$maxw,$maxh,$stretch = FALSE) {
	if ((!$stretch) && (($w < $maxw) || (!$maxw)) &&
		(($h < $maxh) || (!$maxh))) return array($w,$h);
	
	// Scale Height
	if ((!$maxw) || (($h > $w) && ($maxh)) ) {
	    $newh = $maxh;
	    $neww = floor($w * $newh /$h);
	}
	// Scale width
	elseif ((!$maxh) || (($w >= $h) && ($maxw))) {
	    $neww = $maxw;
	    $newh = floor($h * $neww / $w);
	} else
	// Scale neither
	    return array($w,$h);
	
	return array($neww,$newh);
}


	


			
                
            // check to make sure files were uploaded
            $no_files = 0;
            $uploaded = array();
            foreach($_FILES as $file)
                {
                    switch($file['error'])
                        {
                            case 0:
                                // file found
                                if($file['name'] != NULL && okFileType($file['type']) != false)
                                    {
                                        // process the file
                                        if(processFile($file) == true)
										$filename = $file_prefix.$file['name'];
										$filetype = $file['type'];
										
										
										
										// if the file is an image, then create a thumbnail
										if($filetype == 'image/jpg' || $filetype == 'image/jpeg' || $filetype == 'image/pjpeg' || $filetype == 'image/x-png' || $filetype == 'image/gif'){
										// create two sizes
										create_thumbnail("../upload/temp_$filename","../thumbs/$filename",$thumb_width,0,$stretch = FALSE); 
										create_thumbnail("../upload/temp_$filename","../upload/$filename",$large_width,0,$stretch = FALSE); 
										unlink("../upload/temp_$filename"); // delete the original
										}
										
										if($filetype != 'image/jpg' || $filetype != 'image/jpeg' || $filetype != 'image/pjpeg' || $filetype != 'image/x-png' || $filetype != 'image/gif')
{
rename("../upload/temp_$filename", "../upload/$filename");
}

										// creating an array for later use
										$aBestanden[$filename] = $filetype;
										
											
                                    }
                                break;
                                
                            case (1|2):
                                // upload too large
                                $messages[] = 'File '.$file['name'] . 'too large';
                                break;
                                
                            case 4:
                                // no file uploaded
								
                                break;
                                
                            case (6|7):
                                // no temp folder or failed write - server config errors
                                $messages[] = 'Unable to write '.$file['name'] . ' to temporary folder';
                                break;
                        }
                }
                
        }
?>
Return current item: ZompLog