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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

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

Post 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>
Last edited by Sindarin on Sat Aug 30, 2008 4:04 am, edited 2 times in total.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

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

Post by ghurtado »

User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

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

Post 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?
Last edited by Sindarin on Fri Aug 22, 2008 9:51 am, edited 1 time in total.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

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

Post by ghurtado »

Code: Select all

if(strlen( basename( ( $_FILES['userfile']['name']  )  )  ) > 30)  ...
You said "download" but I assume you meant "upload".
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

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

Post 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?
User avatar
Sindarin
Forum Regular
Posts: 521
Joined: Tue Sep 25, 2007 8:36 am
Location: Greece

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

Post 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.
Last edited by Sindarin on Fri Aug 22, 2008 10:03 am, edited 1 time in total.
User avatar
ghurtado
Forum Contributor
Posts: 334
Joined: Wed Jul 23, 2008 12:19 pm

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

Post by ghurtado »

You could use the text-overflow CSS property, like gmail does:

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