Jump to content

problem with writing filesize & filetype to database


motofzr1000

Recommended Posts

Hi,

 

I'm working on some home-made cms, in which I have to add a page in the back-end to upload files. I can manage some very basic php, but this one seems to be over my head.

 

At this moment I succeeded in getting the file on the server, and the file description and file name into the database, but I can't find how I can get the file size and the file type (extension) into the database.

 

The page which uploads the file, contains some included files, and I'm having problems with figuring out what I have to add, and where. I've tried a few dozen of things, but nothing works. I've included a zip file to this post with the needed files.

 

These are the fields in the DB table that I'm using:

 

pfile_id  int(11)

pfile_description text

pfile_date_created datetime

pfile_file varchar(255)

pfile_type varchar(30)

pfile_size int(11)

 

I guess it's pretty simple if you have some decent php/MySQL knowledge, but I can't figure it out.

Can anyone please help me with this?

 

cheers,

 

Bert

 

[attachment deleted by admin]

Problem is that the page which uploads the file, that it contains several included files, which contain functions etc. I've found the following functions in one of these pages, which should return the filesize and filetype, is this correct?

 

<?php
class files	{

var $root;
var $dir_to;

var $file_name;
var $file_temp_name;
var $file_size;
var $file_type;

var $dir_name;

/*
function dir_tree_array($start_of_tree,$level) {

			$dirs			= $this->get(array("browse" => $start_of_tree));

			$i			= 0;
			if (empty($return)) { $return = array(); }
			if (empty($level)) { $level = 0; }
			$level++;

			foreach ($dirs as $dir) {

				if ($dir['type'] == "dir") {

					$return[$i]['name']		= $dir['name'];

					if (is_dir($this->root . $start_of_tree . $dir['name']. "/")) {

						$return[$i]['sub_dir'] = $this->dir_tree_array($start_of_tree . $dir, $level);
					}

					$i++;	
				}
			}


		return (sizeof($return) == 0) ? null : $return;
	}
*/

function get($param) {

	$return				= array();
	if ($param['browse']) { if (substr($param['browse'], -1) != "/") { $param['browse'] = $param['browse']."/"; } }
	$read				= opendir($this->root.$param['browse']);
	$i					= 0;

	while ($get = readdir($read)) {

		if (!(($get == '..') || ($get == '.'))) {

				$return[$i]['type'] 	= filetype($this->root.$param['browse'].$get);
				$return[$i]['name'] 	= $get;
				$return[$i]['mtime'] 	= filemtime($this->root.$param['browse'].$get);

				if (filetype($this->root.$param['browse'].$get) == "file") {
				$return[$i]['size'] 	= filesize($this->root.$param['browse'].$get);
				$return[$i]['ext'] 		= end(explode('.',$this->root.$param['browse'].$get));
				}		

				$return[$i]['browse'] 	= $param['browse'];

			$i++;

		}

	}

	closedir($read);
	return $return;
}

function files() {
}

function upload($if_file_exists=NULL) {

	if (is_uploaded_file($this->file_temp_name)) {

		if ($if_file_exists) {
			$this->if_file_exists();
		}

		$file = $this->root.$this->dir_to.$this->file_name;
		move_uploaded_file($this->file_temp_name,$file);

	}

	return $file;
}

function if_file_exists($copy_of=NULL) {

	if (empty($copy_of)) { $copy_of = "copy_of_"; }
	if (file_exists($this->root.$this->dir_to.$this->file_name)) {

		$this->file_name = $copy_of.$this->file_name;
		$this->if_file_exists();

	}
}

function delete_file() {

	if (file_exists($this->root.$this->dir_to.$this->file_name)) {

		//chmod($this->root.$this->dir_to.$this->file_name, 777);
		unlink($this->root.$this->dir_to.$this->file_name);

	}

}

function add_dir() {

	mkdir($this->root.$this->dir_to.$this->dir_name);

}






}
?>

 

 

In the page where the upload form is located, there's this code to write the info to the database, but I'm only getting the filename:

 

	case "add_single":

	$pfile 						= $_POST['pfile'];

	if (is_uploaded_file($_FILES['pfile']['tmp_name']['pfile_file'])) {

		$files = new files(); 
		$files->root = $client['path']['absolute']."PDF/Test_PDF/";

		$files->file_name			= $_FILES['pfile']['name']['pfile_file'];
		$files->file_temp_name		= $_FILES['pfile']['tmp_name']['pfile_file'];
		$files->file_size			= $_FILES['pfile']['size']['pfile_size'];
		$files->file_type			= $_FILES['pfile']['type']['pfile_type'];
		$files->dir_to				= "";
		$files->if_file_exists();
		$files->upload();
		$pfile['pfile_file']		= $files->file_name;
		$pfile['pfile_size']		= $files->file_size;
		$pfile['pfile_type']		= $files->file_type;
	}

	$mysql->data_add_single("db_productfiles",$pfile);
	header("location: productfiles2.php");

 

 

If someone could help, I could do some design work in return, because programming isn't really my thing...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.