Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

File upload restrictions

Explorer ,
Oct 06, 2011 Oct 06, 2011

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>

TOPICS
Server side applications
600
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 07, 2011 Oct 07, 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 {

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 07, 2011 Oct 07, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Oct 07, 2011 Oct 07, 2011
LATEST

Then you use the filesize function, as follows

$size= filesize($uploadfile);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines