Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Path Variable

XarcellXarcell North Carolina
edited July 2012 in Vanilla 2.0 - 2.8

I'm new to vanilla and not very good with php. I'm trying to create a plugin. My question for now is how do I proper the proper absolute path? Is a variable used, or something else?

For example how do I get the:
/home/public_html/blah/blah/blah/

Is something similar to this used?
$path/blah/blah/blah/

Comments

  • dirname(__FILE__) is the absolute path of the directory you are currently in, or the directory that the current script is executing from to be precise.

    so you can do

    include_once(dirname(__FILE__).DS.'class.somemodel.php');

    you also have various cosntants in Garden

    PATH_ROOT 
    PATH_APPLICATIONS
    PATH_CACHE
    PATH_LIBRARY
    PATH_PLUGINS
    PATH_THEMES
    PATH_UPLOADS
    

    DS is the director separator.

    grep is your friend.

  • XarcellXarcell North Carolina

    I'm trying to wrap my head around what your saying. I understand half of it. However, I don't need to point to a file(include), I need to point to a directory where files are to be deposited.

    Example:
    // files storage folder
    $dir = '/home/web/sitecom/plugin/images/';

    So would it be something like this?
    define('PATH_ROOT' . 'plugin/images/') = $dir;

    FYI, I cannot get this stupid editor to display code properly.

  • I would take a more general course in php, so you get a better overall understanding. Also any C based language will help.

    if the script is already in the directory you can use

    define('MY_IMG_DIR' , dirname(__FILE__).DS.'images');

    or

    define('MY_IMG_DIR' , PATH_PLUGINS.DS.'MyPlugin'.DS.'images');

    grep is your friend.

  • peregrineperegrine MVP
    edited July 2012

    define is just setting a CONSTANT rather than a variable (which can change)

    http://php.net/manual/en/function.define.php

    so when you use it it,

    after you define it.

    you don't use the $ as you would in $my_img_dir

    you would use MY_IMG_DIR

    as in echo MY_IMG_DIR;

    vs a variable

    echo $my_img_dir;

    and you would want a relative path not an absolute path if you plan on uploading the plugin so other can use.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • XarcellXarcell North Carolina

    I'm just trying to get this script to work as part of the editor plugin.

    <?php
    
    // This is a simplified example, which doesn't cover security of uploaded images. 
    // This example just demonstrate the logic behind the process. 
    
    
    // files storage folder
    $dir = '/home/web/sitecom/redactor/images/';
    
    $_FILES['file']['type'] = strtolower($_FILES['file']['type']);
    
    if ($_FILES['file']['type'] == 'image/png' 
    || $_FILES['file']['type'] == 'image/jpg' 
    || $_FILES['file']['type'] == 'image/gif' 
    || $_FILES['file']['type'] == 'image/jpeg'
    || $_FILES['file']['type'] == 'image/pjpeg')
    {   
        // setting file's mysterious name
        $file = $dir.md5(date('YmdHis')).'.jpg';
    
        // copying
        copy($_FILES['file']['tmp_name'], $file);
    
        // displaying file
        echo '<img src="/tmp/images/'.$file.'" />';
    }
    
    ?>
    
Sign In or Register to comment.