Hello Everyone,
I'm trying to debug someone elses php code. I'm actually a Perl
programmer, with OO experience, but not in php.
The code is supposed to upload a photo from a form and save it both on
the file system and in a mySql database. 90% of the time it works just
fine. But for some reason, with some photos, it doesn't work. The
image never shows up on the filesystem and there is no entry in the
database. I feel it's a problem with the image because if I resave the
image in photoshop with a lower resolution -- it uploads just fine. I
don't completely understand why this resaving of the image works though
because the orriginal size of the image is still well under the
FileSize Max, which according to the code below is 3.5mb.
I'm not including all of the code because there is a lot. I believe
I've pasted the correct snips below though. If you need any more info,
please let me know.
My main quesitons are:
1. How/where in the code, is the image file being written to the disk?
I don't see the 'imagejpeg' function I am familar with.
2. I want to put in some debugging print statments to figure out what's
going on -- maybe have a log file being written to but I don't know how
to go about that in OO php? Can someone give me an example to do this?
Thanks for any help and guidance!
M
-- Snip -- This is the code in the page that accepts the photos from a
form. There are actually 7 photo fields on that page. This is just one
of them.
$this->Image2 = new clsControl(ccsI mage, "Image2", "Image2", ccsText,
"", CCGetRequestPar am("Image2", $Method));
$this->photo2 = new clsFileUpload(" photo2", "photo2", "temp/",
"photos/", "*.jpg", "", 3500000);
-- Snip -- This is the complete clsFileUpload Class
//clsFileUpload Class @0-81C1F4F1
class clsFileUpload
{
var $Name;
var $Caption;
var $Visible;
var $Required;
var $TemporaryFolde r;
var $FileFolder;
var $AllowedMask; // @deprecated , use AllowedFileMask s property
var $AllowedFileMas ks;
var $DisallowedFile Masks;
var $FileSizeLimit;
var $Value;
var $Text;
var $State;
var $CCSEvents = "";
var $CCSEventResult ;
function clsFileUpload($ Name, $Caption, $TemporaryFolde r,
$FileFolder, $AllowedFileMas ks, $DisallowedFile Masks, $FileSizeLimit)
{
$this->Errors = new clsErrors;
$this->Name = $Name;
$this->Visible = true;
$this->Caption = $Caption;
if(substr($Temp oraryFolder, 0, 1) == "%") {
$TemporaryFolde r = substr($Tempora ryFolder, 1);
$TemporaryFolde r = getenv($Tempora ryFolder);
}
$this->TemporaryFolde r = $TemporaryFolde r;
if(substr($File Folder, 0, 1) == "%") {
$FileFolder = substr($FileFol der, 1);
$FileFolder = getenv($FileFol der);
}
$this->FileFolder = $FileFolder;
$this->AllowedFileMas ks = $AllowedFileMas ks;
$this->AllowedMask = & $this->AllowedFileMas ks;
$this->DisallowedFile Masks = $DisallowedFile Masks;
$this->FileSizeLimi t = $FileSizeLimit;
$this->Value = "";
$this->Text = "";
$this->Required = false;
$FileName = "";
$FieldName = $this->Caption;
if( !is_dir($Tempor aryFolder) ) {
$this->Errors->addError("Unab le to upload the file specified in "
.. $FieldName . " - temporary upload folder doesn't exist.");
} else if( !is_writable($T emporaryFolder) ) {
$this->Errors->addError("Insu fficient filesystem permissions to
upload the file specified in " . $FieldName . " into temporary
folder.");
} else if( !is_dir($FileFo lder) ) {
$this->Errors->addError("Unab le to upload the file specified in "
.. $FieldName . " - upload folder doesn't exist.");
} else if( !is_writable($F ileFolder) ) {
$this->Errors->addError("Insu fficient filesystem permissions to
upload the file specified in " . $FieldName . ".");
}
}
function Validate()
{
$validation = true;
if($this->Required && $this->Value === "" && $this->Errors->Count() == 0)
{
$FieldName = $this->Caption;
$this->Errors->addError("Th e file attachment in field " .
$FieldName . " is required.");
}
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "OnValidate ");
return ($this->Errors->Count() == 0);
}
function Upload($RowNumb er = "")
{
global $HTTP_POST_FILE S;
$FieldName = $this->Caption;
if(strlen($RowN umber)) {
$ControlName = $this->Name . "_" . $RowNumber;
$FileControl = $this->Name . "_File_" . $RowNumber;
$DeleteControl = $this->Name . "_Delete_" . $RowNumber;
} else {
$ControlName = $this->Name;
$FileControl = $this->Name . "_File";
$DeleteControl = $this->Name . "_Delete";
}
$SessionName = CCGetParam($Con trolName);
$this->State = CCGetSession($S essionName);
if (strlen(CCGetPa ram($DeleteCont rol))) {
// delete file from folder
$ActualFileName = $this->State[0];
if( file_exists($th is->FileFolder . $ActualFileName ) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->FileFolder . $ActualFileName );
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
} else if ( file_exists($th is->TemporaryFolde r . $ActualFileName ) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $ActualFileName );
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
}
$this->Value = ""; $this->Text = "";
$this->State[0] = "";
} else if (isset ($HTTP_POST_FIL ES[$FileControl])
&& $HTTP_POST_FILE S[$FileControl]["tmp_name"] != "none"
&& strlen ($HTTP_POST_FIL ES[$FileControl]["tmp_name"])) {
$this->Value = ""; $this->Text = "";
$FileName = $HTTP_POST_FILE S[$FileControl]["name"];
$GoodFileMask = 1;
$meta_character s = array("*" =".+", "?" =".", "\\" ="\\\\",
"^" ="\\^", "\$" ="\\\$", "." ="\\.", "[" ="\\[", "]" ="\\]",
"|" ="\\|", "(" ="\\(", ")" ="\\)", "{" ="\\{", "}" ="\\}",
"+" ="\\+", "-" ="\\-");
if ($this->AllowedFileMas ks != "") {
$GoodFileMask = 0;
$FileMasks=spli t(';', $this->AllowedFileMas ks);
foreach ($FileMasks as $FileMask) {
$FileMask =
preg_replace("/(\\*|\\?|\\\\|\ \^|\\\$|\\.|\\[|\\]|\\||\\(|\\)|\\ {|\\}|\\+|\\-)/ei",
"\$meta_charact ers['\\1']", $FileMask);
if (preg_match("/^$FileMask$/i", $FileName)) {
$GoodFileMask = 1;
break;
}
}
}
if ($GoodFileMask && $this->DisallowedFile Masks != "") {
$FileMasks=spli t(';', $this->DisallowedFile Masks);
foreach ($FileMasks as $FileMask) {
$FileMask =
preg_replace("/(\\*|\\?|\\\\|\ \^|\\\$|\\.|\\[|\\]|\\||\\(|\\)|\\ {|\\}|\\+|\\-)/ei",
"\$meta_charact ers['\\1']", $FileMask);
if (preg_match("/^$FileMask$/i", $FileName)) {
$GoodFileMask = 0;
break;
}
}
}
if($HTTP_POST_F ILES[$FileControl]["size"] $this->FileSizeLimi t) {
$this->Errors->addError("Th e file size in field " . $FieldName
.. " is too large.");
} else if (!$GoodFileMask ) {
$this->Errors->addError("Th e file type specified in field " .
$FieldName . " is not allowed.");
} else {
// move uploaded file to temporary folder
$file_exists = true;
$index = 0;
while($file_exi sts) {
$ActualFileName = date("YmdHis") . $index . "." . $FileName;
$file_exists = file_exists($Ac tualFileName);
$index++;
}
if( copy($HTTP_POST _FILES[$FileControl]["tmp_name"],
$this->TemporaryFolde r . $ActualFileName ) ) {
$this->Value = $ActualFileName ;
$this->Text = $ActualFileName ;
if(strlen($this->State[0])) {
if(file_exists( $this->TemporaryFolde r . $this->State[0])) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $this->State[0]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
$this->State[0] = $ActualFileName ;
} else {
if(!is_dir($thi s->TemporaryFolde r . $this->State[1]) &&
file_exists($th is->TemporaryFolde r . $this->State[1])) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $this->State[1]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
}
$this->State[1] = $ActualFileName ;
}
} else {
$this->State[0] = $ActualFileName ;
}
} else {
$this->Errors->addError("Insu fficient filesystem permissions
to upload the file specified in " . $FieldName . " into temporary
folder.");
}
}
} else {
$this->SetValue(strle n($this->State[1]) ? $this->State[1] :
$this->State[0]);
}
}
function Move()
{
if (strlen($this->Value) && !file_exists($t his->FileFolder .
$this->Value)) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeProcessF ile");
$FileName = $this->GetFileName( );
$FieldName = $this->Caption;
if (!file_exists($ this->TemporaryFolde r . $this->Value)) {
$this->Errors->addError("Th e file " . $FileName . " specified
in " . $FieldName . " was not found.");
} else if (!@copy($this->TemporaryFolde r . $this->Value,
$this->FileFolder . $this->Value)) {
$this->Errors->addError("Insu fficient filesystem permissions to
upload the file specified in " . $FieldName . ".");
} else if (strlen($this->State[1])) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->FileFolder . $this->State[0]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
}
if($this->Errors->Count() == 0 &&
file_exists($th is->TemporaryFolde r . $this->Value)) {
unlink($this->TemporaryFolde r . $this->Value);
}
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterProcessFi le");
}
}
function Delete()
{
if( !is_dir($this->FileFolder . $this->State[0]) &&
file_exists($th is->FileFolder . $this->State[0]) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeDeleteFi le");
unlink($this->FileFolder . $this->State[0]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterDeleteFil e");
} else if ( !is_dir($this->TemporaryFolde r . $this->State[0]) &&
file_exists($th is->TemporaryFolde r . $this->State[0]) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $this->State[0]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterDeleteFil e");
}
if( !is_dir($this->FileFolder . $this->State[1]) &&
file_exists($th is->FileFolder . $this->State[1]) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeDeleteFi le");
unlink($this->FileFolder . $this->State[1]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterDeleteFil e");
} else if ( !is_dir($this->TemporaryFolde r . $this->State[1]) &&
file_exists($th is->TemporaryFolde r . $this->State[1]) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $this->State[1]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterDeleteFil e");
}
}
function Show($RowNumber = "")
{
global $Tpl;
if($this->Visible)
{
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeShow ");
if(!$this->Visible) {
$Tpl->setblockvar("F ileUpload " . $this->Name, "");
return;
}
if(strlen($RowN umber)) {
$ControlName = $this->Name . "_" . $RowNumber;
$FileControl = $this->Name . "_File_" . $RowNumber;
$DeleteControl = $this->Name . "_Delete_" . $RowNumber;
} else {
$ControlName = $this->Name;
$FileControl = $this->Name . "_File";
$DeleteControl = $this->Name . "_Delete";
}
$SessionName = CCGetParam($Con trolName);
if(!strlen($Ses sionName)) {
srand ((double) microtime() * 1000000);
$random_value = rand();
$SessionName = "FileUpload " . $random_value . date("dHis");
$this->State = array($this->Value, "");
}
CCSetSession($S essionName, $this->State);
$Tpl->SetVar("State" , $SessionName);
$Tpl->SetVar("Contro lName", $ControlName);
$Tpl->SetVar("FileCo ntrol", $FileControl);
$Tpl->SetVar("Delete Control", $DeleteControl) ;
if (strlen($this->Value) ) {
$Tpl->SetVar("Actual FileName", $this->Value);
$Tpl->SetVar("FileNa me", $this->GetFileName()) ;
$Tpl->SetVar("FileSi ze", $this->GetFileSize()) ;
$Tpl->parse("FileUpl oad " . $this->Name . "/Info", false);
if($this->Required) {
$Tpl->parse("FileUpl oad " . $this->Name . "/Upload", false);
$Tpl->setblockvar("F ileUpload " . $this->Name .
"/DeleteControl", "");
} else {
$Tpl->setblockvar("F ileUpload " . $this->Name . "/Upload", "");
$Tpl->parse("FileUpl oad " . $this->Name . "/DeleteControl", false);
}
} else {
$Tpl->parse("FileUpl oad " . $this->Name . "/Upload", false);
$Tpl->setblockvar("F ileUpload " . $this->Name . "/Info", "");
$Tpl->setblockvar("F ileUpload " . $this->Name . "/DeleteControl", "");
}
$Tpl->Parse("FileUpl oad " . $this->Name, false);
}
else
{
$Tpl->setblockvar("F ileUpload " . $this->Name, "");
}
}
function SetValue($Value ) {
$this->Text = $Value;
$this->Value = $Value;
$this->State[0] = $Value;
if(strlen($Valu e)
&& !file_exists($t his->TemporaryFolde r . $Value)
&& !file_exists($t his->FileFolder . $Value)) {
$FileName = $this->GetFileName( );
$FieldName = $this->Caption;
$this->Errors->addError("Th e file " . $FileName . " specified
in " . $FieldName . " was not found.");
}
}
function SetText($Text) {
$this->SetValue($Text );
}
function GetValue() {
return $this->Value;
}
function GetText() {
return $this->Text;
}
function GetFileName() {
return preg_match("/^\d{14,}\./", $this->Value) ?
substr($this->Value, strpos($this->Value, ".") + 1) : $this->Value;
}
function GetFileSize() {
$filesize = 0;
if( file_exists($th is->FileFolder . $this->Value) ) {
$filesize = filesize($this->FileFolder . $this->Value);
} else if ( file_exists($th is->TemporaryFolde r . $this->Value) ) {
$filesize = filesize($this->TemporaryFolde r . $this->Value);
}
return $filesize;
}
}
//End clsFileUpload Class
I'm trying to debug someone elses php code. I'm actually a Perl
programmer, with OO experience, but not in php.
The code is supposed to upload a photo from a form and save it both on
the file system and in a mySql database. 90% of the time it works just
fine. But for some reason, with some photos, it doesn't work. The
image never shows up on the filesystem and there is no entry in the
database. I feel it's a problem with the image because if I resave the
image in photoshop with a lower resolution -- it uploads just fine. I
don't completely understand why this resaving of the image works though
because the orriginal size of the image is still well under the
FileSize Max, which according to the code below is 3.5mb.
I'm not including all of the code because there is a lot. I believe
I've pasted the correct snips below though. If you need any more info,
please let me know.
My main quesitons are:
1. How/where in the code, is the image file being written to the disk?
I don't see the 'imagejpeg' function I am familar with.
2. I want to put in some debugging print statments to figure out what's
going on -- maybe have a log file being written to but I don't know how
to go about that in OO php? Can someone give me an example to do this?
Thanks for any help and guidance!
M
-- Snip -- This is the code in the page that accepts the photos from a
form. There are actually 7 photo fields on that page. This is just one
of them.
$this->Image2 = new clsControl(ccsI mage, "Image2", "Image2", ccsText,
"", CCGetRequestPar am("Image2", $Method));
$this->photo2 = new clsFileUpload(" photo2", "photo2", "temp/",
"photos/", "*.jpg", "", 3500000);
-- Snip -- This is the complete clsFileUpload Class
//clsFileUpload Class @0-81C1F4F1
class clsFileUpload
{
var $Name;
var $Caption;
var $Visible;
var $Required;
var $TemporaryFolde r;
var $FileFolder;
var $AllowedMask; // @deprecated , use AllowedFileMask s property
var $AllowedFileMas ks;
var $DisallowedFile Masks;
var $FileSizeLimit;
var $Value;
var $Text;
var $State;
var $CCSEvents = "";
var $CCSEventResult ;
function clsFileUpload($ Name, $Caption, $TemporaryFolde r,
$FileFolder, $AllowedFileMas ks, $DisallowedFile Masks, $FileSizeLimit)
{
$this->Errors = new clsErrors;
$this->Name = $Name;
$this->Visible = true;
$this->Caption = $Caption;
if(substr($Temp oraryFolder, 0, 1) == "%") {
$TemporaryFolde r = substr($Tempora ryFolder, 1);
$TemporaryFolde r = getenv($Tempora ryFolder);
}
$this->TemporaryFolde r = $TemporaryFolde r;
if(substr($File Folder, 0, 1) == "%") {
$FileFolder = substr($FileFol der, 1);
$FileFolder = getenv($FileFol der);
}
$this->FileFolder = $FileFolder;
$this->AllowedFileMas ks = $AllowedFileMas ks;
$this->AllowedMask = & $this->AllowedFileMas ks;
$this->DisallowedFile Masks = $DisallowedFile Masks;
$this->FileSizeLimi t = $FileSizeLimit;
$this->Value = "";
$this->Text = "";
$this->Required = false;
$FileName = "";
$FieldName = $this->Caption;
if( !is_dir($Tempor aryFolder) ) {
$this->Errors->addError("Unab le to upload the file specified in "
.. $FieldName . " - temporary upload folder doesn't exist.");
} else if( !is_writable($T emporaryFolder) ) {
$this->Errors->addError("Insu fficient filesystem permissions to
upload the file specified in " . $FieldName . " into temporary
folder.");
} else if( !is_dir($FileFo lder) ) {
$this->Errors->addError("Unab le to upload the file specified in "
.. $FieldName . " - upload folder doesn't exist.");
} else if( !is_writable($F ileFolder) ) {
$this->Errors->addError("Insu fficient filesystem permissions to
upload the file specified in " . $FieldName . ".");
}
}
function Validate()
{
$validation = true;
if($this->Required && $this->Value === "" && $this->Errors->Count() == 0)
{
$FieldName = $this->Caption;
$this->Errors->addError("Th e file attachment in field " .
$FieldName . " is required.");
}
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "OnValidate ");
return ($this->Errors->Count() == 0);
}
function Upload($RowNumb er = "")
{
global $HTTP_POST_FILE S;
$FieldName = $this->Caption;
if(strlen($RowN umber)) {
$ControlName = $this->Name . "_" . $RowNumber;
$FileControl = $this->Name . "_File_" . $RowNumber;
$DeleteControl = $this->Name . "_Delete_" . $RowNumber;
} else {
$ControlName = $this->Name;
$FileControl = $this->Name . "_File";
$DeleteControl = $this->Name . "_Delete";
}
$SessionName = CCGetParam($Con trolName);
$this->State = CCGetSession($S essionName);
if (strlen(CCGetPa ram($DeleteCont rol))) {
// delete file from folder
$ActualFileName = $this->State[0];
if( file_exists($th is->FileFolder . $ActualFileName ) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->FileFolder . $ActualFileName );
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
} else if ( file_exists($th is->TemporaryFolde r . $ActualFileName ) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $ActualFileName );
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
}
$this->Value = ""; $this->Text = "";
$this->State[0] = "";
} else if (isset ($HTTP_POST_FIL ES[$FileControl])
&& $HTTP_POST_FILE S[$FileControl]["tmp_name"] != "none"
&& strlen ($HTTP_POST_FIL ES[$FileControl]["tmp_name"])) {
$this->Value = ""; $this->Text = "";
$FileName = $HTTP_POST_FILE S[$FileControl]["name"];
$GoodFileMask = 1;
$meta_character s = array("*" =".+", "?" =".", "\\" ="\\\\",
"^" ="\\^", "\$" ="\\\$", "." ="\\.", "[" ="\\[", "]" ="\\]",
"|" ="\\|", "(" ="\\(", ")" ="\\)", "{" ="\\{", "}" ="\\}",
"+" ="\\+", "-" ="\\-");
if ($this->AllowedFileMas ks != "") {
$GoodFileMask = 0;
$FileMasks=spli t(';', $this->AllowedFileMas ks);
foreach ($FileMasks as $FileMask) {
$FileMask =
preg_replace("/(\\*|\\?|\\\\|\ \^|\\\$|\\.|\\[|\\]|\\||\\(|\\)|\\ {|\\}|\\+|\\-)/ei",
"\$meta_charact ers['\\1']", $FileMask);
if (preg_match("/^$FileMask$/i", $FileName)) {
$GoodFileMask = 1;
break;
}
}
}
if ($GoodFileMask && $this->DisallowedFile Masks != "") {
$FileMasks=spli t(';', $this->DisallowedFile Masks);
foreach ($FileMasks as $FileMask) {
$FileMask =
preg_replace("/(\\*|\\?|\\\\|\ \^|\\\$|\\.|\\[|\\]|\\||\\(|\\)|\\ {|\\}|\\+|\\-)/ei",
"\$meta_charact ers['\\1']", $FileMask);
if (preg_match("/^$FileMask$/i", $FileName)) {
$GoodFileMask = 0;
break;
}
}
}
if($HTTP_POST_F ILES[$FileControl]["size"] $this->FileSizeLimi t) {
$this->Errors->addError("Th e file size in field " . $FieldName
.. " is too large.");
} else if (!$GoodFileMask ) {
$this->Errors->addError("Th e file type specified in field " .
$FieldName . " is not allowed.");
} else {
// move uploaded file to temporary folder
$file_exists = true;
$index = 0;
while($file_exi sts) {
$ActualFileName = date("YmdHis") . $index . "." . $FileName;
$file_exists = file_exists($Ac tualFileName);
$index++;
}
if( copy($HTTP_POST _FILES[$FileControl]["tmp_name"],
$this->TemporaryFolde r . $ActualFileName ) ) {
$this->Value = $ActualFileName ;
$this->Text = $ActualFileName ;
if(strlen($this->State[0])) {
if(file_exists( $this->TemporaryFolde r . $this->State[0])) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $this->State[0]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
$this->State[0] = $ActualFileName ;
} else {
if(!is_dir($thi s->TemporaryFolde r . $this->State[1]) &&
file_exists($th is->TemporaryFolde r . $this->State[1])) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $this->State[1]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
}
$this->State[1] = $ActualFileName ;
}
} else {
$this->State[0] = $ActualFileName ;
}
} else {
$this->Errors->addError("Insu fficient filesystem permissions
to upload the file specified in " . $FieldName . " into temporary
folder.");
}
}
} else {
$this->SetValue(strle n($this->State[1]) ? $this->State[1] :
$this->State[0]);
}
}
function Move()
{
if (strlen($this->Value) && !file_exists($t his->FileFolder .
$this->Value)) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeProcessF ile");
$FileName = $this->GetFileName( );
$FieldName = $this->Caption;
if (!file_exists($ this->TemporaryFolde r . $this->Value)) {
$this->Errors->addError("Th e file " . $FileName . " specified
in " . $FieldName . " was not found.");
} else if (!@copy($this->TemporaryFolde r . $this->Value,
$this->FileFolder . $this->Value)) {
$this->Errors->addError("Insu fficient filesystem permissions to
upload the file specified in " . $FieldName . ".");
} else if (strlen($this->State[1])) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"BeforeDeleteFi le");
unlink($this->FileFolder . $this->State[0]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents,
"AfterDeleteFil e");
}
if($this->Errors->Count() == 0 &&
file_exists($th is->TemporaryFolde r . $this->Value)) {
unlink($this->TemporaryFolde r . $this->Value);
}
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterProcessFi le");
}
}
function Delete()
{
if( !is_dir($this->FileFolder . $this->State[0]) &&
file_exists($th is->FileFolder . $this->State[0]) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeDeleteFi le");
unlink($this->FileFolder . $this->State[0]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterDeleteFil e");
} else if ( !is_dir($this->TemporaryFolde r . $this->State[0]) &&
file_exists($th is->TemporaryFolde r . $this->State[0]) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $this->State[0]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterDeleteFil e");
}
if( !is_dir($this->FileFolder . $this->State[1]) &&
file_exists($th is->FileFolder . $this->State[1]) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeDeleteFi le");
unlink($this->FileFolder . $this->State[1]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterDeleteFil e");
} else if ( !is_dir($this->TemporaryFolde r . $this->State[1]) &&
file_exists($th is->TemporaryFolde r . $this->State[1]) ) {
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeDeleteFi le");
unlink($this->TemporaryFolde r . $this->State[1]);
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "AfterDeleteFil e");
}
}
function Show($RowNumber = "")
{
global $Tpl;
if($this->Visible)
{
$this->CCSEventResu lt = CCGetEvent($thi s->CCSEvents, "BeforeShow ");
if(!$this->Visible) {
$Tpl->setblockvar("F ileUpload " . $this->Name, "");
return;
}
if(strlen($RowN umber)) {
$ControlName = $this->Name . "_" . $RowNumber;
$FileControl = $this->Name . "_File_" . $RowNumber;
$DeleteControl = $this->Name . "_Delete_" . $RowNumber;
} else {
$ControlName = $this->Name;
$FileControl = $this->Name . "_File";
$DeleteControl = $this->Name . "_Delete";
}
$SessionName = CCGetParam($Con trolName);
if(!strlen($Ses sionName)) {
srand ((double) microtime() * 1000000);
$random_value = rand();
$SessionName = "FileUpload " . $random_value . date("dHis");
$this->State = array($this->Value, "");
}
CCSetSession($S essionName, $this->State);
$Tpl->SetVar("State" , $SessionName);
$Tpl->SetVar("Contro lName", $ControlName);
$Tpl->SetVar("FileCo ntrol", $FileControl);
$Tpl->SetVar("Delete Control", $DeleteControl) ;
if (strlen($this->Value) ) {
$Tpl->SetVar("Actual FileName", $this->Value);
$Tpl->SetVar("FileNa me", $this->GetFileName()) ;
$Tpl->SetVar("FileSi ze", $this->GetFileSize()) ;
$Tpl->parse("FileUpl oad " . $this->Name . "/Info", false);
if($this->Required) {
$Tpl->parse("FileUpl oad " . $this->Name . "/Upload", false);
$Tpl->setblockvar("F ileUpload " . $this->Name .
"/DeleteControl", "");
} else {
$Tpl->setblockvar("F ileUpload " . $this->Name . "/Upload", "");
$Tpl->parse("FileUpl oad " . $this->Name . "/DeleteControl", false);
}
} else {
$Tpl->parse("FileUpl oad " . $this->Name . "/Upload", false);
$Tpl->setblockvar("F ileUpload " . $this->Name . "/Info", "");
$Tpl->setblockvar("F ileUpload " . $this->Name . "/DeleteControl", "");
}
$Tpl->Parse("FileUpl oad " . $this->Name, false);
}
else
{
$Tpl->setblockvar("F ileUpload " . $this->Name, "");
}
}
function SetValue($Value ) {
$this->Text = $Value;
$this->Value = $Value;
$this->State[0] = $Value;
if(strlen($Valu e)
&& !file_exists($t his->TemporaryFolde r . $Value)
&& !file_exists($t his->FileFolder . $Value)) {
$FileName = $this->GetFileName( );
$FieldName = $this->Caption;
$this->Errors->addError("Th e file " . $FileName . " specified
in " . $FieldName . " was not found.");
}
}
function SetText($Text) {
$this->SetValue($Text );
}
function GetValue() {
return $this->Value;
}
function GetText() {
return $this->Text;
}
function GetFileName() {
return preg_match("/^\d{14,}\./", $this->Value) ?
substr($this->Value, strpos($this->Value, ".") + 1) : $this->Value;
}
function GetFileSize() {
$filesize = 0;
if( file_exists($th is->FileFolder . $this->Value) ) {
$filesize = filesize($this->FileFolder . $this->Value);
} else if ( file_exists($th is->TemporaryFolde r . $this->Value) ) {
$filesize = filesize($this->TemporaryFolde r . $this->Value);
}
return $filesize;
}
}
//End clsFileUpload Class
Comment