SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a library to deal with files?

    Hi,
    I'm looking for a library that would deal with files, and especially with file upload (check MIME type, check file size, CHMOD etc.) You see what wideImage lets you do with images, how user friendly it is? Well, I'd like something in the same vein, but for files. Any recommandations?


  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the think with files is there's so many different ways to act upon it. Sometimes you need to validate the format, maybe the mime type, file size, sometimes default permissions are okay, sometimes they aren't. Weird subtle things like that can make it hard to abstract out.

    Also with images, you don't want to check the mime type, you want to use something like exif to check the actual headers, because I can easily upload a javascript file and tell the server it's an image/gif, server is just going to pass it on unless you have a custom setup of some sort.

  3. #3
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the info but this isn't helpful at all I am just looking for a library. As I said, there must be plenty of interesting projects, maybe developped by SPF gurus (like wideImage).
    Could my thread be sent back to the advanced php forum as we could discuss there the various approaches to such a subject?

  4. #4
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jjshell View Post
    Thank you for the info but this isn't helpful at all I am just looking for a library. As I said, there must be plenty of interesting projects, maybe developped by SPF gurus (like wideImage).
    Could my thread be sent back to the advanced php forum as we could discuss there the various approaches to such a subject?
    o_O...

    Let me confirm this, you asked for a file upload library, I give specific examples on why it would be difficult to abstract into something generic, you claim it wasn't helpful without stating specific arguments as to why you think stated points won't matter, and you want me to continue the discussion?

    I'm sorry, but if this is the way you plan to respond to things I say, I'm not prompted to continue anything here... If you'd like to point out specific issues with my response, I'll be glad to respond back. Otherwise, this is as much effort as I plan to put into this thread.

  5. #5
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris_fuel View Post
    o_O...
    Otherwise, this is as much effort as I plan to put into this thread.
    I think you won't like what I'm going to say, but it's fine with me. Don't misunderstand me. Your first post was fine, it was just out of topic and didn't answer my question at all Unless of course I did not understand you and that what you were actually saying, is that there is no such library, and that none could be created that would fit the needs of the masses because of the forementionned arguments. In this case, your post makes sense and, unless someone has something to add, I'll consider that this is a mad project I won't try to go into and I will create objects handling file uploads on a case-to-case basis.

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I made a set of uploader classes not so long ago.

    Long enough ago though that I know they aren't written terribly well. ( I am learning, see?)

    I am learning about Patterns by using them when I can, I mean I actively seek for a reason to use a pattern just so I know ( not read about, or have a hunch about ), like really know what that pattern is good for and what they are bad for, hidden maintenance costs etc.

    Anyhow, back to your uploader.

    I used something called the TemplateMethod to make an uploader, and I submitted it to one of the PHP Pattern sites - to no avail, they did not use it.

    Still, if you'd like the code please PM me. I have used it in several projects and apart from the weird error handling and lack of Exceptions, it works really nicely on win32 and *nix ( though doesn't deal with CHMOD issues ).

    It contains simpleTest unit tests, though they too show my na´vetÚ.

    Attached is the readme.txt I was going to offer as an article introducing the Uploader classes using the TemplateMethod.
    Attached Files Attached Files

  7. #7
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups. It looks really interesting. I'd be interesting in the code. What licence is it released under?

  8. #8
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Cups, remember to add a namespace to your component to make sure it doesn't clash with others. And also, use one underscore before the method name, methods with 2 underscores at the beginning are used by the php dev-team.

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by jjshell View Post
    Hi Cups. It looks really interesting. I'd be interesting in the code. What licence is it released under?
    Er, Ok let me admit some ignorance here, how about the "Here's some code I hope it helps you, but if you find you can improve it then let me know, and if you do decide to release it somewhere, it'd be nice if you said I gave you some ideas license."

    What licence would that be?

    I mean I was planning to donate it to a PHP Patterns site, so what do you suggest? I am not precious about it all because I know how much it needs so much tidying up. I have learned so much about PPP and Exceptions etc (mostly here and in Dagfin's book)- I just don't have time to refactor it again.

    Quote Originally Posted by php_impact
    Hi Cups, remember to add a namespace to your component to make sure it doesn't clash with others. And also, use one underscore before the method name, methods with 2 underscores at the beginning are used by the php dev-team.
    So a good example of a namespace would be what?

    Something like:
    PHP Code:
    class Cups_Uploader {} 
    The double underscore was my typo error on the readme, I am sorry about that.

  10. #10
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry to wake up an old thread, but I was actually looking for something exactly like this, but I couldn't find anything, so I started writing a couple of classes myself.

    The interface looks something like this at the moment (altough I'm not finished with it).

    PHP Code:
    <?php
    $mgr 
    = new nc_File_Upload_Manager$_FILES );
    $mgr->setTargetDir'/path/to/upload/dir' );

    // We want to handle all images, and all MS Word documents
    $mgr->addAllowedMime( new nc_File_ImageMimes() );
    $mgr->addAllowedMime'application/msword' );

    // We want all images, except bitmaps
    $mgr->addUnallowedMime'image/bmp' );

    // Add an observer that resizes the images
    $resize = new Some_Resize_Class();
    $resize->setMaxHeight100 );
    $observer = new nc_File_Upload_ImageResizeObserver$resize );

    // An observer can be attached to a mime like this:
    $mgr->addObserver$observer'image/jpeg''after' );

    // Or:
    $mgr->addObserver$observer, Array( 'image/jpeg''image/png' ), 'after' );

    // Or, to attach it to all web images and 'image/bmp':
    $mgr->addObserver$observer, new nc_File_WebImageMimes'image/bmp' ), 'after' );

    // Or even:
    $mgr->addObserver$observer, new nc_File_WebImageMimes( new nc_File_DocumentMimes() ), 'after' );

    try
    {
        
    $file $mgr->upload'upload_field' );

        
    // Or, if you wish to specify the name yourself
        
    $file $mgr->upload'upload_field''myfile.jpg' );

        
    // Or, if you just want to upload all files
        
    $files $mgr->uploadAll();

        
    // Or, if you just want to upload all files when you have named your inputs 
        // something like 'upload_field[]'
        
    $files $mgr->uploadAll'upload_field' );
    }
    catch( 
    nc_File_Upload_Exception $e )
    {
        echo 
    $e->getMessage();
        exit;
    }
    ?>
    Some explainations:
    Mime checking is done by first checking if the extension fileinfo is availible. If it isn't, a shell command is executed to get the correct mime (only works on *nix), and if the two above fails, it relys on the mime set in $_FILES, which isn't that good.

    The observers can be attached to be executed either before the file is moved (when it's in the temp folder) or after it's been moved. An observer just needs to implement the nc_File_Upload_iObserver interface to be attached. An observer can also throw an exception which cancels the upload and removes the file.

    The classes like nc_File_WebImageMimes are just wrappers around common mime types, so you don't have to type all of them, but you still have to control to add/remove mime types in these collections.


    It's not by far finshed yet, and I'm still working on the unit tests, but if anyone is interested, I'd be happy to share the code. And also, if anyone has any feedback on the interface, I'd be happy to hear it!
    Last edited by wysiwyg; Apr 19, 2008 at 05:39.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •