TeaCupApp
TeaCupApp

Reputation: 11452

How to get an URL of an image which has been stored on server

It must be very easy but I have no clue how to get the URL of an image from server.

Lets say I have domain named www.example.com and the image has been stored in my

htdocs>www>resources>userid>images>image.jpg

So, I want upload an image and save it through php script into my images folder and return the URL of that image back to browser like,

www.example.com/.../.../images/image.jpg

Is there any php function to get URL of a file?

EDIT:

After looking at answer I thought I should more clarify my self. I am really sorry for misunderstanding,

The help I wanted is to : what if the domain name isn't static I mean you can download and attach my image uploader plugin to your website but now it should dynamically get domain name and http path to your image.

Upvotes: 2

Views: 7944

Answers (4)

web-nomad
web-nomad

Reputation: 6003

Since i don't have your plugin, i don't know the flow of your code. My guess below, hopefully, you can modify it according to your code.

Suppose i download your plugin to my website and upload an image using it. You can get the image path like this:

<?php
$upload_path = 'PATH_WHERE_IMAGE_IS_UPLOADED';
$image_name = 'NAME_OF_THE_UPLOADED_IMAGE';

$relative_path = $upload_path . DS . $image_name;

$path = str_replace( $_SERVER['DOCUMENT_ROOT'], $_SERVER['SERVER_NAME']
                                                            , $relative_path );

$path = 'http://' . $path; // this is where image is uploaded
?>

Note: This assumes that the upload directory is within the webroot.

Hopefully, this will work.

Upvotes: 5

JerabekJakub
JerabekJakub

Reputation: 5340

You can look at manual page of PHP function move_uploaded_file(). If I understand your problem, solution is there.

Upvotes: 0

amburnside
amburnside

Reputation: 1903

Editted

If you have for example:

$domain = $_SERVER['SERVER_NAME'];
$uploadPath = "/htdocs/www/resources/userid/images/image.jpg"

// replace "/htdocs/www", with an empty string
$subdir = str_replace("/htdocs/www", "", $uploadPath);

$url = $domain.$subdir;

Upvotes: 0

Rohit Choudhary
Rohit Choudhary

Reputation: 2259

Try this

$image='name of your image uploaded';
$imageurl='http://www.example.com/images'.$image;
echo '<input type=text name="image" value="'.$imageurl.'" disabled />;

Upvotes: 0

Related Questions