1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Using symlink() - can't get it to work properly

Discussion in 'PHP' started by PoPSiCLe, Oct 3, 2016.

  1. #1
    Okay. I'm building a gallery, where users can be logged in for uploading to their own folders, or just view the page via "public" (not logged in) - what I'm trying to do is make it possible to share uploads from the logged in user to the public folder, without having to duplicate files.

    For this, I'm trying to use symlink() - which works (in that the file I'm clicking "make public" on get a symlink created in the public folder - however, the link does not go back to the actual file in the user folder).

    I'm a little at a loss as to what I need to do.

    The code for the make_public is as follows:
    
    <?php
    if (!session_id()) { session_start(); };
    require_once('conf/config.php');
       $symbolic = (isset($_POST['filename']) ? $_POST['filename'] : '');
       if (!empty($symbolic)) {
         $username = ((isset($_POST['username']) && !empty($_POST['username'])) ? $_POST['username'].'/' : $username);
         $fullpath = $userpath.$username.'/'.$symbolic;
    
         $checkthumbs = explode('/',$symbolic);
         $checkthumbs[1] = ($checkthumbs[0] == 'video') ? $checkthumbs[1].'.jpg' : $checkthumbs[1];
         $thumbs = ($checkthumbs[0] == 'pictures' || $checkthumbs[0] == 'video') ? symlink($userpath.$username.$checkthumbs[0].'/thumbs/'.$checkthumbs[1],$userpath.'public/'.$checkthumbs[0].'/thumbs/'.$checkthumbs[1]) : false;
         symlink($userpath.$username.$symbolic,$userpath.'public/'.$symbolic);
         if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
           echo json_encode(["content"=>"File made public","infotype"=>"success"]);
         } else {
           header('location: gallery');
         }
       }
    ?>
    
    PHP:
    What happens is that the "target" (the first part of the symlink()), seems to use the "public/" folder for target, instead of the user-name-folder. Not really sure why, because if I echo the $username it shows the correct value.
    The link is created in the correct folder, but is not recognized as a link if I do is_link() or readlink() on it (returns bool(false) on var_dump()).

    Does anyone have any tips as to how I could do this correctly?

    As an example, the file being shared publicly, is an image - what would I need to show this as a regular image file in the public gallery?
     
    PoPSiCLe, Oct 3, 2016 IP
  2. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #2
    I'd echo out
    symlink($userpath.$username.$symbolic,$userpath.'public/'.$symbolic);
    symlink($userpath.$username.$checkthumbs[0].'/thumbs/'.$checkthumbs[1],$userpath.'public/'.$checkthumbs[0].'/thumbs/'.$checkthumbs[1]);
    
    Code (php):
    echo($userpath.$username.$symbolic,$userpath.'public/'.$symbolic);
    echo($userpath.$username.$checkthumbs[0].'/thumbs/'.$checkthumbs[1],$userpath.'public/'.$checkthumbs[0].'/thumbs/'.$checkthumbs[1])
    
    Code (php):
    to see if it's actually linking the file you want. Chances are you're creating a non existent symbolic link
     
    Einheijar, Oct 3, 2016 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    I'm not. The variables are populated (it's the exact same variables used for moving and deleting files, it's just moved to that file, and changed some variable-names). The return values for the two echo's are as follows:
    First echo:
    users/admin/pictures/01_06_03_060.jpg
    users/public/pictures/01_06_03_060.jpg
    Second echo:
    users/admin/pictures/thumbs/01_06_03_060.jpg
    users/public/pictures/thumbs/01_06_03_060.jpg

    Which is the exact placements. However, I'm a bit unsure if I need a full path - a symbolic link should be able to work with dynamic link, correct? Or do I need the full link ($_SERVER['DOCUMENT_ROOT'])?
     
    PoPSiCLe, Oct 3, 2016 IP
  4. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #4
    No harm trying out the full path. Which brings me to think, are you on a windows platform? If so you need to use absolute paths.
     
    Einheijar, Oct 3, 2016 IP
  5. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #5
    No, this is currently running on MAMP (OSX) - which might be why it's not working, of course :D Hard to find OSX-specific cases to read up on. But I will test full paths, see if it helps. It really shouldn't matter, though, as the test-server is set up as a vhost, and everything else works as intended.
     
    PoPSiCLe, Oct 3, 2016 IP
  6. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #6
    And, it was the absolute paths. Adding $_SERVER['DOCUMENT_ROOT'] fixed it.
     
    PoPSiCLe, Oct 3, 2016 IP
  7. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #7
    Why not use realpath? Also I'd suggest sanitizing user input
     
    Einheijar, Oct 4, 2016 IP
  8. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #8
    realpath have too many potential pitfalls on different systems. DOCUMENT ROOT seems to be working perfectly fine on any type of system I've bothered testing on thus far. Also, it would involve a rewrite of certain parts of the code, which I can't be arsed doing right now.
    As for user-input sanitation, I agree, need to work that in as well - although it's not really that much that can be done with this.
     
    PoPSiCLe, Oct 4, 2016 IP