Reputation: 157
I am rewriting my website, changing it over from ColdFusion to PHP. While I am making the changes and testing I am using a subfolder under my son’s domain name. I have encountered a problem that I have seen before, but I have been able to fix it before by putting a couple of .. or / in front of the path.
While testing I have discovered that some of these paths work and some don't; it seems a bit random. So, it makes it a little difficult to test and in fact on one of the pages I'm working on I cannot get the path to work at all unless I put the complete path. However, that would defeat the purpose of using databases.
(And yes, the PHP variable is working.)
Does anyone know of a sure fix?
<img src="..$ArtFilePath" title="$Title" alt="$Title">
<img src="/$ArtFilePath" title="$Title" alt="$Title">
<img src="/PHP2012/$ArtFilePath" title="$Title" alt="$Title">
<img src="http://markdinwiddie.com/PHP2012/artwork/Drawings/Boy.jpg" title="$Title" alt="$Title">
Upvotes: 3
Views: 15697
Reputation: 17
<?php
$path1='C:/ff/ss.html';
$file=file_get_contents($path 1);
$dom = new DOMDocument;
@$dom->loadHTML($file);
$links = $dom->getElementsByTagName('img');
//extract img src path from the html page
foreach ($links as $link)
{
$re= $link->getAttribute('src');
$a[]=$re;
}
$oldpth=explode('/',$path1);
$c=count($oldpth)-1;
$fname=$oldpth[$c];
$pth=array_slice($oldpth,0,$c);
$cpth=implode('/',$pth);
foreach($a as $v)
{
if(is_file ($cpth.'/'.$v))
{
$c=explode('/',$v);
$c[0]="xyz007";
$f=implode('/',$c);
$file=str_replace ($v,$f,$file);
}
}
$path2='C:/mail/newpath/';
$wnew=fopen($path2.$fname,'w+');
fwrite($wnew,$file);
?>
Upvotes: 1
Reputation: 1104
Absolute Path URLs
Absolute paths are called that because they refer to the very specific location, including the domain name. The absolute path to a Web element is also often referred to as the URL. For example, the absolute path to this Web page is: http://webdesign.about.com/library/weekly/aa040502a.htm
You typically use the absolute path with the domain to point to Web elements that are on another domain than your own. For example, if I want to link to the Graphic Design Guide's site - I need to include the domain in the URL: http://graphicdesign.about.com/
. So a link to her glossary entry would look like this:
<a href="http://graphicdesign.about.com/library/glossaries/web/blabsoluteurl.htm"> ...</a>
If you're referring to a Web element that is on the same domain that you're on, you don't need to use the domain name in the path of your link. Simply leave off the domain, but be sure to include the first slash (/
) after the domain name.
For example, the Beginner's Resource Center has the URL: http://webdesign.about.com/library/beginning/bl_begin.htm
If I were to link to this URL from another page on my site, I could link to it in this way:
<a href="/library/beginning/bl_begin.htm">...</a>
It is a good idea to use absolute paths, without the domain name, on most Web sites. This format insures that the link or image will be usable no matter where you place the page. This may seem like a silly reason to use longer links, but if you share code across multiple pages and directories on your site, using absolute paths will speed up your maintenance.
Relative Path URLs
Relative paths change depending upon what page the links are located on. There are several rules to creating a link using the relative path:
links in the same directory as the page have no path information listed filename sub-directories are listed without any preceding slashes weekly/filename links up one directory are listed as
../filename
How to determine the relative path:
Determine the location of the page you are editing.
This article is located in the /library/weekly
folder on my site.
Determine the location of the page or image you want to link to.
The Beginner's Resource Center is located here: /library/beginning/
Compare the locations and to decide how to point to it
From this article, I would need to step up one directory (to/library
) and then go back down to the beginning directory
Write the link using the rules listed above:
...
Upvotes: 3
Reputation: 37701
If you want it to work dynamically and also when you move it to your server, make a fullpath variable, say $dir = "http://markdinwiddie.com/PHP2012";
or $dir = "/PHP2012";
and then call your images like:
<img src="$dir/$ArtFilePath" title="$Title" alt="$Title">
Upvotes: 0
Reputation: 157895
Speaking of paths, (not urls), everything that starts from the /
being an absolute path and works from any subfolder.
Thus, get one starts from the /
and works - it will work through whole site as well.
There is no use for the site address at all. http://markdinwiddie.com
have to be used only to address other sites and shouldn't be used for the local links.
Upvotes: 0
Reputation: 542
I am not quite sure what you want, but you can have something like:
<img scr="http://markdinwiddie.com/PHP2012/<?php echo $variable; ?>/<?php echo $anothervariable; ?>/" title="<?php echo $Title; ?>" alt="<?php echo $Title; ?>">
That would have the 'http://markdinwiddie.com/PHP2012/' part of the URL already in place and you can then use variables from your database to fill the other sections of the link. (With $variable being one section of the URL and and $anothervariable being the second part of the URL.
This would echo: (provided $variable=hi123, $anothervariable=two2, and $title=tilt)
<img scr="http://markdinwiddie.com/PHP2012/hi123/two2/" title="tilt" alt="tilt">
Hope that answers your question (or at least helps).
Upvotes: 3
Reputation: 9211
It really depends on your directory structure. As I'm sure you know, ../
will go up one level from the current working directory and /
will go to the root. (Note: Your web server root, not necessarily your machine's root directory.)
For consistency, say you have a sub directory of your webserver root called img
, then /img/$filename
should always resolve to what you are expecting.
Upvotes: 1
Reputation: 6919
Full path would be best.
You could create another variable for the domain name/subdomain, and then use full paths in your links.
You can also make it conditional, based on the server where the script is running at a given moment, using echo gethostname();
or php_uname('n');
Upvotes: 0