php从两个不同位置写入文件路径
我有一个用于管理写入/读取/删除文件的 php 类。构造函数接收文件的路径(字符串)。基本上是这样的:
class customFile{
public function __construct($path){
$this->path=$path;
}
public function write($content){
//use fopen and fwrite to write content
}
public function read(){
//use fopen and fgets to read content
}
public function delete(){
//use unlink to delete the file
}
}
我也有这个目录树
├─ root/
│ ├─ helpers/
│ │ ├─ customFile.php
│ ├─ dir1/
│ │ ├─ function1.php
│ ├─ tempDir/
│ ├─ function2.php
│ ├─ useCustomFileFunction.php
非常重要的是 useCustomFileFunction.php 负责在 tempDir 中写入文件,因此该函数创建一个带有这样的路径的 customFile 对象 “./tempDir/”+someName (例如“./tempDir/writingFile.txt)。function1.php
和function2.php都使用一些文件名调用useCustomFileFunction(以写入一些文件),但是当从function1.php调用该函数时,结果是(不正确):
├─ root/
│ ├─ helpers/
│ │ ├─ customFile.php
│ ├─ dir1/
│ │ ├─ function1.php
│ │ ├─ tempDir/
│ │ │ ├─ writtenFile.txt
│ ├─ tempDir/
│ ├─ function2.php
│ ├─ useCustomFileFunction.php
当从 function2.php 调用时,结果是(正确):
├─ root/
│ ├─ helpers/
│ │ ├─ customFile.php
│ ├─ dir1/
│ │ ├─ function1.php
│ ├─ tempDir/
│ │ ├─ writtenFile.txt
│ ├─ function2.php
│ ├─ useCustomFileFunction.php
所以问题是:有没有办法告诉 fwrite,路径从调用 fwrite 的类/开始(customFile1.php?或使用CustomFileFunction.php代替function1.php和function2.php)
I have a php class used to manage write/read/delete files. The constructor receives the path (string) of the file. Basically something like:
class customFile{
public function __construct($path){
$this->path=$path;
}
public function write($content){
//use fopen and fwrite to write content
}
public function read(){
//use fopen and fgets to read content
}
public function delete(){
//use unlink to delete the file
}
}
Also I have this directory tree
├─ root/
│ ├─ helpers/
│ │ ├─ customFile.php
│ ├─ dir1/
│ │ ├─ function1.php
│ ├─ tempDir/
│ ├─ function2.php
│ ├─ useCustomFileFunction.php
Is very important to say that useCustomFileFunction.php is in charge of writing the files in the tempDir, so that function create a customFile object with a path like this "./tempDir/"+someName (for example "./tempDir/writtenFile.txt).
both function1.php and function2.php call useCustomFileFunction with some file name (to write some files) but when the function is called from function1.php the result is (incorrect):
├─ root/
│ ├─ helpers/
│ │ ├─ customFile.php
│ ├─ dir1/
│ │ ├─ function1.php
│ │ ├─ tempDir/
│ │ │ ├─ writtenFile.txt
│ ├─ tempDir/
│ ├─ function2.php
│ ├─ useCustomFileFunction.php
and when is called from function2.php, the result is (correct):
├─ root/
│ ├─ helpers/
│ │ ├─ customFile.php
│ ├─ dir1/
│ │ ├─ function1.php
│ ├─ tempDir/
│ │ ├─ writtenFile.txt
│ ├─ function2.php
│ ├─ useCustomFileFunction.php
So the question is: Is there a way to tell fwrite, that the path begin from the class/that call fwrite? (customFile1.php or useCustomFileFunction.php instead of function1.php and function2.php)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
function1.php 应该使用带有
../tempDir
路径的customFile
类。使用__FILE__
或__DIR__
是更好的方法。 code> 用于寻址。因此,让我们这样构建项目:
我们可以在
config.php
文件中包含此代码:我们将 config.php 包含在
function1.php
中: function2.php 中的方式:
现在您可以使用 TEMP_DIR 常量来寻址 tempDir 文件夹。
function1.php should use
customFile
class with../tempDir
path.It's a better way to use the__FILE__
or__DIR__
for addressing.So let's struct the project this way:
And we can have this code in
config.php
file:And we include the config.php in
function1.php
this way:And this way in
function2.php
:Now you can use the
TEMP_DIR
Constant to address thetempDir
folder.