Skip to main content
Inspiring
October 6, 2011
Question

File upload restrictions

  • October 6, 2011
  • 1 reply
  • 689 views

I am using the following script to upload photos to server and store details in a database

I'm a bit stuck as how to add restrictions such as size and type to this script?

Any help would be appreciated.

Tom

<?php

//this is the directory where the images images will be saved

$target = "images/";

$target = $target . basename( $_FILES['photo']['name']);

// this gets all the other info from the form

$name=$_POST['name'];

$email=$_POST['email'];

$phone=$_POST['phone'];

$pic=($_FILES['photo']['name']);

// Connects to your Database

mysql_connect("host", "user", "pass") or die(mysql_error()) ;

mysql_select_db("DB") or die(mysql_error()) ;

//Writes the information to the database

mysql_query("INSERT INTO employees (name,email,phone,photo)

VALUES ('$name', '$email', '$phone', '$pic')") ;

//Writes the photo to the server

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))

{

          $note['success'] = 'The file '. basename( $_FILES['photo']['name']). ' has been uploaded, and your information has been added to the directory';

}

else {

          //Give an error if it's not

          $note['error'] = 'Sorry, there was a problem uploading your file.';

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Untitled Document</title>

</head>

<body><?php

if (isset($note)) {

          echo '<ul>';

          foreach ($note as $alert) {

                    echo "<li class='warning'>$alert</li>/n";

          }

          echo '<ul>';

}

?>

</body>

</html>

This topic has been closed for replies.

1 reply

Rob Hecker2
Legend
October 7, 2011

Here is an example:

$width=getimagesize($uploadfile);

$widthx=$width[0];

$heightx=$width[1];

if (($widthx > 110) | (heightx > 145)){

$message = "The image size was too large and was not submitted. Maximum image size is 110 pixels wide by 145 pixels high";

} else {

Inspiring
October 7, 2011

thanks for that.

I need to know more specifically about size as in bytes. I also need to be able to allow client to upload up to 2GB if possible. Any ideas?

Cheers

T

Rob Hecker2
Legend
October 7, 2011

Then you use the filesize function, as follows

$size= filesize($uploadfile);