Ok so I have a form that is uploading an image and then resizing it and moving a copy of it to a folder. This is all working fine, however:
I have looked all over the internet and I cant find the answer to my problem. I mean I found the answer but it doesnt work. I am trying to limit the size of my uploads. For some reason, if I try to upload a file that is 3mb it will wait until the entire file is uploaded before it processes any script, including any script that detects file size to deny images that large! I just dont get it. How can I limit uploads?
This is my script:
<?php
$conn = mysql_connect("SERVER", "USERNAME", "PASSWORD");
mysql_select_db("DB_NAME");
// Do this process if user has browse the
// file and click the submit button
if(isset($_POST['atn']) && $_POST['atn'] == "edit"){
$title = $_POST['newsTitle'];
$body = $_POST['newsBody'];
$id = $_POST['id'];
$current = $_POST['current'];
$q = "update news set title = '".$title."',body='".$body."',date='".date('Y-m-d')."' where id ='".$id."'";
$results = mysql_query($q,$conn);
if($current == "no"){
$size = 250; // the thumbnail width
$filedir = '../../originals/'; // the directory for the original image
$thumbdir = '../../thumbs/'; // the directory for the thumbnail image
$prefix = 'th_'; // the prefix to be added to the original name
$maxfile = '1000000';
$mode = '0666';
$userfile_name = $id.".jpg";
$userfile_tmp = $_FILES['image']['tmp_name'];
$userfile_size = $_FILES['image']['size'];
$userfile_type = $_FILES['image']['type'];
if($userfile_size > $maxfile){
die("file size too large!");
exit;
}
$prod_img = $filedir.$userfile_name;
$prod_img_thumb = $thumbdir.$prefix.$userfile_name;
if(file_exists($prod_img))
$delImg = unlink($prod_img);
if(file_exists($prod_img_thumb))
$delTh = unlink($prod_img_thumb);
move_uploaded_file($userfile_tmp, $prod_img);
chmod($prod_img, octdec($mode));
//chmod($thumbdir, octdec($mode));
$sizes = getimagesize($prod_img);
if($sizes[1] > $sizes[0]){ //photo is vertical
$aspect_ratio = $sizes[0]/$sizes[1];
if ($sizes[0] <= $size){
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_width = $size;
$new_height = abs($new_width/$aspect_ratio);
}
}
else{ //photo is horizontal
$aspect_ratio = $sizes[0]/$sizes[1];
if ($sizes[0] <= $size){
$new_width = $sizes[0];
$new_height = $sizes[1];
}else{
$new_width = $size;
$new_height = abs($new_width/$aspect_ratio);
}
}
$destimg=ImageCreateTrueColor($new_width,$new_height)or die('Problem In Creating image');
$srcimg=ImageCreateFromJPEG($prod_img)or die('Problem In opening Source Image');
if(function_exists('imagecopyresampled')){
imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))or die('Problem In resizing');
}else{
Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))or die('Problem In resizing');
}
touch($prod_img_thumb);
ImageJPEG($destimg,$prod_img_thumb,90)or die('Problem In saving');
imagedestroy($destimg);
header("Location: index.php?success=true");
}
else{
if($results)
header("Location: index.php?success=true");
}
}
else if(isset($_POST['selectedItems'])){
$id = $_POST['selectedItems'];
$q = "select * from news where id = '".$id."'";
$results = mysql_query($q,$conn);
$row = mysql_fetch_array($results);
}
else
header("Location: index.php");
?>
<html>
<head>
<title>Casamidy news Administration</title>
<link href="../css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="../logo.gif" width="208" height="64"></td>
</tr>
<tr>
<td>
<div id="statusBar" class="<?php if($success){ echo "success"; } ?>" style="display:<?php if($success){ echo "block"; } else{ echo "none"; } ?>;">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td width="2%" class="icon"> </td>
<td width="98%" class="content"><div id="statusContent"><?php if($success){ echo "The Operation was Completed Successfully"; } ?></div></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</table>
</div>
</td>
</tr>
<tr>
<td><?php include("../include/navBar.php"); ?></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="addNewsForm">
<input type="hidden" name="atn" value="edit">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="FCFCFC">
<tr>
<th class="titleBar" colspan="2"><h1>Edit New News Piece</h1></th>
</tr>
<tr>
<td width="15%" style="padding-left:10px;">Title:</td>
<td width="85%"><input type="text" name="newsTitle" id="newsTitle" style="width:345px;" value="<?php echo $row['title']; ?>"></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td valign="top" style="padding-left:10px;">Body:</td>
<td><textarea name="newsBody" id="newsBody" style="width:345px;"><?php echo $row['body']; ?></textarea></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td valign="top" style="padding-left:10px;">Keep Current Photo:</td>
<td><img src="../images/photos.gif" width="16" height="16">
<input name="current" type="radio" id="currentYes" value="yes" checked onClick="javascript:document.getElementById('image').disabled = true;">
yes
<input type="radio" name="current" id="currentNo" value="no" onClick="javascript:document.getElementById('image').disabled = false;">
no </td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td style="padding-left:10px;">Image:</td>
<td>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" name="image" id="image" size="40" style="width:200px;" disabled="disabled"></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td style="padding-left:10px;"><input type="submit" value="Submit" >
<input type="button" value="Cancel" onClick="javascript:history.go(-1)"></td>
<td> </td>
</tr>
<tr>
<th colspan="2" class="footerBar"> </th>
</table>
</form>
</body>
</html>
If I upload a large file it will just sit there, until the file finishes posting! I thought that all the coding and the post_max_size in the php.ini were supposed to prevent that! This is important, please help!