By andromeda on
Hi all,
I've configured my httd.conf and turned clean url on.
Everything is ok in Drupal except some nodes where I have some php code that simply read one or more filenames in a subdirectory of directory "files" and then display one or more images (contained in those files). Here's the snippet:
$dir = "files/miadir";
$dh = opendir("$dir") or die('Folder not found.');
while($file = readdir($dh)) {
if(is_file("$dir/$file")) {
$imagesize = getimagesize("$dir/$file",&$info);
print("<CENTER>");
print("<IMG SRC=\"$dir/$file\" BORDER=\"0\" $imagesize[3] ALT=\"bla bla bla\" ALIGN=CENTER>\n");
print("</CENTER>");
}
}
closedir($dh);
The resulnting html code is the same both with cleanurl on or with cleanurl disabled, but with cleanurl enabled the image/s is not shown:
<!-- begin content --> <div class="node">
<h1 class="title">bla bla bla</h1>
<div class="content">
<CENTER><IMG SRC="files/miadir/7085508i.jpg" BORDER="0" width="640" height="480" ALT="bla bla bla" ALIGN=CENTER>
</CENTER> </div>
<div class="links"><a href="/node/5/print" title="Mostra una versione stampabile di questa pagina">Versione stampabile</a> | letto 2303693 volte</div>
</div>
<!-- end content --> </div>
Any hints ?
Thank you in advance.
Andromeda
Comments
Hi there
You cannot use relative path to your "files" directory when clean URL is enabled. That's a limitation in Drupal.
I am afraid you'll have to go back and edit your nodes...
Change this line :
$dir = "files/miadir";to
$dir = base_path . "files/miadir";For more details on this, visit : http://drupal.org/node/106608
Thanks for your kind
Thanks for your kind reply.
I edited my code
to
but it doesn't work. The script isn't able to find the directory anymore !!!
According to the resulting html (as you can see above) I think Drupal can have access to the image file because it is able to get its dimensions that are reported in the img tag, but it isn't able to display it. It seems like there is something wrong in the img tag.
Any other ideas ?
Thanks
Andromeda
From your resulting html
From your resulting html
<IMG SRC="files/....etc(which is wrong when using clean URLs) it seems that you must add the base_path() when printing.Now it works !!!
Now it works !!!
I can't understand (till now, I have to read a bit more ;-) ) why when calling in
or
or
the path is catched right and why in the img tag it isn't !
Thank you, anyway !!!
Andromeda
Because in that case the
Because in that case the relative paths were the real directories under Drupal. So, it is '/files/miadir/your-image'
But when they are printed on a Drupal page with clean URL's and you are, for example, on '/node/28' then the relative paths become '/node/28/files/miadir/your-image', which is wrong. The difference is that now you did the trick when printing.
base_path() always prints at least a "/" (so it starts from your web root and not from '/node/28'), and when Drupal is in a subdirectory it also adds its path.
Andromeda
Is there a slash missing at the end of the subdirectory miadir ?
Thank you for having added the parenthesis there, which I had forgotten (base_path() rather than base_path).
Like CogRusty said, it's very useful to right-click on the image to fetch in its properties (under Windows anyway, I don't remember how to do this if you're web surfing on a MAC) the full path of the file, to see where the path err...
In the image tag, src has to be set using php (I am afraid), or alternatively using a macro with a contributed module, hence if you use php that would be something like this :
<img src="<?php print base_path() . $dir . '/' . $file; ?>" alt="blabla" width="<?php print $width; ?>" height="<?php print $height; ?>" />Caroline
Hi Chill35
I posted the (relevant) html code generated where the full path of the file can be read (in the img tag) !!!
When the script is not working and the image is not displaying where have I to right-click to read what ?
However, using - as suggested by CogRusty - another variable $absdir, everything is ok !!!
Thank you very much anyway :-) !!!
If you like, you can see the result at www.capracotta.com/node/5 (also ...node/6 and ...node/7) The site is in Italian, but the relevant informations are well understandable !!!
Andromeda
What is your final image URL
What is your final image URL when you hover with the mouse? I suspect that a Drupal path is being inserted depending on the page you are viewing. See this analysis, esp. the last case:
http://drupal.org/node/102667#comment-165791
The above is about Drupal in a subdirectory and the last case is problematic (it would need to be src="/base_path/files/...etc" to work).
If you are not in a subdirectory, then a front slash in your links should do the trick (src="/files/...etc")
If you always control the display of images you can leave them without a front slash and use something like
<?php print base_path() . "files/...etc"?>(or with a $base_path variable in themes).