*
* More features and better error checking will come in the next version
*/
Class Upload
{
var $maxupload_size;
var $HTTP_POST_FILES;
var $errors;
function Upload(&$HTTP_POST_FILES)
{
$this->HTTP_POST_FILES = $HTTP_POST_FILES;
$this->isPosted = false;
$this->maxupload_size = 68700;
}
function save($directory, $field, $overwrite,$mode=0777)
{
$this->isPosted = true;
if ($this->HTTP_POST_FILES[$field]['size'] < $this->maxupload_size && $this->HTTP_POST_FILES[$field]['size'] >0)
{
$noerrors = true;
$this->isPosted = true;
// Get names
$tempName = $this->HTTP_POST_FILES[$field]['tmp_name'];
$file = $this->HTTP_POST_FILES[$field]['name'];
$all = sprintf("%s%s",$directory,$file);
// Copy to directory
if (file_exists($all))
{
if ($overwrite)
{
unlink($all) || $noerrors=false; $this->errors = "Upload class save error: unable to overwrite ".$all."
";
copy($tempName,$all) || $noerrors=false; $this->errors .= "Upload class save error: unable to copy to ".$all."
";
chmod($all,$mode) || $ernoerrorsrors=false; $this->errors .= "Upload class save error: unable to change permissions for: ".$all."
";
}
} else{
copy($tempName,$all) || $noerrors=false;$this->errors = "Upload class save error: unable to copy to ".$all."
";
chmod($all,$mode) || $noerrors=false;$this->errors .= "Upload class save error: unable to change permissions for: ".$all."
";
}
return $noerrors;
} elseif ($this->HTTP_POST_FILES[$field]['size'] > $this->maxupload_size) {
$this->errors = "File size exceeds maximum file size of ".$this->maxuploadsize." bytes";
return false;
} elseif ($this->HTTP_POST_FILES[$field]['size'] == 0) {
$this->errors = "File size is 0 bytes";
return false;
}
}
function append($directory, $field, $addcrlf=0, $mode=0777)
{
$this->isPosted = true;
if ($this->HTTP_POST_FILES[$field]['size'] < $this->maxupload_size && $this->HTTP_POST_FILES[$field]['size'] >0)
{
$noerrors = true;
$this->isPosted = true;
// Get names
$tempName = $this->HTTP_POST_FILES[$field]['tmp_name'];
$file = $this->HTTP_POST_FILES[$field]['name'];
$all = sprintf("%s%s",$directory,$file);
// Copy to directory
if (file_exists($all))
{
$fp = @fopen($all,"a+");
if($fp) {
if($addcrlf)
fwrite($fp,"\x0D\x0A");
$ndata = file($tempName);
$x=1;
while($ndata[$x]) fwrite($fp,$ndata[$x++]);
fclose($fp);
chmod($all,$mode) || $noerrors=false; $this->errors .= "Upload class save error: unable to change permissions for: ".$all."
";
}
} else{
copy($tempName,$all) || $noerrors=false;$this->errors = "Upload class save error: unable to copy to ".$all."
";
chmod($all,$mode) || $noerrors=false;$this->errors .= "Upload class save error: unable to change permissions for: ".$all."
";
}
return $noerrors;
} elseif ($this->HTTP_POST_FILES[$field]['size'] > $this->maxupload_size) {
$this->errors = "File size exceeds maximum file size of ".$this->maxuploadsize." bytes";
return false;
} elseif ($this->HTTP_POST_FILES[$field]['size'] == 0) {
$this->errors = "File size is 0 bytes";
return false;
}
}
function saveAs($filename, $directory, $field, $overwrite,$mode=0777)
{
$this->isPosted = true;
if ($this->HTTP_POST_FILES[$field]['size'] < $this->maxupload_size && $this->HTTP_POST_FILES[$field]['size'] >0)
{
$noerrors = true;
// Get names
$tempName = $this->HTTP_POST_FILES[$field]['tmp_name'];
$all = $directory.$filename;
// Copy to directory
if (file_exists($all))
{
if ($overwrite)
{
@unlink($all) || $noerrors=false; $this->errors = "Upload class saveas error: unable to overwrite ".$all."
";
@copy($tempName,$all) || $noerrors=false; $this->errors .= "Upload class saveas error: unable to copy to ".$all."
";
@chmod($all,$mode) || $noerrors=false; $this->errors .= "Upload class saveas error: unable to copy to".$all."
";
}
} else{
@copy($tempName,$all) || $noerrors=false; $this->errors = "Upload class saveas error: unable to copy to ".$all."
";
@chmod($all,$mode) || $noerrors=false; $this->errors .= "Upload class saveas error: unable to change permissions for: ".$all."
";
}
return $noerrors;
} elseif ($this->HTTP_POST_FILES[$field]['size'] > $this->maxupload_size) {
$this->errors = "File size exceeds maximum file size of ".$this->maxuploadsize." bytes";
return false;
} elseif ($this->HTTP_POST_FILES[$field]['size'] == 0) {
$this->errors = "File size is 0 bytes";
return false;
}
}
function getFilename($field)
{
return $this->HTTP_POST_FILES[$field]['name'];
}
function getFileMimeType($field)
{
return $this->HTTP_POST_FILES[$field]['type'];
}
function getFileSize($field)
{
return $this->HTTP_POST_FILES[$field]['size'];
}
}
?>