PDA

View Full Version : image/pjpeg not working


VEX
01-24-2007, 03:01 AM
This is so weird. This script works perfectly fine and updates the database fine until I try to upload an jpg or jpeg image. What am I doing wrong? I have traced my steps and have reconstructed via cut n' paste and it handles the jpeg fine but it won't in this script for some reason???? I get "Your request was not successful" as I have instructed it to if the upload is not successful.
Can anyone help?


<html>
<head>
<title>Catalog Setup</title>
<script language="JavaScript">
function goToURL() {window.location = "/lp/catsu.html";}
</script>
</head>

<body>
<body bgcolor=#ddddaa>
<center>
<h1>CATALOG SETUP</h1>
<hr height=8>
</center>


<?php

if ($_SERVER['REQUEST_METHOD'] =='POST') {

$input = $_FILES['userfile']['name'];
$input = EscapeShellCmd($input);

if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {



$type = strtolower($_FILES['userfile']['type']);
switch ($type) {
case "image/bmp";
$mimeType = "bmp";
break;
case "image/jpg";
$mimeType = "jpg";
break;
case "image/jpeg";
$mimeType = "jpeg";
break;
case "image/pjpeg";
$mimeType = "jpg";
break;
case "image/gif";
$mimeType = "gif";
break;
default:
$mimeType = "unknown";
}

$file = fopen($_FILES['userfile']['tmp_name'], "r");
$file = fread($file, filesize($_FILES['userfile']['tmp_name']));
$file = addslashes($file);




$link = mysql_connect('localhost', 'root');
if (!$link) {
die('could not connect to catalog' . mysql_error());
}
echo "<center><font color=#aa0000><i>you have connected to the catalog.</i></font></center>";
echo "<a href='/lp/catsu.html'>MAIN MENU</a>";
mysql_select_db('prototad', $link);


$tmp = $_FILES['userfile']['tmp_name'];
$id = mysql_insert_id() + 1;

if ($_POST['seasonal'] == "y") {
$sql = "INSERT into seasonal values ($id, '{$_POST[name]}', {$_POST[quantity]}, '{$_POST[description]}', {$_POST[price]}, '$mimeType')";
$result = mysql_query($sql);

}
$sql = "insert into {$_POST['type']} (id, name, seasonal, quantity, description, price, ext, pic_id) values (0, '{$_POST[name]}', default, {$_POST['quantity']}, '{$_POST[description]}', {$_POST['price']}, '$mimeType', LAST_INSERT_ID())";
$result = mysql_query($sql);


if (mysql_affected_rows($link)) {
$sql = "select * from necklaces";
$result = mysql_query($sql);
$id = mysql_num_rows($result);



$folder = $_POST['type'];
$destination = "uploads\\" . $folder . "\\" . $id . "." . $mimeType;

}

if (move_uploaded_file($tmp, $destination)) {

echo "<p><center>your merchandise was submitted succesfully</center><br><hr>";

}
}
else
{
$file = NULL;
echo "<center>Your request was not successful.</center>";
}
}

?>

<html>
<form action=<?=$_SERVER['PHP_SELF']?> method="POST" enctype="multipart/form-data">
<font color=#aa0000 style=bold> Fields that are in red are required or else your submission will not go through.</font>
<p>
<font color=#aa0000 style=bold>What type of merchandise are you submiting?</font><p>
<select name=type>
<option value=necklaces>Necklaces</option>
<option value=earrings>Earrings</option>
<option value=bracelets>Bracelets</option>
<option value=anklets>Anklets</option>
<option value=watches>Watches</option>
</select>
<p>
Give this item a label for easy lookup if wanted:<p>
<input type=text name=name></text><p>
Give a short description of this item:<p>
<textarea name="description" width=400 height=250></textarea><p>
Would you like to mark this item as seasonal?<p>
<input type="radio" name="seasonal" value="y">
Seasonal<p>
<font color=#aa0000 style=bold>How many are you holding in inventory?<p>
<input type=text name="quantity"><p>
What would you like to set the price to?&nbsp;(please show your answer in the format such as: 1.25 or 10.50)<p>
<input type=text name="price"><p>
<p>
Upload your picture:<p>
<input type="hidden" name='max_file_size' value='30000'>
<input name='userfile' type="file">
</font><p>
<center>
<input type="submit"><p>

<input type=button value="MAIN MENU" onClick="goToURL()">
</script>
</form>
<p>

</center>
</html>


I also just noticed that the pjpeg case in the switch is shifted over in my code I just pasted? Is that relevant? I hope I'm not missing anything minor that I already know but I'm really not catching anything here.

scoutt
01-26-2007, 05:53 PM
first, a jpg and jpeg are the same so they both have this type"image/jpeg" I can almost bet that a pjpeg will have the same type. So what exactly doesn't work when you upload a pjpeg? and why in the world are you addslashes() to a image file?

VEX
01-30-2007, 04:56 PM
Thanks for the reply but I finally got my script to work using the imagesize() function. I still do not know why it would not submit my jpg's. Either way I have the script working now and read up on security a bit better. I was using a tutorial that wasn't written very well. Thanks for the reply any how.

erisco
01-30-2007, 06:33 PM
$mimeType = explode('/', $type);
$mimeType = $mimeType[1];
Now you can recognize any mimetype. No need for cases.

If you are worried about security, I hope PHP is setup to automatically slash data... magic quotes runtime or something... regardless escaping slashes isn't always enough. Always use mysql_real_escape_string() on every variable you are unsure of going into the database.

Just some advice, though, never store images in a database.

scoutt
01-30-2007, 07:16 PM
why would you worry about case? doing what he did by using strtolower() is perfect, no need to split the mimetype. Actually, you want it all at once. this way you can verify it is a image and not something with a jpg marker.

erisco
01-30-2007, 07:37 PM
$mimeType = explode('/', $type);
if ($mimeType[0] == 'image') {
$mimeType = $mimeType[1];
}
else {
$mimeType = 'unkown';
}
That satisfying?

VEX
01-30-2007, 09:01 PM
Actually, this is what I changed it to:


$image = getimagesize($_FILES['userfile']['tmp_name']);
echo $image['mime'];
$image = getimagesize($_FILES['userfile']['tmp_name']);
$mimeType = substr($image['mime'], strpos($image['mime'], "/") + 1);


I do have magic quotes enabled and am still setting up some different filters for my form. I have also used mysql_real_escape_string(). I know my security functions are a little odd up there in my original script. Those were actually leftovers while I was playing around with it earlier. I think I got it down now thanks to these references:

http://phpsec.org/projects/guide/1.html#1.2
http://conf.phpquebec.com/slides/2005/mastering-php-security.pdf?phpquebec=1f52174ee7873ab5e78269507392a6cf