Hello, for a while now I have been studying file uploads. I have 2 scripts, 1 creates a folder for a user and store the image in it. The other resizes the image and stores it inside another folder. What I would like to do is combine both scripts in one. Image uploaded and resized and put in a folder created on upload. I'm having trouble figuring this out. Here are both scripts.

<?php
//define constant for maximum upload size
define ('MAX_FILE_SIZE',9000000);
if(array_key_exists('upload', $_POST)) {
	//define constant for upload folder
	define('UPLOAD_DIR','C:/xampp/htdocs/dck_site/images/');
	define('MAX_WIDTH',300);
    define('MAX_HEIGHT',300);
	//replace spaces with underscores and assign to a var
	$file = str_replace('','_',$_FILES['image']['name']);
	//converts size to kb
	$max = number_format(MAX_FILE_SIZE/1024, 1). 'kb';
	//create array of permitted mimi types
	$permitted = array('image/gif','image/jpeg','image/pjpeg','image/png');
	//begin by assumong the file is unacceptable
	$sizeOK = false;
	$typeOK = false;
	//check that file is in permittes size
	if ($_FILES['image']['size'] >0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
		$sizeOK=true;
	}
	//check that file is of permitted mimi type
	foreach ($permitted as $type) {
		if ($type == $_FILES['image']['type']) {
			$typeOK = true;
			break;
		}
	}
	if($sizeOK && $typeOK) {
		switch($_FILES['image']['error']) {
			case 0:
			include('includes/create_thumb-final.php');
			break;
	//$username would normally come from a session var
	 $username = 'daveko';
	//if subfolder doesn't exist, create one
	if (!is_dir(UPLOAD_DIR.$username)) {
		mkdir(UPLOAD_DIR.$username);
	}
	//chck if file same name has been uploaded
	if (!file_exists(UPLOAD_DIR.$username.'/'.$file)) {
	//move file to upload folder and rename it
	$success = move_uploaded_file($_FILES['image']['tmp_name'],
		UPLOAD_DIR.$username.'/'.$file);
	}
	if ($success) {
		$result = "$file uploaded successfully";
	}
	else {
		$result = "Error uploading $file";
	}
	break;
	case 3:
	$result = "error uploading $file";
		default:
		$result = "System error uploading $file. Contact webmaster";
	}
}
elseif ($_FILES['image']['error'] == 4) {
	$result = 'No file selected';
}
else {
	$result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types are gif,jpeg, and png";
}
}
?>

Resize script:

<?php
//deifne contants
define('THUMBS_DIR', 'C:/xampp/htdocs/dck_site/images/');
define('MAX_WIDTH',300);
define('MAX_HEIGHT',300);
//process the uploaded image
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
	$original = $_FILES['image']['tmp_name'];
//get image name and build full path name

/*if (!empty($_POST['pix'])) {
	$original = SOURCE_DIR.$_POST['pix'];
}
else {
	$original = NULL;
}
//abandon processing if no image selected
if (!$original) {
	$result = 'No image selected';
}
//otherwise resize the image
else { */
	//begin by getting details of the original
	list($width, $height, $type) = getimagesize($original);
	//calculate scalling ratio
	if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
		$ratio =1;
	}
	elseif ($width > $height) {
		$ratio = MAX_WIDTH/$width;
	}
	else {
		$ratio = MAX_HEIGHT/$height;
	}
	//strip off extention from file name
	$imagetypes = array('/\.gif$/','/\.jpg$/','/\.jpeg$/','/\.png$/');
	$name = preg_replace($imagetypes, '', basename($_FILES['image']['name']));
	//create folder
	//
	//move temp file to upload folder
	$moved = @ move_uploaded_file($original,
		UPLOAD_DIR.$_FILES['image']['name']);
	if ($moved) {
		$result = $_FILES['image']['name'].'successfully uploaded; ';
		$original = UPLOAD_DIR.$_FILES['image']['name'];
	}
	else
	{
		$result = 'Problem uploading '.$_FILES['image']['name'].';';
	}
	//create an image resource for the original
	switch($type) {
		case 1:
		$source = @ imagecreatefromgif($original);
		if (!$source) {
			$result = 'Can not process GIF files, try JPEG or PNG';
		}
		break;
		case 2:
		$source = imagecreatefromjpeg($original);
		break;
		case 3:
		$source = imagecreatefrompng($original);
		break;
		default:
		$source = NULL;
		$result = 'Can not id file type';
	}
	//make sure image resource is ok
	if(!$source) {
		$result = 'Problem copying original';
	}
	else {
		//calculate the demensions of the thumbnail
		$thumb_width = round($width * $ratio);
		$thumb_height = round($height * $ratio);
		//crate image resource for image thumbnail
		$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		//create the resized copy
		imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
		//save the resized copy
		switch($type) {
			case 1:
	      if (function_exists('imagegif')) {
	        $success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
	        $thumb_name = $name.'_thb.gif';
		    }
	      else {
	        $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50);
		    $thumb_name = $name.'_thb.jpg';
		    }
	      break;
	    case 2:
	      $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100);
	      $thumb_name = $name.'_thb.jpg';
	      break;
	    case 3:
	      $success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
	      $thumb_name = $name.'_thb.png';
	    }
		if ($success) {
		  $result = "$thumb_name created";
		  }
		else {
		  $result = 'Problem creating thumbnail';
		  }
	  // remove the image resources from memory
	  imagedestroy($source);
      imagedestroy($thumb);
	  }
	}
		
?>

Thanks

It may be simpler to run them serially rather than mash them together. Use a header command at the end of execution of the first one (after line 47) to redirect to the second one.

Thanks, but I really could not get that one to work. Im definitely open to other suggestions though.

Any suggestions would be awesome. Here is the code I have so far. Im closer to solving problem but I still cannot get resized image in created folder:

<?php
define ('MAX_FILE_SIZE',9000000);
if(array_key_exists('upload', $_POST)) {
//deifne contants
define('THUMBS_DIR', 'C:/xampp/htdocs/dck_site/images/');
define('MAX_WIDTH',300);
define('MAX_HEIGHT',300);
//replace spaces with underscores and assign to a var
	$file = str_replace('','_',$_FILES['image']['name']);
	//converts size to kb
	$max = number_format(MAX_FILE_SIZE/1024, 1). 'kb';
//process the uploaded image
$permitted = array('image/gif','image/jpeg','image/pjpeg','image/png');
	//begin by assumong the file is unacceptable
	$sizeOK = false;
	$typeOK = false;
	//check that file is in permittes size
	if ($_FILES['image']['size'] >0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
		$sizeOK=true;
	}
	//check that file is of permitted mimi type
	foreach ($permitted as $type) {
		if ($type == $_FILES['image']['type']) {
			$typeOK = true;
			break;
		}
	}
	if($sizeOK && $typeOK) {
		switch($_FILES['image']['error']) {
			case 0:
			//include('includes/create_thumb-final.php');
			//break;
	//$username would normally come from a session var
	 $username = 'daveko';
	//if subfolder doesn't exist, create one
	if (!is_dir(TUMBS_DIR.$username)) {
		mkdir(THUMBS_DIR.$username);
	}
	//chck if file same name has been uploaded
	if (!file_exists(THUMBS_DIR.$username.'/'.$file)) {
	//move file to upload folder and rename it
	$success = move_uploaded_file($_FILES['image']['tmp_name'],
		THUMBS_DIR.$username.'/'.$file);
	}
	if ($success) {
		$result = "$file uploaded successfully";
	}
if (is_uploaded_file($_FILES['image']['tmp_name'])) {
	$original = $_FILES['image']['tmp_name'];
//get image name and build full path name

/*if (!empty($_POST['pix'])) {
	$original = SOURCE_DIR.$_POST['pix'];
}
else {
	$original = NULL;
}
//abandon processing if no image selected
if (!$original) {
	$result = 'No image selected';
}
//otherwise resize the image
else { */
	//begin by getting details of the original
	//create folder
	list($width, $height, $type) = getimagesize($original);
	//calculate scalling ratio
	if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
		$ratio =1;
	}
	elseif ($width > $height) {
		$ratio = MAX_WIDTH/$width;
	}
	else {
		$ratio = MAX_HEIGHT/$height;
	}
	//strip off extention from file name
	$imagetypes = array('/\.gif$/','/\.jpg$/','/\.jpeg$/','/\.png$/');
	$name = preg_replace($imagetypes, '', basename($_FILES['image']['name']));
	//create folder
	//
	//move temp file to upload folder
	$moved = @ move_uploaded_file($original,
		THUMBS_DIR.$_FILES['image']['name']);
	if ($moved) {
		$result = $_FILES['image']['name'].'successfully uploaded; ';
		$original = THUMBS_DIR.$_FILES['image']['name'];
	}
	else
	{
		$result = 'Problem uploading '.$_FILES['image']['name'].';';
	}
	//create an image resource for the original
	switch($type) {
		case 1:
		$source = @ imagecreatefromgif($original);
		if (!$source) {
			$result = 'Can not process GIF files, try JPEG or PNG';
		}
		break;
		case 2:
		$source = imagecreatefromjpeg($original);
		break;
		case 3:
		$source = imagecreatefrompng($original);
		break;
		default:
		$source = NULL;
		$result = 'Can not id file type';
	}
	//make sure image resource is ok
	if(!$source) {
		$result = 'Problem copying original';
	}
	else {
		//calculate the demensions of the thumbnail
		$thumb_width = round($width * $ratio);
		$thumb_height = round($height * $ratio);
		//crate image resource for image thumbnail
		$thumb = imagecreatetruecolor($thumb_width, $thumb_height);
		//create the resized copy
		imagecopyresampled($thumb, $source, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);
		//save the resized copy
		switch($type) {
			case 1:
	      if (function_exists('imagegif')) {
	        $success = imagegif($thumb, THUMBS_DIR.$name.'_thb.gif');
	        $thumb_name = $name.'_thb.gif';
		    }
	      else {
	        $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 50);
		    $thumb_name = $name.'_thb.jpg';
		    }
	      break;
	    case 2:
	      $success = imagejpeg($thumb, THUMBS_DIR.$name.'_thb.jpg', 100);
	      $thumb_name = $name.'_thb.jpg';
	      break;
	    case 3:
	      $success = imagepng($thumb, THUMBS_DIR.$name.'_thb.png');
	      $thumb_name = $name.'_thb.png';
	    }
		if ($success) {
		  $result = "$thumb_name created";
		  }
		else {
		  $result = 'Problem creating thumbnail';
		  }
	  // remove the image resources from memory
	  imagedestroy($source);
      imagedestroy($thumb);
	  }
	}
}
		}
	}
?>
<body>
<form action="" method="post" enctype="multipart/form-data"
name="uploadImage" id="uploadImage">
<p>
<label for="image">Upload Image:</label>
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>"/>
<input type="file" name="image" id="image"/>
</p>
<p>
<input type="submit" name="upload" id="upload" value="Upload" />
</p>
</form>
<?php 
echo $success;
//if form is submitted, display results
if (isset($result)) {
	echo "<p><strong>$result</strong></p>";
}
?>
</body>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.