BrighteyesDesign
asked on
Image upload trouble with larger files
I have setup an image upload function here http://www.raglaninternational.com/upload.php
All images upload fine except for ones that creep over the 4mb mark.
With these larger files the following error message shows:
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home7/toolboxl/public_htm l/raglan/u pload_clas s.php on line 116
Any ideas?
All images upload fine except for ones that creep over the 4mb mark.
With these larger files the following error message shows:
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home7/toolboxl/public_htm
Any ideas?
<?php
/*
* CLASS: UPLOAD
* VERSION: 1.0
* DESCRIPTIONS: This class upload and resize images.
* class can upload and resize multiple images in array form or can upload and resize single image.
* AUTHOR: OBAIDULLAH KHAN
* EMAIL: UBAID23@GMAIL.COM
* WEB: HTTP://WWW.MYHUJRA.COM
* COUNTRY: PAKISTAN
* LICENCE: GNU/GPL
*
*/
class Upload
{
public $FileName;
public $NewName;
public $RNewName;
public $File;
public $NewWidth = 600;
public $NewHeight = 600;
public $TWidth = 100;
public $THeight = 100;
public $SavePath;
public $ThumbPath;
public $OverWrite;
public $NameCase;
public $Error;
public $ImageNewExt;
public $ThumbNewExt;
private $Image;
private $width;
private $height;
function Upload()
{
$this -> FileName = 'lssll.jpg';
$this -> OverWrite = true;
$this -> NameCase = '';
$this -> Error = '';
$this -> NewName = '';
$this -> ImageNewExt= '';
$this -> ThumbNewExt= '';
$this -> RNewName = '';
}
function UploadFile()
{
if(is_array($this->File['name']))
{
$this -> _ArrayUpload();
}
else
{
$this -> _NormalUpload();
}
return $this -> Error;
}
function _ArrayUpload()
{
for($i = 0; $i < count($this -> File['name']); $i++)
{
$_FileName = $this->File['name'][$i];
//if new name is set then apply this.
$_NewName = $this -> NewName[$i];
if(!empty($this->File['name'][$i]) and $this -> _FileExist($_NewName, $_FileName,$i) == false)
{
$ErrorMsg= '';
//Upload and resize image
$ErrorMsg = $this -> _UploadImage($this -> File['name'][$i], $this -> File['tmp_name'][$i], $this -> File['size'][$i],
$this -> File['type'][$i],$this -> NewName[$i]);
//==== Creaet Thumb
if(!empty($this -> ThumbPath) && empty($ErrorMsg))
{
$this -> _ThumbUpload($this -> File['name'][$i], $this -> File['tmp_name'][$i], $this -> File['size'][$i],
$this -> File['type'][$i], $this -> NewName[$i]);
}// if save thumb
}//if !empty file name
}//for loop
}
function _NormalUpload()
{
$_FileName = $this->File['name'];
//if new name is set then apply this.
$_NewName = $this -> NewName;
if(!empty($this->File['name']) and $this -> _FileExist($_NewName, $_FileName) == false)
{
//upload and resize image
$this -> _UploadImage($this -> File['name'], $this -> File['tmp_name'], $this -> File['size'],
$this -> File['type'], $this -> NewName);
//upload thumb
if(!empty($this -> ThumbPath))
{
$this -> _ThumbUpload($this -> File['name'], $this -> File['tmp_name'], $this -> File['size'],
$this -> File['type'], $this -> NewName);
}// if save thumb
}// if check file empty and file exist
} // function _Normal Upload
function _UploadImage($FileName, $TmpName, $Size, $Type, $NewName)
{
list($width, $height) = getimagesize($TmpName);
$this -> image = new Image($FileName);
$this -> image -> newWidth = $this -> NewWidth; // new width
$this -> image -> newHeight = $this -> NewHeight; //new height
$this -> image -> PicDir = $this -> SavePath;
$this -> image -> TmpName = $TmpName;
$this -> image -> FileSize = $Size;
$this -> image -> FileType = $Type;
//if user want to change the file name chackname function will do that.
$this -> image -> FileName = $this-> _CheckName($NewName , $FileName, $this -> ImageNewExt);
if($width < $this -> NewWidth and $height < $this -> NewHeight)
{
return $this -> image -> Save(); //use this if you wish images without resizing
}
else
{
return $this -> image -> Resize();
}
}
function _ThumbUpload($FileName, $TmpName, $Size, $Type, $NewName)
{
list($width, $height) = getimagesize($TmpName);
$this -> Timage = new Image($FileName);
$this -> Timage -> newWidth = $this -> TWidth; // new width
$this -> Timage -> newHeight = $this -> THeight; //new height
$this -> Timage -> PicDir = $this -> ThumbPath;
$this -> Timage -> TmpName = $TmpName;
$this -> Timage -> FileSize = $Size;
$this -> Timage -> FileType = $Type;
//if user want to change the file name chackname function will do that.
$this -> Timage -> FileName = $this-> _CheckName($NewName , $FileName, $this -> ThumbNewExt);
if($width < $this -> TWidth and $height < $this -> THeight)
{
$this -> Timage -> Save(); //use this if you wish images without resizing
}
else
{
$this -> Timage -> Resize();
}
}
function _CheckName($NewName,$UpFile, $NewExt = '')
{
if(empty($NewName))
{
return $this->_ChangeCase($UpFile);
}
else
{
$Ext = explode(".",$UpFile);
$Ext = end($Ext);
$Ext = strtolower($Ext);
return $this->_ChangeCase($this -> _CheckExtantion($NewName,$Ext,$NewExt));
}
}
function _CheckExtantion($NewName, $Ext, $NewExt)
{
$Ext2 = explode(".",$NewName);
if(is_array($Ext2))
{
$NewName = $Ext2[0];
}
/* if(!empty($NewExt) && $NewExt != $Ext)
{
return $NewName.'.'.$NewExt;
}
else
{
return $NewName.'.'.$Ext;
}
*/
return $NewName.'.'.$Ext;
}
function _ChangeCase($FileName)
{
if($this->NameCase == 'lower')
{
$this -> RNewName = $FileName;
return strtolower($FileName);
}
elseif($this->NameCase == 'upper')
{
$this -> RNewName = $FileName;
return strtoupper($FileName);
}
else
{
$this -> RNewName = $FileName;
return $FileName;
}
}
function _FileExist($_NewName, $_FileName,$i)
{
if($this -> OverWrite == true)
{
if(file_exists($this->SavePath.$this -> _CheckName($_NewName, $_FileName)))
{
if(!unlink($this->SavePath.$this->_CheckName($_NewName, $_FileName)))
{
$this -> Error[] = "File: ". $this->_CheckName($_NewName, $_FileName) . " Cannot be overwrite.";
}
else
{
if(file_exists($this->ThumbPath.$this -> _CheckName($_NewName, $_FileName)))
{
//also remove thumb
unlink($this->ThumbPath.$this->_CheckName($_NewName, $_FileName));
}
}
}
}
else
{
if(file_exists($this->SavePath. $this -> _CheckName($_NewName, $_FileName)))
{
$this -> Error[] = "File: ". $this -> _CheckName($_NewName, $_FileName) . " aready exist! ";
return true;
}
}
}//function _FileExist
}
?>
upload.php
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You have to change your maximum file size upload.:
post_max_size = 8M
upload_max_filesize = 8M
post_max_size = 8M
upload_max_filesize = 8M
@Hackman, that's already mentioned in my 2 comments :)
ASKER
Thanks guys,
The code on the upload.php also uses imageresizer.class.php (attached)
In this file there is $MaxFileSize = 5000000 but no reference to post max size. Would this be where i need to add code? ie $PostMaxSize = 5000000
Thanks again
The code on the upload.php also uses imageresizer.class.php (attached)
In this file there is $MaxFileSize = 5000000 but no reference to post max size. Would this be where i need to add code? ie $PostMaxSize = 5000000
Thanks again
<?php
//this class is not part of Resize and upload image class.
//this class is property of some one else.
//I get this from phpclasses.org but forget the name of author.
//sorry for that.
class Image {
var $FileName;
var $FileSize;
var $FileType;
var $AllowedExtentions = array ("image/png", "image/gif", "image/jpeg", "image/pjpeg", "image/jpg");
var $newWidth = 100; // new width
var $newHeight = 100; //new height
var $TmpName;
var $PicDir; //store uploaded images
var $MaxFileSize = 5000000; //kbytes
var $ImageQuality = 90; // image compression (max value 100)
function Image($FileName) {
$this->FileName=$FileName;
}
function GetInfo() {
$out=' <br><br>Upload success!<br>
Name: '.$this->FileName.'<br>
file size: '.$this->FileSize.' byte<br>
file type: '.$this->FileType.'<br>
<img src=' . dirname($_SERVER['PHP_SELF']) . '/' . $this->PicDir . $this->FileName . '><br><br>';
return $out;
}
function GetFileExtention ($FileName) {
if (in_array ($this->FileType, $this -> AllowedExtentions)) {
return true;
} else {
return false;
}
}
function ExistFile () {
$fileexist = $_SERVER['DOCUMENT_ROOT'] .
dirname($_SERVER['PHP_SELF']) .
'/' . $this->PicDir .
$this->FileName;
if (file_exists($fileexist)) { return true; }
}
function GetError ($error) {
switch ($error) {
case 0 :
return "Error: Invalid file type <b>$this->FileType</b>! Allow type: .jpg, .jpeg, .gif, .png <b>$this->FileName</b><br>";
break;
case 1 :
return "Error: File <b>$this->FileSize</b> is too large! You must upload 1000 MB file<br>";
break;
case 2 :
return "Error: Please, select a file for uploading!<br>";
break;
case 3 :
return "Error: File <b>$this->FileName</b> already exist!<br>";
break;
}
}
function Resize () {
// header('Content-Type: image/gif');
if (empty ($this->TmpName)) {return $this -> GetError (2);}
else if ($this->FileSize > $this->MaxFileSize) {return $this -> GetError (1);}
else if ($this -> GetFileExtention ($this->FileName) == false) {return $this -> GetError (0);}
else if ($this -> ExistFile()) {return $this -> GetError (3);}
else {
$ext = explode(".",$this->FileName);
$ext = end($ext);
$ext = strtolower($ext);
// Get new sizes
list($width_orig, $height_orig) = getimagesize($this->TmpName);
$ratio_orig = $width_orig/$height_orig;
if ($this->newWidth/$this->newHeight > $ratio_orig) {
$this->newWidth = $this->newHeight*$ratio_orig;
} else {
$this->newHeight = $this->newWidth/$ratio_orig;
}
$normal = imagecreatetruecolor($this->newWidth, $this->newHeight);
if ($ext == "jpg") { $source = imagecreatefromjpeg($this->TmpName); }
else if ($ext == "gif") { $source = imagecreatefromgif ($this->TmpName); }
else if ($ext == "png")
{
$this->ImageQuality = 9;
$source = imagecreatefrompng ($this->TmpName);
}
imagecopyresampled($normal, $source, 0, 0, 0, 0, $this->newWidth, $this->newHeight, $width_orig, $height_orig);
if ($ext == "jpg") {
//ob_start();
imagejpeg($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality");
//$binaryThumbnail = ob_get_contents();
//ob_end_clean();
}
else if ($ext == "gif") { imagegif ($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality"); }
else if ($ext == "png") { imagepng ($normal, "$this->PicDir/$this->FileName", "$this->ImageQuality"); }
imagedestroy($source);
//echo $this -> GetInfo();
}
}
function Save() {
if (empty ($this->TmpName)) {return $this -> GetError (2);}
else if ($this->FileSize > $this->MaxFileSize) {return $this -> GetError (1);}
else if ($this -> GetFileExtention ($this->FileName) == false) {return $this -> GetError (0);}
else if ($this -> ExistFile ()) {return $this -> GetError (3);}
else {
copy($this->TmpName,$this->PicDir.$this->FileName);
//echo $this -> GetInfo();
}
}
}
?>
You don't read what I say!!
Create a txt file call it .htaccess, put this in it and upload .htaccess file in your document root:
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200
Create a txt file call it .htaccess, put this in it and upload .htaccess file in your document root:
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200
ASKER
I did read what you said,
I simply thought as there was another file which mentioned Max Size which i did not attach initially that this may be part of the problem and a solution could have been found by editing that rather than the htaccess method.
I simply thought as there was another file which mentioned Max Size which i did not attach initially that this may be part of the problem and a solution could have been found by editing that rather than the htaccess method.
ASKER
No joy with that CSecurity.
The same error message shows.
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home7/toolboxl/public_htm l/raglan/u pload_clas s.php on line 116
The same error message shows.
Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home7/toolboxl/public_htm
To check what you did is correct or not. Write this in a file:
<?php
phpinfo();
?>
and put it close to uploader.php
Then see max_upload or post_max_size etc. if they are changed. If they are not changed, you did something wrong
<?php
phpinfo();
?>
and put it close to uploader.php
Then see max_upload or post_max_size etc. if they are changed. If they are not changed, you did something wrong
ASKER
Adding the .htaccess to the root folder actually stopped the homepage from showing?
Try reducing sizes a little. Maybe also you have an error in syntax. Make sure you copy pasted properly.
http://roshanbh.com.np/2008/01/uploading-larger-files-in-php.html
php_value post_max_size 150M
php_value upload_max_filesize 150M
php_value max_execution_time 800
php_value max_input_time 800