note 54258 added to features.file-upload
| From: | suri at suribaladotcom | Date: | Tue, 28 Jun 2005 16:31:04 +0000 |
| Subject: | note 54258 added to features.file-upload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91097@lists.php.net to get a copy of this message | ||
A geneic fileupload class is included here....
<?php
// author: Suri Bala
// freely distributable
class fileupload{
private $upload_tmp_dir = "/tmp/"; // leading and trailing slash required
private $file_upload_flag = "off";
private $upload_max_filesize = "100";
private $allowable_upload_base_dirs = array("/tmp/",
"/web/dynawolf/uploads/");
private $allowable_upload_tmp_dirs = array( "/tmp/");
private $upload_dir= "/tmp/"; // leading and trailing slash required
private $upload_file_name;
function __construct($name) {
if( is_null($_FILES[$name]) ) {
echo "Specified file <strong> ".$name." </strong> does not exist
in the FILES array. Please check if it exists";
echo "Exiting...";
exit;
}
$this->getConfigurationSettings();
if( $this->file_upload_flag == "off" ) {
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');
$this->upload_max_filesize = ini_get('upload_max_filesize');
$this->upload_max_filesize = preg_replace('/M/', '000000',
$this->upload_max_filesize);
}
public function getErrors() {
return $_FILES[$this->upload_file_name]['error'];
}
public function getFileSize() {
return $_FILES[$this->upload_file_name]['size'];
}
public function getFileName() {
return $_FILES[$this->upload_file_name]['name'];
}
public function getTmpName() {
return $_FILES[$this->upload_file_name]['tmp_name'];
}
public function setUploadDir($upload_dir) {
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 ) {
echo "Cannot upload to the dir ->".$upload_dir;
return;
}else{
$this->upload_dir = $upload_dir;
echo $this->upload_dir;
}
}
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;
}
}
if( !$can_upload ) {
echo "Cannot upload to the dir ->".$uplaod_tmp_dir;
return;
}
$this->upload_tmp_dir = $upload_dir;
}
public function uploadFile() {
if( $this->checkMaxMemorySizeLimit() ) {
echo "File size of ".$this->getFileSize()." greater than allowable limit of
".$this->upload_max_filesize."Please change the configuration setting.";
return;
}else{
if( !move_uploaded_file($this->getTmpName(), $this->upload_dir.$this->getFileName())
) {
echo "Failed to upload file ".$this->getTmpName();
}
}
}
public function checkMaxMemorySizeLimit() {
if( $this->getFileSize() > $this->upload_max_filesize ) {
return true;
}else{
return false;
}
}
}
----
Manual Page -- http://www.php.net/manual/en/features.file-upload.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54258
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54258&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54258&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54258&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54258&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54258&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54258&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54258&report=yes
Search -- http://master.php.net/manage/user-notes.php