PDA

View Full Version : upload restrictions to only allow jpgs


davidmerson
03-28-2008, 02:10 AM
hi, can someone please help me!

i have this code to upload files which all works:


<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}

include 'config.php';
include 'opendb.php';

$errmsg = "";
if (!isset($_POST['customer_name']) || empty($_POST['customer_name'])) $errmsg .= "<p>name not entered";
if (!isset($_POST['customer_address1']) || empty($_POST['customer_address1'])) $errmsg .= "<p>address line 1 not entered";
if (!isset($_POST['customer_address2']) || empty($_POST['customer_address2'])) $errmsg .= "<p>address line 2 not entered";
if (!isset($_POST['customer_address3']) || empty($_POST['customer_address3'])) $errmsg .= "<p>address line 3 not entered";
if (!isset($_POST['customer_code']) || empty($_POST['customer_code'])) $errmsg .= "<p>postcode not entered";
if (!isset($_POST['customer_tel']) || empty($_POST['customer_tel'])) $errmsg .= "<p>telephone number not entered";
if (!isset($_POST['customer_email']) || empty($_POST['customer_email'])) $errmsg .= "<p>email address not entered";
if ($_POST['customer_options'] == "none") $errmsg .= "<p>printing option not entered";
if (!isset($_POST['customer_qty']) || empty($_POST['customer_qty'])) $errmsg .= "<p>quantity not entered";

if (!$errmsg == "") {
echo "<td>" . $errmsg ."</td><br /><br />";
echo "Please re-enter the information above";
exit;
} else {

$query = "INSERT INTO upload (name, size, type, content, customer_name, customer_address1, customer_address2, customer_address3, customer_address4, customer_code, customer_tel, customer_email, customer_options, customer_qty ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content','$_POST[customer_name]','$_POST[customer_address1]','$_POST[customer_address2]','$_POST[customer_address3]','$_POST[customer_address4]','$_POST[customer_code]','$_POST[customer_tel]','$_POST[customer_email]','$_POST[customer_options]','$_POST[customer_qty]')";

mysql_query($query) or die('Error, query failed');
include 'closedb.php';

echo "<br>your details have been successfully sent<br>";
}
}
?>

but i want to make sure you can only upload jpgs of gifs! i have the snippit of code to do this but im not sure how to link it in with my code, when i try it just doesnt work, i just get a blank page, i would be very greatful if someone could show me the correct way of linking the jpg restriction code into mine. below is the restriction code i have:


<?php

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
}
else
{
echo "Invalid file";
}

?>



thanks
David

Fumigator
03-28-2008, 05:30 AM
http://www.codingforums.com/showthread.php?t=68462

http://www.codingforums.com/showthread.php?t=136229

Arnaud
03-28-2008, 09:49 AM
if ($_FILES['userfile']['type'] == "image/jpeg") { do something }