Hello,
I need your help. The script that i have below is intended to upload files. The max upload file size is 1mb. If the size is greater than 1mb, it is supposed to return with an error message displayed.
I tried upload files less then 1mb and it works fine.. however, if i try to upload say a 2.5 mb file, it crashes. I wrote some echo statements to help me decode it but really can't figure it out. Echo statements were placed inside checkMaxMemorySizeLimit()
I am not using this because i read it doesn't work on IE 6 and stuff..
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
I really want the checks to be done in my php first and then i'll add the above later...
uploadfile.php
<?php
session_start();
$errStack_register = array();
if (isset($_POST['submit'])) { // if form has been submitted
require('fileupload.php');
$fileupload = new fileupload("image_upload",$_POST['photo_caption']);
$fileupload->uploadFile();
$errStack_register = $fileupload->validateUserInput();
}
if ( count($errStack_register) < 0){
header('Location: invitefriends.php');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE> - Register</TITLE>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<BODY text=#000000 vLink=#551a8b aLink=#ff0000 link=#0000ee bgColor=#89b7e5 leftMargin=0 topMargin=0 rightMargin=0 marginheight="0" marginwidth="0">
<BR> <BR> <BR>
<?php
if ( count($errStack_register) > 0){
?>
<TABLE cellSpacing=0 cols=4 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD>
<ul>
<?php
for ($i=0; $i<count($errStack_register); $i++){
echo "<li>" . $errStack_register[$i] . "</li>";
}
?>
</ul>
</TD>
</tr></tbody></table>
<?php
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table border="0"><tr>
<TD vAlign=top width="13%"><FONT face=Verdana><FONT color=#ffffff><FONT size=-2>Select a photo</FONT></FONT></FONT><BR><input type="file" name="image_upload" value="<?php echo $fileupload->upload_file_name ;?>"></td>
<TD vAlign=top width="13%"><FONT face=Verdana><FONT color=#ffffff><FONT size=-2>Choose a caption</FONT></FONT></FONT><BR><INPUT class=bginput style="FONT-SIZE: 8pt; FONT-FAMILY: Verdana" tabIndex=2 size=15 name=photo_caption value="<?php echo $fileupload->photo_caption; ?>"></td>
</tr></table>
<TABLE cellSpacing=0 cols=4 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD align="center"><input type="submit" name="submit" value="Submit"></td>
</TR>
</TBODY>
</TABLE>
</form>
</BODY></HTML>
<?php
unset($_SESSION['errStack_register']);
$_SESSION['errStack_register'] = array();
?>
fileupload.php
<?php
// author: Suri Bala
// freely distributable
// http://us3.php.net/features.file-upload
// -- Modified from Original --
error_reporting( E_ALL );
class fileupload{
var $file_upload_flag = "off";
var $upload_max_filesize_bytes = "1048576"; //1MB
var $upload_tmp_dir = "/tmp/"; // leading and trailing slash required
var $upload_dir= "tmp/"; // leading and trailing slash required
var $upload_file_name;
var $upload_file_name_caption;
var $err_stack;
function fileupload($name,$caption) {
$this->err_stack = array();
$this->getConfigurationSettings();
if( is_null($_FILES[$name]) ) {
array_push($this->err_stack, "Please check if the file exists and try again.");
}
$this->getConfigurationSettings();
if( $this->file_upload_flag == "off" ) {
array_push($this->err_stack,"File upload capability is currently turned off. Please check back at a later time.");
}
$this->upload_file_name = $name;
$this->upload_file_name_caption = $caption;
}
function getConfigurationSettings() {
$this->file_upload_flag = ini_get('file_uploads');
$this->upload_tmp_dir = ini_get('upload_tmp_dir');
}
function getErrors() {
return $_FILES[$this->upload_file_name]['error'];
}
function getFileSize() {
//return in bytes
return $_FILES[$this->upload_file_name]['size'];
}
function getFileName() {
return $_FILES[$this->upload_file_name]['name'];
}
function getTmpName() {
return $_FILES[$this->upload_file_name]['tmp_name'];
}
function getupload_max_filesize_inMB(){
//1KB = bytes/1024
//1mb = (KB/1024) = 1048576 bytes;
return ($this->upload_max_filesize_bytes/1048576);
}
function uploadFile() {
if( $this->checkMaxMemorySizeLimit() ) {
array_push($this->err_stack,"The picture you uploaded is greater than the allowable limit of ".$this->getupload_max_filesize_inMB()."MB(s). Please upload a smaller file.");
return;
}else{
if( !move_uploaded_file($this->getTmpName(), $this->upload_dir.$this->getFileName()) ) {
array_push($this->err_stack, "Failed to upload file. Please try again.");
}
}
}
function checkMaxMemorySizeLimit() {
if( $this->getFileSize() > $this->upload_max_filesize_bytes ) {
return true;
}else{
return false;
}
}
function validateUserInput(){
return $this->err_stack;
}
}
?>