Jump to content

include subdirectory - images not displaying


immobilarity

Recommended Posts

I'm new to PHP and am hoping someone can help me with a problem.

 

I have a page named printdocs.php that needs to include multiple .html pages into one page for printing purposes. The printdocs.php page looks like this:

 

include('documents/100/100.html');

include('documents/101/101.html');

include('documents/102/102.html');

 

Each one of the .html files I'm trying to include has relative references to images (i.e. 100.html has <img src="100-101.jpg" />).  When I use the above include functions, the html outputs properly but the images do not display.  Note: I cannot change the code in the included .html files.  Is there a way to get the images to display?

 

Thank you!

Link to comment
Share on other sites

The links to the images are relative to the folder that the html file is in. The fact that they are all in different folders means that you change the link of the image to wherever your php file is. The only way to could change the links on the html after including them is to read the contents into a buffer and search for the image links and change them. Other than that, you could move all the html files to the same folder as the php along with the images.

Link to comment
Share on other sites

when you reference files, you need to reference them as if they were being loaded for the page the code is included into. 

 

so if youre in the /htdocs directory and you include a file in the /htdocs/includes directory, then the file needs to call the images as if it were in the /htdocs directory.

 

you can use PHP to create a dynamic link that calls the image location based on where the file is being called.

Link to comment
Share on other sites

Other than that, you could move all the html files to the same folder as the php along with the images.

 

I wish I could do that but these documents get updated almost on a daily basis and must remain within the documents subdirectory.  I use to program in ColdFusion and including files like this was not a problem (images would display)...so I'm a little stumped as to why I couldn't do the same with PHP.

 

I can change the images src from "100-101.jpg" to "documents/100/100-101.jpg" via javascript but i was hoping there'd be something simple with PHP.

 

Thanks for the quick reply.

Link to comment
Share on other sites

You could use something like

echo ereg_replace("<img src=\"", "<img src=\"documents/100/", file_get_contents("documents/100/100.html"));

 

That is assuming all the image tags are written like that (which is probable.) And actually, using file_get_contents() is a little more efficient than include() when you aren't using any php in the file :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.