P.S i know title sucks but i couldn't think of an appropriate title to explain this:(
Well i tried to think over this matter for days but couldn't found the logic of how it is done, so i guess only you guys can help me understand this

some of you may have notice that some image hosting give different version of uploaded images

Examples:
http://www.site.com/images/picname.png -->This is direct link to image
http://www.site.com/view/picname.png --->This one IS EXACTLY the page you get when u just upload an image(i.e other links like bb code displayed + preview of image).Thus if i put advertisements on this page , they will appear on every page

My question is how is this done in php
Some points to be noted
1.Image is not uploaded 2 times, wastage of memory and space so not feasible
2.It not temporary , like i can change picname.png to any picture name stored on server and it will load exactly as if i just uploaded the image and show me the directly link to image + other versions as well

How is this done , help me understand
Want to implement something similar:D

view/picname.png is either a htaccess rewrite to another PHP script showing the picture in it's page, or view is the actual script and the picture is the parameter.

not htaccess redirect for sure as view/picname.png would IS not redirected to images.picname.png
view is Not the script as well as after upload url become site.com/upload.php
view/picname.png is the exact copy of upload.php @time of upload

not htaccess redirect for sure as view/picname.png would IS not redirected to images.picname.png

It doesn't have to be a visible redirection (to you).

Can u show an example of a hidden redirection
Kind of loss here

Member Avatar for diafol

Rewrite is not the same as redirect. Will post an example later unless somebody else does so first.

I am waiting for your example diafol dude
You rock

Also in case you allow me , can i pm me a site url that uses the system that i am talking about:)

The site is probably using something like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule view/(.*) images/$1 [L]

No Guys i think you are not understanding what i mean
Here is a live example
http://lulzimg.com/view/45b0e107d6.jpg
Click on it and check for yourself
This way the guy can put ads on the links that have "/view/"
Direct link is http://i3.lulzimg.com/45b0e107d6.jpg
Huge difference:P
(ignore i3 in direct link)
I want a similar structure for my site
Peace and thanks for showing interest

It would have been helpful if you could have posted the link to the actual example earlier.

So instead of what I posted before, you could use:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule view/(.*) view.php?image=$1 [L]

Where view.php contains:

<?php
$image = isset($_GET['image']) ? $_GET['image'] : false;

// Check image exists
if(! file_exists("images/{$image}")) {
    // Return 404 - image doesn't exist
}
?>
<html>
    <head></head>
    <body>
        <!-- various page elements -->

        <img src="images/<?php echo $image; ?>" alt="Image title" />

        <!-- various other page elements -->
    </body>
</html>

@blocblue
Thanks so much for quick response

Where do i have to put this , in my parent directory or in images folder or in "view" folder.Ofc i know its a .htaccess file but i get 500 internal server error.Most probably i am putting in wrong location

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule view/(.*) view.php?image=$1 [L]

Take note that view is A directory in the example
Shall i create a folder "view" and then put the code below(i.e in index.php in view folder

<?php
$image = isset($_GET['image']) ? $_GET['image'] : false;
// Check image exists
if(! file_exists("images/{$image}")) {
    // Return 404 - image doesn't exist
}
?>
<html>
    <head></head>
    <body>
        <!-- various page elements -->
        <img src="images/<?php echo $image; ?>" alt="Image title" />
        <!-- various other page elements -->
    </body>
</html>

Please clarify your response
Much appreciated
Peace

If you haven't already, you will need to enable the Rewrite Engine and set the Rewrite Base - see below. If this still fails, please post the relevant errors from your error log.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule view/(.*) view.php?image=$1 [L]

The PHP file, view.php, should be placed in the root directory of your website. The appearance of the view sub-directory is achieved by the rewrite rule.

Thanks a ton for the clarification
500 internal error solved
However when i tried
http://www.site.com/view/picname.jpg , picname.jpg(which is in "images" folder) does not load
Thanks to your support

Do you have a publicly accessible example?

You say picname.jpg doesn't load. Does it 404?

Can you outline the directory and file structure for your website?

Can you post your code?

Does it 404?
Well i don't get 404 not found message but i get the icon of a corrupted image
Like this
http://gyazo.com/783ff1f52d0d3dfad7ba21b534c92916
Outline of directory structure
Images are stored in "images" folder
I am using appserv on windows atm
Can i post my code?
No its not possible coz i will be using for production.However i can let you view it via teamviewer

A screenshot isn't helpful. It doesn't show any source code.

If you're seeking help on a public forum, you're going to need to post some code. Otherwise we can only speculate as to the issue.

If you want private help, I'd suggest you hire a developer.

The view.php is exactly as you given me above
I haven't added any other page element
Once image get loaded , i will add others
html source

<html>

    <head></head>

    <body>

        <!-- various page elements -->

        <img src="images/" alt="Image title" />

        <!-- various other page elements -->

    </body>

</html>

How i am doing things?
suppose i want to view a pic like blocbluerocks.jpg
i Enter
http://localhost/fileupload/view/images/blocbluerocks.jpg as url and blocbluerocks.jpg should load
then later i can add other page elements

appserv on windows

note .htaccess doesn't work on IIS, it's an apache thing. Had me hating htaccess for months until i realised why i could never get it to work

Web Server: Application Server installs Internet Information Services (IIS) 7.0

appserv -> http://technet.microsoft.com/library/0f51315e-de4b-491f-8536-9aaea03ed73d

IIS not support htaccess -> http://stackoverflow.com/questions/257936/htaccess-or-htpasswd-equivalent-on-iis

in that case i'll facepalm and leave it to blocblue

The content of view.php should be as follows. Your snippet is missing all PHP source code.

<?php
$image = isset($_GET['image']) ? $_GET['image'] : false;
// Check image exists
if(! file_exists("images/{$image}")) {
    // Return 404 - image doesn't exist
}
?>
<html>
    <head></head>
    <body>
        <!-- various page elements -->
        <img src="images/<?php echo $image; ?>" alt="Image title" />
        <!-- various other page elements -->
    </body>
</html>

Also, the redirect rule is set to work when visiting the URL:
http://site.com/view/example.jpg
NOT
http://site.com/view/images/example.jpg

As the latter would rewrite the image URL to be:
http://site.com/images/images/example.jpg

If you post details outlining your directory struture, that would help. As would knowing the source URL for the missing image.

So can you confirm if your directory structure is:

/
    fileupload/
        .htaccess
        view.php
        images/
            example.jpg

Can you also confirm that your view.php script matches what I have in my last post?

And you said that when viewing http://site.com/images/example.jpg the image didn't display. If you view your source code, what is the value of the image source attribute?

yes i confirm my directory structure is same as you shown above
My view.php is EXACTLY like yours
I repeat dude everything is same as you posted , so the question of a different view.php doesn't arise
I wonder what is wrong

Still waiting for this...

And you said that when viewing http://site.com/images/example.jpg the image didn't display. If you view your source code, what is the value of the image source attribute?

I think either me or you are confused:D
http://site.com/images/example.jpg
Loads fines , it just show the image and that's all(Perfect) - No source here , just an image,No problem @all:D

When i view source of
http://site.com/view/example.jpg( That's the problem)
i get

<html>
    <head></head>
    <body>
        <!-- various page elements -->
        <img src="images/" alt="Image title" />
        <!-- various other page elements -->
    </body>
</html>

This implies that it is the source of view.php
Now the source of view.php is exactly as yours

Read below , that's all what i need to do
Problem is that example.jpg Don't enter the

<img src="images/" alt="Image title" />
To become

<img src="images/example.jpg" alt="Image title" />
I appreciate the amount of time you are putting for this
i struggle with this feature for days

Are you sure that the first line in view is good
What is needed here is as follows
Suppose user navigates
http://site.com/view/example.jpg
The view.php extract part of url after /view/(i.e example.jpg)
Then stores it in a variable($image in our case)
Then echo it in <img src=/> tags
Done

I'm not confused. I am trying to persevere and find the issue without being able to see the complete source code.

And to be fair, it's like getting blood from a stone with answers to questions.

Yes, the value of $image isn't being output. That is why I wanted to know what the value of the src attribute was so that I could ascertain whether you had made a typo when posting the code previously (you posted the rendered code, not the original source code).

So, the question is - why is $image empty?

Can you add the following to the top of view.php:

echo '<pre>'; var_dump($_GET); echo '</pre>'; die;

And also post the entire content of the .htaccess file again...

Full View.php(Its my full source code dude)

<?php
$image = isset($_GET['image']) ? $_GET['image'] : false;
echo '<pre>'; var_dump($_GET); echo '</pre>'; die;
// Check image exists
if(! file_exists("images/{$image}")) {
    // Return 404 - image doesn't exist
}
?>
<html>
    <head></head>
    <body>
        <!-- various page elements -->
        <img src="images/<?php echo $image; ?>" alt="Image title" />
        <!-- various other page elements -->
    </body>
</html>

Full .htaccess

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule view/(.*) view.php?image=$1 [L]

Result of

echo '<pre>'; var_dump($_GET); echo '</pre>'; die;

array(0) {
}

Okay, so we've established that the $_GET parameter isn't being passed through.

I've had a go at recreating this issue in my own development environment and encountered the same problem when when I had MultiViews enabled.

The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements. http://serverfault.com/questions/60/mod-rewrite-does-not-forward-get-parameters

To get around the problem, I had to modify the .htaccess file as follows:

RewriteEngine On
Options -MultiViews

RewriteBase /fileupload/
RewriteRule ^view/(.*)$ view.php?image=$1 [L]

I also changed the image source in the view.php file to make it especially explicity as follows:

<?php

$image = isset($_GET['image']) ? $_GET['image'] : false;

// Check image exists
if(! file_exists("images/{$image}")) {
    // Return 404 - image doesn't exist
}
?>
<html>
    <head></head>
    <body>
        <!-- various page elements -->
        <img src="/fileupload/images/<?php echo $image; ?>" alt="Image title" />
        <!-- various other page elements -->
    </body>
</html>

This therefore means that your directory structure with files should look like:

fileupload/
    .htaccess
    images/
        example.jpg
    view.php

When visiting /fileupload/view/example.jpg, you should then see the image rendered in your page.

Wow finally problem solved
Thanks a ton blocblue
You are indeed a php guru........
Thanks so much for help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.