Hy!
I parse a website with simplehtml dom to get all links from the pictures.
The problem is that the link is like "/pics/bla.jpg".
I have the full path from the website like "http://xxx.xxx/blob/gulsch".
Now i want to get the full image link from the image (link root + /pics/bla.jpg) (no concat)
like: http://xxx.xxx/pics/bla.jpg
This should work for many websites
I tried it with explode()
$root = explode("/", $link);
echo $root[2];
I never get it working.
Please help.
Try with parse_url:
$r = parse_url($websiteUrl);
$imageUrl = $r["scheme"] . "://" . $r["host"] . "/" . $imageRelativeUrl;
The "root" of the website is simply $r["host"].
Given the parts of a URL, you can build a full URL with http_build_url.
Related
So I'm working on a website currently on localhost. I'm testing an image uploader, and I have to generate an URL for the image to be displayed..
etc etc..
// move the file to folder <- it works
move_uploaded_file($_FILES["file"]["tmp_name"], '../../common/img/post-uploads/' . $name);
// Generate response.
$response = new StdClass;
$response->link = MAINFOLDER . '/common/img/post-uploads/' . $name; // but I need an URL here..?:/
echo stripslashes(json_encode($response));
As you see, when I move the uploaded file to path, it works like this "../../common/img/post-uploads"
but at the end, I would like to get an URL.. so the final diplayed image would be like
< img src="http://localhost/mysite/common/image/post-uploads/image.jpg">
is it possible somehow? like
$response->link = convertThisToURL('../../common/img/post-uploads/' . $name);
? :/
Since you show two ../ I would think that this item is outside of your domain. There isn't a way to do that unless the full link is inside the domain directory.
Per your comment below then answer then would be a symbolic link.
ln -s /localhost/mysite/common/img/post-uploads /localhost/mysite/images
For example. then just post that nice new url without all that ../ stuff
I have an XML file that I use to create a PHP object. I then want to simply print out the image however I am getting the error 'Not allowed to load local resource'.
To get the image URL I have set a variable
$root = ( __DIR__ );
And then append that to the path in my XML file.
$parseXML = simplexml_load_file('chicken.xml');
$img_url = $parseXML->picture;
$imgg = $root . '/' . $img_url;
This now gives me the current path which is
C:\wamp\www\recipesUpdated/images/MarinoWebsite.jpg
If I copy and paste this into my browser it displays the image but won't work when I echo it in an img src.
Is there a way of getting the path just to
\recipesUpdated/images/MarinoWebsite.jpg
Without the C:\wamp etc ?
Thank you!
You get path to image on your local computer, and browser not allowed to load this path.
Url must contain web server address or be relative
Example:
http://localhost/recipesUpdated/images/MarinoWebsite.jpg
or
/recipesUpdated/images/MarinoWebsite.jpg
For your question, try to set variable $root to empty string or your web server address.
I am loading a url into my page on my local server with PHP file_get_contents. It works fine but relative paths on the site I am pulling obviously fail and default to my localhost.
Does anyone have any advice on how I could swap relative paths of a page I am pulling to absolute ones?
Ive tried something like this but it fails...
$homepage = file_get_contents($theURL);
$homepage2 = str_replace($homepage, "/images", $theURL + '/images');
echo $homepage2;
Thanks!
You're arguments for str_replace are in the wrong order.
Instead of
$homepage2 = str_replace($homepage, "/images", $theURL + '/images');
You should be doing
$homepage2 = str_replace("/images", $theURL + '/images', $homepage);
http://php.net/str_replace
str_replace(search, replace, subject)
I'm trying to get a few PDF files I need for a project from a website (legally), but am running into some issues.
The URL of the location of the PDF file is f.e. example.com/?download_id=290758&s=d12134cac7ddb2198d232bba75c07d57&t=2-1-2014%2016:19:00
So first of all I parse this URL from the page containing it with the following code:
$html = file_get_html('http://www.example.com/Acer-CR-6530/manual-5-100076.html');
// find the download link containing the session ID
foreach($html->find('a[rel=nofollow]') as $e)
$link = $e->href;
$link = $baseURL . $link;
// Link: example.com/?download_id=290758&s=d12134cac7ddb2198d232bba75c07d57&t=2-1-2014%2016:19:00
echo file_get_contents($link);
This doesn't work. It doesn't output the PDF file. Should I use cURL? If so, I'm not a cURL pro so I would love to see code for doing this.
Thanks in advance!
I'm trying to add a link which allows users to view uploaded files.
Right now I have this:
$path = "http://" . APACHE_ROOT . UPLOAD_PATH . $result[0]['username'] . "ref_" . $ref_info[$i]['ref_email'] ."_" . $ref_info[$i]['year'];
$status = ' Uploaded ';
(Where:
APACHE_ROOT = /var/www/faculty-recruiting-ref/
UPLOAD_PATH = html/upload/
)
But I get an error that the url doesn't exist (specifically, "Google Chrome could not find var")
I also tried starting the url with "file://" but then the link does not respond.
Thanks!
PS If anyone thinks of a better title for this, please change it! I couldn't think of anything better.
EDIT: I should add that manually typing in the url does link to the correct file
/var/www/faculty-recruiting-ref/ is the local path for your server, it cannot be accessed from the outside.
The APACHE_ROOT should be replaced by something like www.yourdomain.com/faculty-recruiting-ref/