Location: PHPKode > scripts > ImageVine Image Hosting Script > ImageVine/Script/upload.php
<?php

/*

    File Name: upload.php
	Project: ImageVine
	Author: ScriptVine.com
	Revision: 1a

*/


// include header
include("header.php");

// include resize class
include("include/classes/class.resize.php");

// reset script vars
$is_error = 0;
$moved = 0;
$message = '';
$uploaded = false;

// check if a file has been submitted
if(isset($_POST['task']) && $_POST['task'] == "doUpload")
{
    // check if first slot is empty
	if(empty($_FILES['image']['name']['0']))
	{
	    $flashMessage->setFlash("upload_error", "Please select an image to upload.");
	    $redirect = $config['base_url'];
	}
	
    // generate new set id
	$set_id = randString(6);
	
	while(list($key, $value) = each($_FILES['image']['name']))	
	{
	    // make sure slot is not empty
		if(!empty($value))
		{
			// set upload directory
			$upload_dir = $config['upload_dir'] . $set_id . "/";
			
			// set file owner
			$owner = ($user->user_exists == 1) ? $user->user_info['id'] : 0;
	
            // get image info
	        $file_name = $_FILES['image']['name'][$key];
	        $file_tmp_name = $_FILES['image']['tmp_name'][$key];
	        $file_size = $_FILES['image']['size'][$key];
	        $file_type = strtolower($_FILES['image']['type'][$key]);
	        $get_img  = @getimagesize($file_tmp_name);
	        $path_info = pathinfo($file_name);
	
	        // check file size
	        if($file_size >= $config['upload_max_size'])
	        {
	            $is_error = 1;
				$message = $file_name . " is too big.\n";
	        }else
	
	        // check file extension
	        if(!(in_array(strtolower($path_info['extension']), $config['upload_accepted_ext'])) ||
	           !(in_array($file_type, $config['upload_accepted_mime'])))
	        {
	            $is_error = 1;
				$message = $file_name . " is an invalid extension.\n";
	        }else
	
	        // no error, continue to upload image
	        if($is_error != 1)
	        {
	            // generate new image name
		        $newname = randString(6);
		
		        // generate new delete id
		        $delete_id = randString(8);
		    
			    // check if upload dir exists and create it if not
			    if(!is_dir($upload_dir))
			    {
			        mkdir($upload_dir, 0777);
			    }
			
		        // move uploaded file to new location
		        if(move_uploaded_file($file_tmp_name, $upload_dir . $newname . "." . strtolower($path_info['extension'])))
		        {
		            $uploaded = true;
		        }
		
		        // make thumbs
		        if($uploaded)
		        {
				    // incriment moved var
					$moved = 1;
					
			        // init new class object
			        $resize = new resize($upload_dir . $newname . "." . strtolower($path_info['extension']));
			
			        // make thumb
			        $thumb_name = $newname . "_thumb" . "." . strtolower($path_info['extension']);
			        $resize->resizeImage(350, 350, "auto");
			        $resize->imageSaveQuality(60);
			        $resize->saveImage($upload_dir . $thumb_name);
				
			        // make small thumb
			        $thumb_name_small = $newname . "_thumb_small" . "." . strtolower($path_info['extension']);
			        $resize->resizeImage(120, 120, "exact");
			        $resize->imageSaveQuality(60);
			        $resize->saveImage($upload_dir . $thumb_name_small);
			
			        // is image public?
			        $public = isset($_POST['public']) ? "1" : "0";
			
			       // insert image in to database
			        mysql_query("INSERT INTO uploads (image_name,
			                                          original_name,
											          image_type,
											          image_size,
											          uploaded,
											          uploader_ip,
											          views,
											          last_access,
											          delete_code,
											          image_public,
												      owner,
												      set_id
											          ) VALUES (
											          '".$newname . "." . strtolower($path_info['extension'])."',
											          '".$file_name."',
											          '".$file_type."',
											          '".$file_size."',
											          '".time()."',
											          '".$_SERVER['REMOTE_ADDR']."',
											          '0',
											          '".time()."',
											          '".$delete_id."',
											          '".$public."',
												      '".$owner."',
												      '".$set_id."')");
                } // if moved
		    } // if error
	    } // if empty
    } // while
	
	if($moved == 1)
	{
	    if($is_error == 1){ $flashMessage->setFlash("upload_error", "Some images were not uploaded due to errors."); }
	    $redirect = $config['base_url'] . '/u-' . $set_id;
	}else{
	    $flashMessage->setFlash("upload_error", $message);
	    $redirect = $config['base_url'];
	}
	die($redirect);
		
} // if action == upload

?>
Return current item: ImageVine Image Hosting Script