$_FILES problem with uploading
Posted: Thu Feb 14, 2008 10:51 am
Thanks to the code that astions was able to provide to me, we were able to get the image resizing script to work (but only by hardcoding the location of the image on my harddrive). However after making some tweaks to it, i realized that $_FILES['binFile']['name'] was only giving the name of the file and not the full path like we need.
Obviously $_POST['binFile'] won't return the full path (i know because i've tried unless I've done it wrong, which is a real possibility).
Note: We are using pear to connect to our mysql DB
Here is the HTML
Obviously $_POST['binFile'] won't return the full path (i know because i've tried unless I've done it wrong, which is a real possibility).
Note: We are using pear to connect to our mysql DB
Code: Select all
define('MAX_IMAGE_HEIGHT', 100);
define('MAX_IMAGE_WIDTH', 200);
function new_image_by_type($name, $extension)
{
switch($extension)
{
case ".jpg":
return imagecreatefromjpeg($name);
break;
case ".jpeg":
return imagecreatefromjpeg($name);
break;
case ".gif":
return imagecreatefromgif($name);
break;
case ".png":
return imagecreatefrompng($name);
break;
default:
return false;
}
}
if (!empty($_FILES['binFile']['name']))
{
$WHITE_LIST = array('.jpg','.jpeg', '.gif', '.png');
// validate the file extension..
$FILE_INFO = pathinfo($_FILES['binFile']['name']);
$FILE_EXTENSION = '.' . strtolower($FILE_INFO["extension"]);
if (!in_array($FILE_EXTENSION, $WHITE_LIST))
{
exit('The uploaded file is not a valid image type.');
}
//verify upload is actually an image
if (false === ($IMAGE_DATA = getimagesize($_POST['binFile'])))
{
exit('The file uploaded is either corrupt or not a valid image file.');
}
echo "look here" . $_FILES['binFile']['name'];
$ORIGINAL_WIDTH = $IMAGE_DATA[0];
$ORIGINAL_HEIGHT = $IMAGE_DATA[1];
echo $ORIGINAL_WIDTH;
// determine if the image needs to be resized..
if (($ORIGINAL_WIDTH < MAX_IMAGE_WIDTH) && ($ORIGINAL_HEIGHT < MAX_IMAGE_HEIGHT))
{
$content = file_get_contents($_FILES['binFile']['name']);
$content = addslashes($content);
$fileSize = $_FILES['binFile']['size'];
echo "NOT TO BIG";
} else {
// calculate the new image dimensions..
if ($ORIGINAL_WIDTH > $ORIGINAL_HEIGHT)
{
$NEW_WIDTH = floor((MAX_IMAGE_HEIGHT / $ORIGINAL_HEIGHT) * $ORIGINAL_WIDTH);
$NEW_HEIGHT = MAX_IMAGE_HEIGHT;
} else {
$NEW_HEIGHT = floor((MAX_IMAGE_WIDTH / $ORIGINAL_WIDTH) * $ORIGINAL_HEIGHT);
$NEW_WIDTH = MAX_IMAGE_WIDTH;
}
echo "TO BIG";
$NEW_IMAGE = imagecreatetruecolor($NEW_WIDTH, $NEW_HEIGHT);
imagecopyresampled($NEW_IMAGE, new_image_by_type($_FILES['binFile']['name'], $FILE_EXTENSION), 0, 0, 0, 0, $NEW_WIDTH, $NEW_HEIGHT, $ORIGINAL_WIDTH, $ORIGINAL_HEIGHT);
// save image as a jpg and optimize the file size..
imagejpeg($NEW_IMAGE, $_FILES['binFile']['name'], 85);
$content = file_get_contents($_FILES['binFile']['name']);
$content = addslashes($content);
$fileSize = filesize($_FILES['binFile']['name']);
}
$fileName = $_FILES['binFile']['name'];
$fileType = $_FILES['binFile']['type'];
//echo $content;
$sqlquery = $conn->query("INSERT INTO user_extra (user_ID) VALUES ('$id') ON DUPLICATE KEY UPDATE content = '$content', name = '$fileName', size = '$fileSize', type = '$fileType'");
}
Code: Select all
<INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="10000000">
<INPUT TYPE="hidden" NAME="action" VALUE="upload">
<input type="file" size="32" id= "binfile" name="binFile" />