tags:

views:

73

answers:

2

I have a code like this below, which gives me a $link that equals to: http://mydomain.com/image/photo.JPG

if (!empty($_SESSION['item'][$i]))
{
    $path = dirname(__FILE__).'/image/'.basename($_SESSION['item'][$i]);
    if (move_uploaded_file($_SESSION['item'][$i], $path)) 
        global $url;

    if ( $url )
    {
        $link = $url.'/image/'.basename($_SESSION['item'][$i]);
        echo "<td><img src='" . $link . "' width='50' height='50' /></td>";
    }
}

I wonder, how should I properly ommit using global variable here to achieve the same result.

UPDATE

if (move_uploaded_file($_SESSION['item'][$i], $path))
{
    $link = 'image/'.basename($_SESSION['item'][$i]);  
    echo "<td><img src='" . $link . "' /></td>";
}

This modification doesn't show any letter. It seems that if block doesn't execute.

+1  A: 

yes, you can

$link = '/image/'.basename($_SESSION['item'][$i]); 

But your code is damn weird. Took me a hour to understand

Col. Shrapnel
+2  A: 

Well why are you using global in the first place? Its not apparent from the snippet you provided as there is no reason to use it in this code alone.

If its in a function pass $url as an argument... if its something else then we will need the details of usage.

In regards to Col. Shrapnel's answer if you omit the first / youll get a link relative to the page accessed. Which as far as i can tell is what youre after:

$link = 'image/'.basename($_SESSION['item'][$i]);

prodigitalson
i doobt that using repative path is a good idea
Col. Shrapnel
Eh? Reptitive how? `$url` i assume is the url accessed so if he wants images to be accessed realtive to that url he needs to omit your first slash - on the other hand if images is at the document root and thats where things are going then what you provided was fine. But im not sure how either is repetitive - care to elaborate?
prodigitalson
Oh wait i bet that was a typo and you meant "relative". and yeah I have to agree :-)
prodigitalson
It is not inside any function. I just need full url, not relative path. This is an example i've found somewhere on the net. I'am a very beginner in PHP language, and yes, i assume it's a weird code.
dygi
So what is `$url` supposed to be? and where are you getting its value from?
prodigitalson
Nah, the weird and simple answer is: nowhere. See an __UPDATE__ in the post please.
dygi