1

I was wondering if there’s any way I could store an image URL in a database with a unique numeric id, and a php page display the image by getting data (the image URL) from the database and displaying it to the user on the image.php page?

An example of this would be: - Having the image URL http://www.example.com/images/image10.jpg (where this URL would be stored in the database, alongside an id of 10) - Having an image.php page which retrieves the image URL from the database and displays the image on the same page (the image.php page) (and doesn’t redirect to the image URL) - an example result being image.php?id=10 where the image.php file would source the image URL from the database with an image id of 10 (using my example: www.example.com/images/image10.jpg) and display it (so it would be like viewing the image as if you were viewing www.example.com/images/image10.jpg but instead the URL would be http://www.example.com/image.php?id=10)

Is there any way to do this? I'm new to php and MySQL so not really good with code - trying to experiment :)

Thanks in advance!

Edit: I think people are getting confused by my bad description (sorry abot that). I've noticed in your answers that you have included the actual image URL in your code - I dont understand why though.

EXAMPLE of how I'd like this to work:

My table would have these feilds: img_id img_url

The image.php file would GET the img_url FROM the table using the id on the end of the image.php url. FOR EXAMPLE: - image.php?id=10 - the image.php file would use the id (which is 10), and display the image by retreiving the img_url (which would be: www.example.com/images/image10.jpg) from the table and displaying it - the image id would be img_id in the table. - image.php?id=12 - the image.php file would use the id (which is 12) and display the image by retreiving the img_url (which would be: www.example.com/images/image12.jpg) from the table and displaying it - the image id would be img_id in the table.

1
  • Can you give us some code that you have tried? Commented Mar 24, 2014 at 15:08

3 Answers 3

1

You can try this:

//File: image.php?id=10 
//you query   
$img = 'http://example.com/sample.jpg'; // Image path from database
$getInfo = getimagesize($img);
header('Content-type: ' . $getInfo['mime']);
readfile($img);
Sign up to request clarification or add additional context in comments.

2 Comments

This looks good but I'm confused by your answer - I want the php code to Source the image URL from the database, then display an image based on its ID - for example Image10.jpg would have a unique numeric id in the database - this being 10. So I want the image.php to Display AN image by first getting the id (like: image.php?id=10) and using the image url to display the image - where the image url would be on the same row (or would be part of the same record) as the record with an id '10'.
Your comment is not clear to me. please explain in simpler way, so may be I can help you.
0

I think what you need is this :

$image_path="http://example.com/image10.jpg";' 'mysql_query("INSERT INTO table VALUES ('',$image_path)");

in the php have this :

$id=$_GET['id'];if (isset($_GET['id'])){' '$result = mysql_query("SELECT path FROM table WHERE id=$id");' '$row = mysql_fetch_row($result);' '$path=$row[0];echo "Image number ".$id." is <img src='".$path."'>';}

3 Comments

Thanks for your answer. I am confused by this though - why would the image url be in this php code if the php file has to GET the image URL from the database?
You said you want the link look like this : example.com/image.php?id=10 . This means that you want to send the id through the link. The php takes the id from the link with $_GET['id']. Then searches within the database for the image with this id. When it founds it it takes the path. If all images look like this : image 10 image 11 etc you can use $path='example.com/image.php?id='; $path.=$_GET['id']; $path.='.jpg'; This code will results example.com/image.php?id=10 . With ".=" you concatenate strings.
Mysql injections would work here very easy, also use mysqli_* functions the mysql_* are marked as deprecated
0

It all depends on your code organisation, let me explain my self, right now in my mind i can think of two ways of organisation:

First, using a folder containing all images, with the same name + number for each image; e.g: images/img1.jpg - images/img2.jpg - images/img3.jpg etc

Using this method you'll not have to use even database, just within your page you go for this code, exemple for page http://exemple.com/images.php?id=10

$id = (isset($_GET["id"]))? $_GET["id"] : NULL; //Get the ID

Then use the method given by user3410107

$img = "http://example.com/img$id.jpg"; // Image path
$getInfo = getimagesize($img);
header('Content-type: ' . $getInfo['mime']);
readfile($img);

Second method is indeed using database, in this case there should be at least 2 columns within db, a unique ID + Images path, in this case you'll go for:

$query = mysql_query("SELECT id, imagePath FROM table WHERE id = $id");
while($data = mysql_fetch_assoc($query)){
$img = $data["imagePath"];
}

Once you get the image path, the same process as before:

$getInfo = getimagesize($img);
header('Content-type: ' . $getInfo['mime']);
readfile($img);

For security reasons add this line after $id variable

$id = (isset($_GET["id"]))? $_GET["id"] : NULL; //Get the ID
$id = mysql_real_escape_string($id);

2 Comments

Please use the mysqli_* functions, mysql_* is marked as deprecated
Thanks! The second method is the method I wanted!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.