dwh/class/FileUploadClass.php
2024-01-12 15:25:44 +07:00

334 lines
8.9 KiB
PHP

<?php
// author: Suri Bala
// freely distributable
class FileUploadClass{
public $upload_tmp_dir = "/tmp/"; // leading and trailing slash required
public $file_upload_flag = "off";
private $upload_max_filesize_literal = "1.5M";
private $upload_max_filesize = 0;
public $allowable_upload_base_dirs = array("/tmp/");
public $allowable_upload_tmp_dirs = array( "/tmp/");
public $upload_dir= "/tmp/"; // leading and trailing slash required
public $upload_file_name;
public $display_error = 0;
public $new_filename = '';
public $last_msg;
public $ori_ext;
private $file_info;
function __construct($name) {
if( is_null($_FILES[$name]) ) {
if ($this->debug) {
echo "Specified file <strong> ".$name." </strong> does not exist in the FILES array. Please check if it exists<br>\n";
echo "Exiting...";
}
exit;
}
$this->allowable_upload_base_dirs = array(UPLOADDIR,TEMPUPLOADDIR);
$this->getConfigurationSettings();
if( $this->file_upload_flag == "off" ) {
if ($this->debug) {
echo "File upload capability in the configuration file is turned <strong> off </strong> . Please update the php.ini file.";
}
exit;
}
$this->upload_file_name = $name;
}
private function getConfigurationSettings() {
$this->file_upload_flag = ini_get('file_uploads');
$this->upload_tmp_dir = ini_get('upload_tmp_dir');
$max_filesize_server_setting = $this->convertFileSize(ini_get('upload_max_filesize'));
$max_filesize_prog_setting = $this->convertFileSize($this->upload_max_filesize_literal);
if ($max_filesize_prog_setting > $max_filesize_server_setting) {
$this->upload_max_filesize = $max_filesize_server_setting;
} else {
$this->upload_max_filesize = $max_filesize_prog_setting;
}
}
private function convertFileSize($val){
$last = strtolower($val{strlen($val)-1});
switch($last) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $val;
}
public function setFileSizeDefault(){
$val = ini_get('upload_max_filesize');
$this->upload_max_filesize = $this->convertFileSize($val);
}
public function getErrors() {
return $_FILES[$this->upload_file_name]['error'];
}
public function getFileSize() {
return $_FILES[$this->upload_file_name]['size'];
}
public function getFileName() {
if (!empty($this->new_filename)) {
$filename = $this->new_filename;
} else {
$filename = $_FILES[$this->upload_file_name]['name'];
}
return $filename;
}
public function getTmpName() {
return $_FILES[$this->upload_file_name]['tmp_name'];
}
public function setUploadDir($upload_dir) {
$success = 0;
trim($upload_dir);
if( $upload_dir[strlen($upload_dir)-1] != "/" ) {
$upload_dir .= "/"; // add trailing slash
}
$can_upload = false;
foreach( $this->allowable_upload_base_dirs as $dir ) {
if( $dir == $upload_dir ) {
$can_upload = true;
break;
}
}
if( !$can_upload ) {
if ($this->debug) {
echo "Cannot upload to the dir ->".$upload_dir;
}
}else{
if ($this->debug) {
echo "Successfully setting upload dir ->".$upload_dir;
}
$this->upload_dir = $upload_dir;
$success = 1;
}
return $success;
}
public function setTmpUploadDir($upload_tmp_dir) {
trim($upload_tmp_dir);
if( $upload_tmp_dir[strlen($upload_tmp_dir)-1] != "/" ) {
$upload_tmp_dir .= "/"; // add trailing slash
}
$can_upload = false;
foreach( $this->allowable_upload_base_dirs as $dir ) {
if( $dir == $upload_tmp_dir ) {
$can_upload = true;
return 0;
}
}
if( !$can_upload ) {
if ($this->debug) {
if ($this->debug) {
echo "Cannot upload to the dir ->".$upload_tmp_dir;
}
}
return 0;
}
$this->upload_tmp_dir = $upload_dir;
}
public function uploadFile() {
$result = 0;
if( $this->checkMaxMemorySizeLimit() ) {
if ($this->getFileSize() > 0) {
$this->last_msg = "File size of ".number_format($this->getFileSize()/1024)." KB greater than allowable limit of ".number_format($this->upload_max_filesize/1024) . " KB.";
if ($this->debug) {
echo $this->last_msg;
}
} else {
$this->last_msg = "File size is greater than allowable limit of ".number_format($this->upload_max_filesize/1024) . " KB.";
}
} else {
$this->temp_src_file = $this->upload_dir.$this->getFileName();
//echo "<br> temp = " . $this->temp_src_file . "<br>";
if (file_exists($this->temp_src_file)) unlink($this->temp_src_file);
if (!move_uploaded_file($this->getTmpName(), $this->temp_src_file)){
$this->last_msg = "ERROR: Unable to copy file from temp directory.";
die($this->last_msg);
} else {
$result = 1;
if ($this->debug) {
echo "move_uploaded_file OK"."<BR>";
}
}
}
return $result;
}
public function uploadFTPFile() {
$result = 0;
if( $this->checkMaxMemorySizeLimit() ) {
if ($this->getFileSize() > 0) {
$this->last_msg = "File size of ".number_format($this->getFileSize()/1024)." KB greater than allowable limit of ".number_format($this->upload_max_filesize/1024) . " KB.";
if ($this->debug) {
echo $this->last_msg;
}
} else {
$this->last_msg = "File size is greater than allowable limit of ".number_format($this->upload_max_filesize/1024) . " KB.";
}
} else {
$this->local_file = $this->upload_dir.$this->getFileName();
$this->remote_file = $this->getFileName();
$ftp_server = 'mis-server';
$ftp_user_name = 'dwh';
$ftp_user_pass = 'BAGDWH';
$ftp_dir = 'dwh_ext';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if ((!$conn_id) || (!$login_result)) {
$this->last_msg = "Connection to $ftp_server for user $ftp_user_name has failed!";
ftp_close($conn_id);
die($this->last_msg);
} else {
if (ftp_chdir($conn_id, $ftp_dir)) {
$res = ftp_size($conn_id, $this->remote_file);
if ($res != -1) {
ftp_delete($conn_id, $this->remote_file);
}
$upload = ftp_put($conn_id, $this->remote_file, $this->local_file, FTP_ASCII);
if (!$upload) {
$this->last_msg = "ERROR: Unable to copy file to server.";
unlink($this->local_file);
ftp_close($conn_id);
die($this->last_msg);
} else {
$result = 1;
unlink($this->local_file);
ftp_close($conn_id);
if ($this->debug) {
echo "move_uploaded_file OK"."<BR>";
}
}
} else {
$this->last_msg = "Access to $ftp_dir for user $ftp_user_name denied!";
ftp_close($conn_id);
die($this->last_msg);
}
}
}
return $result;
}
public function checkMaxMemorySizeLimit() {
if( $this->getFileSize() > $this->upload_max_filesize && $this->getFileSize() !== 0) {
return true;
}else{
return false;
}
}
public function checkAllowableExtension() {
$filename = basename($this->getFileName());
preg_match_all('/(\.\w+)/i',$filename,$matches);
$this->ori_ext = strtolower(end($matches[1]));
if ((in_array($this->ori_ext,$this->allowable_file_array)) && (array_key_exists($this->info['mime'], array_flip($this->mime_type_allowed)))) {
$result = true;
} else {
$result = false;
}
return $result;
}
public function checkFileExists(){
return file_exists($this->upload_dir.$this->getFileName());
}
public function setFilename($filename) {
$this->new_filename = $filename;
}
public function GetFTPFile () {
$result = 0;
$ftp_server = 'mis-server';
$ftp_user_name = 'dwh';
$ftp_user_pass = 'BAGDWH';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);
if ((!$conn_id) || (!$login_result)) {
$this->last_msg = "Connection to $ftp_server for user $ftp_user_name has failed!";
ftp_close($conn_id);
die($this->last_msg);
} else {
for ($d=1;$d<=2:$d++) {
if ($d==1) {
$ftp_dir = 'KWU';
} else {
$ftp_dir = 'L2R';
}
if (ftp_chdir($conn_id, $ftp_dir)) {
for ($i=1;$i<=3;$i++) {
if ($i == 1) {
$this->new_filename = 'LNTYPE.DBF';
} else if ($i == 2) {
$this->new_filename = 'LNCUST.DBF';
} else {
$this->new_filename = 'LNMAST.DBF';
}
$this->local_file = $this->getFileName();
$this->remote_file = $this->getFileName();
}
}
$res = ftp_size($conn_id, $this->remote_file);
if ($res != -1) {
ftp_delete($conn_id, $this->remote_file);
}
$upload = ftp_put($conn_id, $this->remote_file, $this->local_file, FTP_ASCII);
if (!$upload) {
$this->last_msg = "ERROR: Unable to copy file to server.";
unlink($this->local_file);
ftp_close($conn_id);
die($this->last_msg);
} else {
$result = 1;
unlink($this->local_file);
ftp_close($conn_id);
if ($this->debug) {
echo "move_uploaded_file OK"."<BR>";
}
}
} else {
$this->last_msg = "Access to $ftp_dir for user $ftp_user_name denied!";
ftp_close($conn_id);
die($this->last_msg);
}
}
return $result;
}
}