Organizing uploads in year/month format

Discussion in 'PHP' started by News Updates, Jul 10, 2012.

  1. #1
    I need a little help. Im a newbie. I'm trying to create a php script for uploading files. Currently I'm using a static/fixed location for uploading. But I want to organize them according to year/ month folders. just like wordpress. here is the code I'm using

    $config['baseurl']  =  'http://mysite.com';
    $config['path'] = $config['basedir'].'/uploads';
    Code (markup):
    currently image path looks like: http://mysite.com/uploads/image.jpg

    but I want: http://mysite.com/uploads/2012/07/image.jpg

    I'll very appreciate if someone make me a little php code. Thanks
     
    Solved! View solution.
    News Updates, Jul 10, 2012 IP
  2. #2
    Well you can change the config path, but thats not handy if the script doesn't make the directory tree

    try this.
    
    $config['path'] = $config['basedir'].'/uploads';
    if (!is_dir($config['path'])) { mkdir($config['path']); }
    
    // add YEAR to the PATH 
    $config['path'] = $config['path'] . '/' . date("Y");
    if (!is_dir($config['path'])) { mkdir($config['path']); }
    
    // add MONTH to the PATH
    $config['path'] = $config['path'] . '/' . date("m");
    if (!is_dir($config['path'])) { mkdir($config['path']); }
    
    PHP:
    or something :)
     
    EricBruggema, Jul 11, 2012 IP
  3. News Updates

    News Updates Member

    Messages:
    490
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    35
    #3
    Thanks dude, sure I'd like to make polls at my blog using your site :)

    it really helped, and also please let me know will it be creating directories automatically if year or month changes?
     
    Last edited: Jul 11, 2012
    News Updates, Jul 11, 2012 IP