Jump to content

need help with image uploading restriction


iNko

Recommended Posts

hi once again, i got a form that uploads an image, checks  formats and all that.. i need to add the filesize restriction.

something like this:

define ("max_size","20"); //<- 20kb size
$size=filesize($_FILES['image']['tmp_name'])

if ($size > max_size*1024){
echo "too big filesize";
} else {
  //......
}

 

how do i put that code, into this code? :

<?php

$allowedfiletypes = array("jpeg","jpg","gif","png");
$uploadfolder = "uploads/" ;
$thumbnailheight = 100; //in pixels
$thumbnailfolder = $uploadfolder."thumbs/" ;


$action = $_POST['action'];
if ($action == "upload") {
    //echo "<p>Uploading image... " ;

    if(empty($_FILES['uploadimage']['name'])){
        echo '<script type="text/javascript"> {alert("Pasirinkite faila");} </script>';
    } else {
        $uploadfilename = $_FILES['uploadimage']['name'];
        $fileext = strtolower(substr($uploadfilename,strrpos($uploadfilename,".")+1));

        if (!in_array($fileext,$allowedfiletypes)) { 
	echo '<script type="text/javascript"> {alert("Blogas failo tipas");} </script>';
	} else {
            $fulluploadfilename = $uploadfolder.$uploadfilename ;


            if (move_uploaded_file($_FILES['uploadimage']['tmp_name'], $fulluploadfilename)) {
           echo '<script type="text/javascript"> {alert("Failas irasytas");} </script>';
            $im = imagecreatefromjpeg($fulluploadfilename);

                if (!$im) {
				 echo '<script type="text/javascript"> {alert("Nepavyko sugeneruoti thumbnail");} </script>'; 
			} else {
                    $imw = imagesx($im); // uploaded image width
                    $imh = imagesy($im); // uploaded image height
                    $nh = $thumbnailheight; // thumbnail height
                    $nw = round(($nh / $imh) * $imw); //thumnail width
                    $newim = imagecreatetruecolor ($nw, $nh);
                    imagecopyresampled ($newim,$im, 0, 0, 0, 0, $nw, $nh, $imw, $imh) ;
                    $thumbfilename = $thumbnailfolder.$uploadfilename ;
                    imagejpeg($newim, $thumbfilename) or die('<script type="text/javascript"> {alert("Nepavyko issaugoti thumbnail");} </script>');
                }
            } else { 
		echo '<script type="text/javascript"> {alert("Nepavyko issaugoti failo");} </script>'; 
		}
        }
    }

function watermark($original_image,$original_watermark,$destination="")
	{
		$image=imagecreatefromjpeg($original_image);
		list($imagewidth,$imageheight)=getimagesize($original_image);

		$watermark 	= 	imagecreatefrompng($original_watermark); 			
		list($watermarkwidth,$watermarkheight)=getimagesize($original_watermark);

		if($watermarkwidth>$imagewidth || $watermarkheight>$imageheight)
		{
			$water_resize_factor = $imagewidth / $watermarkwidth;
			$new_watermarkwidth  = $watermarkwidth * $water_resize_factor;
			$new_watermarkheight = $watermarkheight * $water_resize_factor;

			$new_watermark = imagecreatetruecolor($new_watermarkwidth , $new_watermarkheight);

			imagealphablending($new_watermark , false);
			imagecopyresampled($new_watermark , $watermark, 0, 0, 0, 0, $new_watermarkwidth, $new_watermarkheight, $watermarkwidth, $watermarkheight);

			$watermarkwidth  = $new_watermarkwidth; 
			$watermarkheight = $new_watermarkheight; 
			$watermark       = $new_watermark;
		}
		$startwidth 	= 	($imagewidth 	- 	$watermarkwidth)  / 2; 
		$startheight 	= 	($imageheight 	- 	$watermarkheight) / 2;

		imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); 
		if(!empty($destination))
			imagejpeg($image,$destination);
		else 
			imagejpeg($image);
	}	


$original_directory = "uploads/thumbs/";
$watermarked_images = "uploads/thumbs/watermarkedthumbs/";

if ($handle = opendir($original_directory)) 
{
    while (false !== ($file = readdir($handle))) 
    {
    	/*
    		exif_imagetype checks if our file is
    		a .jpg file. See manual for more info
    	*/
    	if(!is_file($original_directory.$file))
    		continue;
        if(exif_imagetype($original_directory.$file)==2)
        {
        	watermark($original_directory.$file,"watermark.png",$watermarked_images.$file);
        	     }
    }
    closedir($handle);
}
}


?>
<form name="newad" method="post" enctype="multipart/form-data"  action="">
<label for="file" id="label">Pasirinkti failą: </label>
<input type="hidden" name="action" value="upload" />
<input type="file" name="uploadimage" id="file">
<input name="Submit" type="submit" value="įkelti" id="submit">
</form>

 

i tried smth like this, but it shows some error ("Warning: filesize() [function.filesize]: stat failed for 1.jpg in")

<?php

/* $allowedfiletypes = array("jpeg","jpg","gif","png");
$uploadfolder = "uploads/" ;
$thumbnailheight = 100; //in pixels
$thumbnailfolder = $uploadfolder."thumbs/" ; */
define ("max_size","20"); 
$size=filesize($_FILES['uploadimage']['name']);

/* $action = $_POST['action'];
if ($action == "upload") {
    //echo "<p>Uploading image... " ;

    if(empty($_FILES['uploadimage']['name'])){
        echo '<script type="text/javascript"> {alert("Pasirinkite faila");} </script>';
    } else {
        $uploadfilename = $_FILES['uploadimage']['name'];
        $fileext = strtolower(substr($uploadfilename,strrpos($uploadfilename,".")+1));

        if (!in_array($fileext,$allowedfiletypes)) { 
	echo '<script type="text/javascript"> {alert("Blogas failo tipas");} </script>';
	} else {
            $fulluploadfilename = $uploadfolder.$uploadfilename ; */


		if ($size > max_size*1024){
			echo '<script type="text/javascript"> {alert("Per didelis failas");} </script>';
		} else {

           /* if (move_uploaded_file($_FILES['uploadimage']['tmp_name'], $fulluploadfilename)) {
           echo '<script type="text/javascript"> {alert("Failas irasytas");} </script>';
            $im = imagecreatefromjpeg($fulluploadfilename);

                if (!$im) {
				 echo '<script type="text/javascript"> {alert("Nepavyko sugeneruoti thumbnail");} </script>'; 
			} else {
                    $imw = imagesx($im); // uploaded image width
                    $imh = imagesy($im); // uploaded image height
                    $nh = $thumbnailheight; // thumbnail height
                    $nw = round(($nh / $imh) * $imw); //thumnail width
                    $newim = imagecreatetruecolor ($nw, $nh);
                    imagecopyresampled ($newim,$im, 0, 0, 0, 0, $nw, $nh, $imw, $imh) ;
                    $thumbfilename = $thumbnailfolder.$uploadfilename ;
                    imagejpeg($newim, $thumbfilename) or die('<script type="text/javascript"> {alert("Nepavyko issaugoti thumbnail");} </script>');
                }
            } else { 
		echo '<script type="text/javascript"> {alert("Nepavyko issaugoti failo");} </script>'; 
		}
        }
    }
}
function watermark($original_image,$original_watermark,$destination="")
	{
		$image=imagecreatefromjpeg($original_image);
		list($imagewidth,$imageheight)=getimagesize($original_image);

		$watermark 	= 	imagecreatefrompng($original_watermark); 			
		list($watermarkwidth,$watermarkheight)=getimagesize($original_watermark);

		if($watermarkwidth>$imagewidth || $watermarkheight>$imageheight)
		{
			$water_resize_factor = $imagewidth / $watermarkwidth;
			$new_watermarkwidth  = $watermarkwidth * $water_resize_factor;
			$new_watermarkheight = $watermarkheight * $water_resize_factor;

			$new_watermark = imagecreatetruecolor($new_watermarkwidth , $new_watermarkheight);

			imagealphablending($new_watermark , false);
			imagecopyresampled($new_watermark , $watermark, 0, 0, 0, 0, $new_watermarkwidth, $new_watermarkheight, $watermarkwidth, $watermarkheight);

			$watermarkwidth  = $new_watermarkwidth; 
			$watermarkheight = $new_watermarkheight; 
			$watermark       = $new_watermark;
		}
		$startwidth 	= 	($imagewidth 	- 	$watermarkwidth)  / 2; 
		$startheight 	= 	($imageheight 	- 	$watermarkheight) / 2;

		imagecopy($image, $watermark, $startwidth, $startheight, 0, 0, $watermarkwidth, $watermarkheight); 
		if(!empty($destination))
			imagejpeg($image,$destination);
		else 
			imagejpeg($image);
	}	


$original_directory = "uploads/thumbs/";
$watermarked_images = "uploads/thumbs/watermarkedthumbs/";

if ($handle = opendir($original_directory)) 
{
    while (false !== ($file = readdir($handle))) 
    {
    	/*
    		exif_imagetype checks if our file is
    		a .jpg file. See manual for more info
    	*/ /*
    	if(!is_file($original_directory.$file))
    		continue;
        if(exif_imagetype($original_directory.$file)==2)
        {
        	watermark($original_directory.$file,"watermark.png",$watermarked_images.$file);
        	     }
    }
    closedir($handle);
}
}

*/
?>

 

any ideas?

thx

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.