Page 1 of 1

[solved] Upload | invalid image width/height/filename check?

Posted: Thu Aug 21, 2008 8:20 am
by Sindarin
In my upload form, I can check whether an image is of the file types I allow or has the max allowed filesize, but I don't know how I can get the width/height of the image and if it is over 400x300 to disallow upload.
This is my code:

Code: Select all

<?
//load functions
require_once('inc/functions.php');
 
switch($_GET['file']){
case 'upload':
 
// UPLOAD FILE
 
//Maximum file size.
$MAX_SIZE = 1000000;
 
//valid extensions
$FILE_EXTS  = array('.jpg','.png','.gif'); 
 
//Get server paths
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$url_this =  "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
 
$upload_dir = "files/uploads/images/";
$upload_url = $url_dir."/files/";
 
//get file name
$file_name = $_FILES['userfile']['name'];
$temp_name = $_FILES['userfile']['tmp_name'];
$file_size = $_FILES['userfile']['size'];
$file_path = $upload_dir.$file_name;
$file_ext = strtolower(substr($file_name,strrpos($file_name,".")));
 
//replace illegal characters
$file_name = str_replace("\\","",$file_name);
$file_name = str_replace("'","",$file_name);
 
//File Size Check
if( $file_size > $MAX_SIZE) 
 {
echo '<center>Error: The file size is over 1 Mb</center>';
}
else
{
 
//File type Check
     if (!in_array($file_ext, $FILE_EXTS))
  {
echo '<center>Error: Invalid File Type Specified, Allowed extensions are .jpg, .gif and .png</center>';
}
else
{
//connect to database
require_once('inc/database-connect.php');
 
//make upload entry
$db_insert="INSERT INTO images (image_id,image_src,image_alt)
VALUES
('i++','$file_name','image')";
 
if (!mysql_query($db_insert,$db_connection))
  {
  die("<font color='red'><img src='files/images/layout/cms-error.png' />Error</font>: " . mysql_error());
  }
 
//fileupload
$result  =  move_uploaded_file($temp_name, $file_path);
if ($result=1)
{
echo "<center>$file_name was uploaded successfully!</center>";
}
else
{
echo "<center>Error: File could not be uploaded!</center>";
exit;
}
}
}
  break;
  default:
      echo "<center>Select file to upload</center>";
      }
?>
 
<html>
<head>
<title>FILE upload</title>
<link rel=stylesheet href=style.css>
</head>
<body>
<br>
<center>
   <font color=red></font>
   <br>
   <form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post" action="upload.php?file=upload">
     Upload File <input type="file" id="userfile" name="userfile">
     <input type="submit" name="upload" value="Upload">
   </form>
 </small>
</center>

Re: File upload | Restrict upon invalid image width/height?

Posted: Thu Aug 21, 2008 8:27 am
by ghurtado

Re: File upload | invalid image width/height and filename check?

Posted: Fri Aug 22, 2008 7:19 am
by Sindarin
Thanks, I managed to get it working.

Now I want to check how many characters are in the filename, and if over 30, restrict the upload,
how do I do this?

Re: File upload | invalid image width/height and filename check?

Posted: Fri Aug 22, 2008 8:07 am
by ghurtado

Code: Select all

if(strlen( basename( ( $_FILES['userfile']['name']  )  )  ) > 30)  ...
You said "download" but I assume you meant "upload".

Re: File upload | invalid image width/height and filename check?

Posted: Fri Aug 22, 2008 8:35 am
by onion2k
Sindarin wrote:Now I want to check how many characters are in the filename, and if over 30, restrict the download
What's wrong with having long filenames?

Re: File upload | invalid image width/height and filename check?

Posted: Fri Aug 22, 2008 9:53 am
by Sindarin
Messes up with the image browser layout I made, and trimming doesn't seem to work. I was gonna rename them using md5 filenames but the client needs to have those files readable. (I know I could have descriptions in the database :| )

I also believe a file like myimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimagemyimage.jpg, could cause problems to the code.
You said "download" but I assume you meant "upload".
Yeah, thanks.

Re: File upload | invalid image width/height and filename check?

Posted: Fri Aug 22, 2008 9:59 am
by ghurtado
You could use the text-overflow CSS property, like gmail does:

http://www.blakems.com/archives/000077.html