与require有关的路径有关问题
与require有关的路径问题
我把网页的header部分独立出来,让其他php文件去require这个文件。
header.php文件放在 /common 文件夹中,
其他文件放在 /main 文件夹中。
我在header.php 文件中设置网页的icon,图标的位置在 /common/imgs
下面这段代码中的路径应该怎么写,才能让其他网页require这个文件的时候能找到icon的位置?
<link rel="shortcut icon" href=<?= $_SERVER["DOCUMENT_ROOT"] .<br /> "/common/imgs/icon.ico" ?> />
------解决方案--------------------
设你的 common 文件夹 是网站的一级子目录
则
href、src 属性都是 url 指向,与文件系统无关
------解决方案--------------------
header.php加上<br /> define("common_dir",dirname(__FILE__));
<link rel="shortcut icon" href="<?php echo common_dir;?>/imgs/icon.ico" />
------解决方案--------------------
不是的
define("common_dir",dirname(__FILE__));
是相对于 使用该函数的文件所在路径 即header.php所在路径
而不是相对引用header.php文件所在路径
你在任何地方引用了 header.php
common_dir都是不会变的
------解决方案--------------------
href、src 属性都是 url 指向,与文件系统无关
一般用相对路径 src="/文件夹/较片地址" 这样就行了
如果你的网站是在二级或多级目录下则在 相对路径前再加上对应的二级目录

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
