Page 1 of 1

File management classes

Posted: Mon Jul 31, 2006 3:24 pm
by Luke
I am looking for suggestions on how to break up a giant upload/download class into several smaller classes. If I want classes to be able to upload/download files and entire directories... what would be the best way to set up my classes... I have considered the following so far:

file class with upload/download (and other operations such as rename) methods/children
folder class with upload/download (and other operations such as rename) methods/children

OR

Upload class with file/folder methods
Downloac class with file/folder methods

The problem is that in either of these, it seems like I am being redundant and inefficient. Suggestions are welcome.

Posted: Mon Jul 31, 2006 3:40 pm
by pickle
How is someone going to go about uploading a folder? They need to select every file anyway.

Why do you have a download class? What's the point of it - to show a list of files, or to mimic the files that are off the web-root & make them available?

Posted: Mon Jul 31, 2006 3:42 pm
by Luke
pickle wrote:How is someone going to go about uploading a folder? They need to select every file anyway.
Hadn't thought about it... I guess that is kind of impossible huh?
pickle wrote:Why do you have a download class? What's the point of it - to show a list of files, or to mimic the files that are off the web-root & make them available?
I don't actually have any classes yet. This is just what I have been kind of tossing around in my head. Download class will download files from the web root.

Posted: Mon Jul 31, 2006 3:45 pm
by feyd
here's the first round I see: four classes
  1. upload class
  2. download class
  3. single file handling class
  4. folder handling class
The file and folder classes would have the same interface so they are as interchangeable as possible. The upload and download classes accept a reference to a file or folder class either during creation (if the class stays in this state for its lifespan,) otherwise it goes to the method to start the action.

Now, I can see further class potential, depending on how you categorize some things. For instance, I have storage controllers that can send data to the file system, database, or a variety of other media to store data onto. The upload class could use that to make storing to places more simple. The download class could use them as ways of retrieving the data to send to the user. Under this idea the structure would be upload/download > storage > file/folder handling, I believe.

I only thought about this momentarily, so keep that in mind.

Posted: Mon Jul 31, 2006 3:46 pm
by Luke
makes sense to me... thanks. I will sketch out some classes and post them.

Posted: Mon Jul 31, 2006 3:47 pm
by pickle
Heh - ok so folder uploading is gone (I'm assuming).

How would you imagine this class is used in a download situation? If I wanted to use this class to allow users to download mycoolinvention.zip, how would it work? Would this class output header("Content-type: application/zip"); and channel the file, or would it generate a link to it?

How would this class work for downloading a folder? Would it generate a list of files to download or would it zip them all together & send that to the user?



Not meaning to pick it apart, I just want to understand what you want to do with this before I give any advice.

Posted: Mon Jul 31, 2006 3:49 pm
by Luke
I would like to give the option to download a file, download a zipped file, list files in a folder for download, or download a zipped folder

Posted: Mon Jul 31, 2006 3:55 pm
by pickle
I'd have two classes. The main File() class can handle the forcing the download. It can also handle zipping the file/folder (basically the filesystem path) sent to it. You could have a second class Folder(), which also has a listUp() function.

If you're looking to really break it up, I guess you could go as far as having Download(), Zip(), and List() classes - though personally, that strikes me as a bit of overkill.

Posted: Mon Jul 31, 2006 4:04 pm
by Luke
feyd wrote:here's the first round I see: four classes
  1. upload class
  2. download class
  3. single file handling class
  4. folder handling class
The file and folder classes would have the same interface so they are as interchangeable as possible. The upload and download classes accept a reference to a file or folder class either during creation (if the class stays in this state for its lifespan,) otherwise it goes to the method to start the action.

Now, I can see further class potential, depending on how you categorize some things. For instance, I have storage controllers that can send data to the file system, database, or a variety of other media to store data onto. The upload class could use that to make storing to places more simple. The download class could use them as ways of retrieving the data to send to the user. Under this idea the structure would be upload/download > storage > file/folder handling, I believe.

I only thought about this momentarily, so keep that in mind.
How could file and folder have the same interface... wouldn't you deal with both of these very differently?

Posted: Mon Jul 31, 2006 4:44 pm
by Jenk
It just so happens I am on a file manager project myself..

file class:

Code: Select all

<?php

class jmt_File implements iFile
{
	private $size;
	private $path;
	private $type;
	private $name;
	
	public function __construct ($file, $path)
	{
		if (!file_exists($fullpath = realpath($path . '/' . $file))) {
			throw new FileNotFoundException ('File path given does not exist.');
		} 
		
		$this->path = $fullpath;
		$this->name = $file;
		
		$this->readProperties();
	}
	
	private function readProperties ()
	{
		if (function_exists('mime_content_type')) {
			$this->type = mime_content_type($this->path);
		} else {
			$this->type = 'application/octet-stream';
		}
		
		$this->size = filesize($this->path);
	}
		
	public function getSize ()
	{
		return $this->size;
	}
	
	public function getType ()
	{
		return $this->type;
	}
	
	public function getName ()
	{
		return $this->name;
	}
	
	public function getPath ()
	{
		return $this->path;
	}
}

?>
The interface specifies the various getters.

Posted: Mon Jul 31, 2006 5:00 pm
by Luke
Here's what I have so far for a file class... kind of just throwing it together right now, so I'm very interested in opinions.

Code: Select all

<?php
class File{
	private $data; // Contents of file
	private $path; // Path to file
	private $name; // Name of file
	private $wholename;
	private $handle; // File handle
	function __construct($path, $create_non_existing=false){
		$this->wholename 	= realpath($path);
		$path_info 			= pathinfo($this->wholename);
		$this->path 		= $path_info['dirname'];
		$this->name 		= $path_info['basename'];
		$this->extension 	= $path_info['extension'];
		$this->size 		= $this->getSize();
	}
	
	public function read($length=false){
		$length = $length ? $length : $this->size;
		$this->handle = $this->setHandle('r');
		return fread($this->handle, $length);
	}
	
	public function write($content){
		$length = $length ? $length : $this->size;
		$this->handle = $this->setHandle('w');
		return fwrite($this->handle, $content);
	}
	
	public function append($content){
		$this->handle = $this->setHandle('a');
		fwrite($this->handle, $content);
	}
	
	public function rename($new_name){
		return @rename($this->wholename, $this->path . $new_name);
	}
	
	private function setHandle($operation){
		$this->close();
		return fopen($this->wholename, $operation);
	}
	
	public function getSize(){
		return filesize($this->wholename);
	}
	
	public function close(){
		if($this->handle) fclose($this->handle);
	}
	
	public function __destruct(){
		$this->close();
	}
}
?>