John Davidson

Get current image url - php

0 comments
Message:


I'd like to pass the current img src to a function I'm working on (to get more data from a database)


Is there a simple way to grab the src from within the img tag?


I'd like to do this:
<img src="http://example.com/images/1.jpg" alt="amazing image" data="<?php echo( get_extra_data($img_src) ); ?>" />


Rather than this:
<img src="http://example.com/images/1.jpg" alt="amazing image" data="<?php echo( get_extra_data("http://example.com/images/1.jpg") ); ?>" />


I'd like to dynamically get the src from the current img rather than having to have the url twice if that makes sense.


In other words, could the function get_extra_data() reference the img element it was called from?


This is because I'd like to just paste data="<?php echo( get_extra_data($img_src) ); ?>" into the exsisting image elements I need it on.



My Answer:
You can use PHP to get the current image URL by using the `$_SERVER['REQUEST_URI']` variable. Here's an example of how you can do this:

php
$currentUrl = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
echo $currentUrl;


This code will output the current URL of the image. You can then pass this URL to your function to get more data from the database.

Rate this post

3 of 5 based on 7705 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us