upload wrapper class
I have many kind of objects: categories, manufacturers, items, articles etc. Each object have it's file uploads: images, manuals in pdf etc. Uploaded files co开发者_Python百科uld be public and protected (available for view/download only for registered users).
I'd like to wrap whole upload routine into one (or more) class.
At the moment I have 2 defined constants at index.php (PATH_UPLOAD_PUBLIC
, PATH_UPLOAD_PROTECTED
). It is absolute paths to forders where I store public and protected files. Public path is avaiable from web, protected path is not available from web (it is under virtual host document root).
At the moment each controller's method dealing with upload have piece of code containing move_uploaded_file() an so on.
Please help to make it more beatiful and to separate this code into the class.
Thank you.
PS: Sorry for my English.
If you are looking for examples of how to create a class that deals with upload, then go to http://www.phpclasses.org and search for file upload class.
If you want to play around and create your own. I would suggest to start simple. Create a construct, and then a method which basically would use one parameter public, or private depending where you want to move the file. In this method you put you move_upload_file logic).
So you end up having something like this:
$fileupload - new MyFileUploadClass(); //you class would have to have _-construct obvioulsy
$fileupload->uploadfile(true); // you uploadfile would have one parameter being true or false and then
you method would look something like this
function uploadfile($private=false){
$path = PATH_UPLOAD_PUBLIC;
if($private)
$path = PATH_UPLOAD_PRIVATE;
move_upload_file($_FILE[name], $path);
}
精彩评论