php PHP如何在url中指定“后一级目录”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7210149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:16:12  来源:igfitidea点击:

PHP how to specify "back one directory level" in an url

phpmysqlurl

提问by Donal.Lynch.Msc

I have moved all of my files from being in the root (no folders, not very organised) so that they now reside within the following directory structure in my root folder:

我已将所有文件从根目录中移出(没有文件夹,不是很有序),以便它们现在位于我的根文件夹中的以下目录结构中:

css
images
js
scripts
index.html

... and this is fine. However, I have two or three php scripts which upload files (images) into the images folder above. This worked fine until I moved all of my files around and now I have been experimenting with things like "../" etc in order to link from the scripts directory, back one level, and then into the images directory.

......这很好。但是,我有两个或三个 php 脚本将文件(图像)上传到上面的图像文件夹中。这工作得很好,直到我移动了我的所有文件,现在我一直在尝试诸如“../”之类的东西,以便从脚本目录链接,回到一个级别,然后进入图像目录。

Any help appreciated guys....

任何帮助赞赏家伙....

回答by Ashley

Using what you stated above will work, so if you're in css and want to go to images (as an example) you would do the following:

使用您上面所说的内容将起作用,因此如果您使用 css 并想要查看图像(例如),您可以执行以下操作:

require_once('../images/yourimg.png');

回答by Krynble

The require_once is simular to the require() statement but it checks wheter the file has been included already, and if it did, then it won't be included again.

require_once 类似于 require() 语句,但它检查文件是否已经包含在内,如果包含,则不会再次包含该文件。

Now for your question, if you have something like:

现在问你的问题,如果你有类似的东西:

yoursiteurl/scripts/myscript.php

And you want that script to write a file to

并且您希望该脚本将文件写入

yoursiteurl/images/sampleimage.jpg

Then you should reference the image destination to something like this:

然后,您应该将图像目标引用为如下所示:

copy('source/file/location/sampleimage.jpg', '../images/sampleimage.jpg')

copy('source/file/location/sampleimage.jpg', '../images/sampleimage.jpg')

回答by James Waldby - jwpat7

  • As other answer notes, from current directory up a level and then back down to images can be done via a path like "../images/yourimg.png".

  • An alternative to that method is using an HTML <base>Tagpointed at the parent directory, after which you would use paths like"images/yourimg.png"to reference in images, or "js/some.js"in js, etc.

  • 正如其他答案所指出的,可以通过像"../images/yourimg.png".

  • 该方法的替代方法是使用指向父目录的HTML<base>标记,之后您将使用类似"images/yourimg.png"在图像或"js/some.js"js 中引用的路径。