File management classes
Moderator: General Moderators
File management classes
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.
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.
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?
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?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Hadn't thought about it... I guess that is kind of impossible huh?pickle wrote:How is someone going to go about uploading a folder? They need to select every file anyway.
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.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?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
here's the first round I see: four classes
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.
- upload class
- download class
- single file handling class
- folder handling class
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.
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.
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
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.
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
How could file and folder have the same interface... wouldn't you deal with both of these very differently?feyd wrote:here's the first round I see: four classesThe 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.
- upload class
- download class
- single file handling class
- folder handling class
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.
It just so happens I am on a file manager project myself..
file class:The interface specifies the various getters.
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;
}
}
?>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();
}
}
?>