Jump to content

File upload


andyd34

Recommended Posts

I am trying to create a file upload script but things arnt really going according to plan.

 

The idea is to upload 1 file into 2 directories and resize the, here is the code i am using

 

$img_dir = $_SERVER['DOCUMENT_ROOT'] . "/images/uploads/";   // Path To Images Directory 
$tmb_dir = $_SERVER['DOCUMENT_ROOT'] . "/images/thumbs/";   // Path To Thumbnails Directory 
$set_large_width = "800";   // Maximum Width For large Images 
$set_thumb_width = "100";   // Maximum Width For Thumbnail Images 

if($_POST['submit'])
{
	$file = $_FILES['imagefile']['name'];
	$file_tpm = $_FILES['imagefile']['tmp_name'];
	$type = $_FILES['imagefile']['type'];

	$upload_img = "$img_dir" . $file;
	$upload_tmb = "$tmb_dir" . $file;


	move_uploaded_file($file_tpm, $upload_img);



	list($width, $height, $type) = getimagesize($upload_img);

	$resize_lrg = $set_large_width / $width; 
	$new_img_width = $set_large_width;  
	$new_img_height = $height * $resize_lrg;

	$resize_tmb = $set_thumb_width / $width; 
	$new_thumb_width = $set_thumb_width;  
	$new_thumb_height = $height * $resize_tmb;



	if($type=='1' || $type=='2' || $type=='3')
		{
			// create gif
			if($type=='1')
				{
					$new_image = imagecreatefromgif($upload_img);
				}
			// create jpg
			if($type=='2')
				{
					$new_image = imagecreatefromjpeg($upload_img);
				}
			// create png
			if($type=='3')
				{
					$new_image = imagecreatefrompng($upload_img);
				}

		}

	if($new_image && $new_thumb)
		{
			imagecopyresized($new_image, $new_img, 0, 0, 0, 0, $new_img_width, $new_img_height, $width, $height);
			imagecopyresized($new_image, $new_tmb, 0, 0, 0, 0, $new_thumb_width, $new_thumb_height, $width, $height);

			if($type=='1')
				{
					imagegif($new_img, $upload_img);
					imagegif($new_tmb, $upload_tmb);
				}

			if($type=='1')
				{
					imagejpeg($new_img, $upload_img);
					imagejpeg($new_tmb, $upload_tmb);
				}

			if($type=='1')
				{
					imagepng($new_img, $upload_img);
					imagepng($new_tmb, $upload_tmb);
				}

			imagedestroy($new_img);
			imagedestroy($new_image);
			imagedestroy($new_thumb);
		}

}

 

Their is only 1 file being created in the uploads directory and when you try to open it its an unsupported file format!!! Does anyone know where i am going wrong

Link to comment
https://forums.phpfreaks.com/topic/192284-file-upload/
Share on other sites

Sorry forgot to include my files array but basically this is what it does

 

if($type=='image/gif' || $type=='image/pjpeg' || $type=='image/jpeg' || $type=='image/png')
		{
			// create gif
			if($type=='image/gif')
				{
					$new_image = imagecreatefromgif($upload_img);
				}
			// create jpg
			if($type=='image/pjpeg' || $type=='image/jpeg')
				{
					$new_image = imagecreatefromjpeg($upload_img);
				}
			// create png
			if($type=='image/png')
				{
					$new_image = imagecreatefrompng($upload_img);
				}

		}

Link to comment
https://forums.phpfreaks.com/topic/192284-file-upload/#findComment-1013361
Share on other sites

even with just the basic upload script

 


	$upload_img = "$img_dir" . $_FILES['imagefile']['name'];
	$upload_tmb = "$tmb_dir" . $_FILES['imagefile']['name'];


	move_uploaded_file($_FILES['imagefile']['tmp_name'], $upload_img);

 

It still gives the error when you try and open the image

Can't determine file type
Link to comment
https://forums.phpfreaks.com/topic/192284-file-upload/#findComment-1013367
Share on other sites

sorted

 

<?php 

$img_dir = $_SERVER['DOCUMENT_ROOT'] . "/images/uploads/";   // Path To Images Directory 
$tmb_dir = $_SERVER['DOCUMENT_ROOT'] . "/images/thumbs/";   // Path To Thumbnails Directory 
$set_large_width = "800";
$set_large_height = "600";   // Maximum Width For large Images 
$set_thumb_width = "100";    // Maximum Width For large Images 
$set_thumb_height = "75";   // Maximum Width For Thumbnail Images 

if($_POST['submit'])
{
	$file_name = $_FILES['imagefile']['name'];
	$file_tpm = $_FILES['imagefile']['tmp_name'];
	$type = $_FILES['imagefile']['type'];

	$upload_img = "$img_dir" . $file_name;
	$upload_tmb = "$tmb_dir" . $file_name;

	copy($file_tpm, $upload_img);

	list($width, $height) = getimagesize($upload_img);

	if ($height > $width) 
		{ 
			$resize_lrg = $set_large_width / $height; 
			$new_img_height = $set_large_height; 
			$new_img_width = $width * $resize_lrg; 

			$resize_tmb = $set_thumb_width / $height; 
			$new_thumb_height = $set_thumb_height; 
			$new_thumb_width = $width * $resize_tmb; 
		} 
		else 
		{  
			$resize_lrg = $set_large_width / $width;  
			$new_img_width = $set_large_width;  
			$new_img_height = $height * $resize_lrg; 

			$resize_tmb = $set_thumb_width / $width;  
			$new_thumb_width = $set_thumb_width;  
			$new_thumb_height = $height * $resize_tmb; 
		} 



	$new_img = imagecreatetruecolor($new_img_width, $new_img_height);
	$new_tmb = imagecreatetruecolor($new_thumb_width, $new_thumb_height);

	if($type=='image/gif' || $type=='image/pjpeg' || $type=='image/jpeg' || $type=='image/png')
		{
			// create gif
			if($type=='image/gif')
				{
					$new_image = imagecreatefromgif($upload_img);
					echo 'gif';
				}
			// create jpg
			if($type=='image/pjpeg' || $type=='image/jpeg')
				{
					$new_image = imagecreatefromjpeg($upload_img);
					echo 'jpeg';
				}
			// create png
			if($type=='image/png')
				{
					$new_image = imagecreatefrompng($upload_img);
					echo 'png';
				}

		}

	if($new_image)
		{

			imagecopyresampled($new_img, $new_image, 0, 0, 0, 0, $new_img_width, $new_img_height, $width, $height);
			imagecopyresampled($new_tmb, $new_image, 0, 0, 0, 0, $new_thumb_width, $new_thumb_height, $width, $height);

			if($type=='image/gif')
				{
					imagegif($new_img, $upload_img);
					imagegif($new_tmb, $upload_tmb);
				}

			if($type=='image/pjpeg' || $type=='image/jpeg')
				{
					imagejpeg($new_img, $upload_img);
					imagejpeg($new_tmb, $upload_tmb);
				}

			if($type=='image/png')
				{
					imagepng($new_img, $upload_img);
					imagepng($new_tmb, $upload_tmb);
				}

			imagedestroy($new_img);
			imagedestroy($new_image);
			imagedestroy($new_tmb);
		}

}

?> 
  <form method="post" action="upload-size.php" enctype="multipart/form-data"> 
   File:<br /> 
  <input type="file" name="imagefile"> 
  <br /><br /> 
  <input name="submit" type="submit" value="Sumbit">  <input type="reset" value="Clear" class="form"> 
  </form> 

Link to comment
https://forums.phpfreaks.com/topic/192284-file-upload/#findComment-1013412
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.