Help - Search - Members - Calendar
Full Version: Storing records & uploading an image
Weborum Webmaster Forum > Web Page Design > PHP
Joe
Hey,

For a new little project of mine I want to be able to store certain things. Text and an image.

Storing the data is simple enough but what I want to do with the images is run a check to see that they are;

1. A certain dimension (150px X 150px)
2. A certain file type (gif)
3. Under a certain size (50kb maybe)

If all of these conform then I want the image name to be stored in the database and places in a folder designated especially for the images.

Say, just for example the fields would be;

Name (text)
Age (integer)
Location (text)
Image (location to the file images/filename1.gif)


I've tried looking at a script I found but don't fully understand it, I was wondering if someone could help me a little on this one smile.gif

CODE
Send this file: <input name="userfile" type="file" />


CODE

$mime_type = array(1=>"image/jpeg", 2=>"image/gif", 3=>"image/jpg", 4=>"image/pjpeg");

   if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {
      // you can change this if you move the uploaded file from the tmp folder.
      $uploaddir = '/the/directory/here/uploads/'; // change this
      $uploadfile = $uploaddir . $_FILES['userfile']['name'];
  if(array_search($_FILES['userfile']['type'],$mime_type)){
      if ($_FILES['userfile']['size'] > 100000) {
              echo 'file size too large';
              exit;
          }
   
          if (!copy($_FILES['userfile']['tmp_name'], $uploadfile)) {
              echo "Possible file upload attack! Here's some debugging info:\n";
              print_r($_FILES);
              exit;
          }
          $new_mimetype = $_FILES['userfile']['type'];
      } else {
          echo "Wrong file type uploaded.";
          exit;
      }
  } else {
      echo "No file Uploaded, or file was empty";
      exit;
  }


Thanks! smile.gif
sjthomas
I'm not sure how to do the dimension check Joe but if you just want to upload a file, make sure its an image, check its size and save it I've got this little snippet from my archive wink.gif

PHP
Form Page:
<form action="upload.php" method="post" enctype="multipart/form-data">
  <p>File Upload</p>
  <label for="file">File</label>
  <input type="file" name="file" id="file" />
  <br />
  <input type="submit" name="submit" value="Submit" />
</form>


Processing Page:
<?php
if (($_FILES["file"]["type"] == "image/gif") &&
(
$_FILES["file"]["size"] < 15000)) {
  echo
"Return Code: " . $_FILES["file"]["error"] . "<br />";
  echo
"Uploading " . $_FILES["file"]["name"];
  echo
" (" . $_FILES["file"]["type"] . ", ";
  echo
ceil($_FILES["file"]["size"] / 1024) . " Kb).<br />";

  if (
file_exists("uploads/" . $_FILES["file"]["name"])) {
    echo
$_FILES["file"]["name"] . " already exists.  ";
    echo
"Please delete the destination file and try again.";
  } else {
    
move_uploaded_file($_FILES["file"]["tmp_name"],
    
"uploads/" . $_FILES["file"]["name"]);
    echo
"File has been stored in your uploads directory.";
  }

} else
  echo
"Sorry, we only accept .GIF images under 15Kb for upload.";
?>


Basically just save the php bit as upload.php and the form will point to it. This one checks to make sure its a gif file which is less than 15Kb, it also checks that the file you are trying to upload doesn't already exist. its a little bit of modding I'm sure you can get it to do what you want. Not sure about dimensions though sad.gif
Timo
I think there is a way in the GD library to output the dimensions... Actually I know there is...
http://us2.php.net/manual/en/function.getimagesize.php

Sorry about the late reply but I wasn't here before and just noticed when SJ responded. biggrin.gif
Joe
Cheers guys smile.gif

I'll play around with both of them.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.