执行位于另一个文件夹中的PHP文件
I have a script that uploads a file. It then creates a new folder and moves the uploaded file to that folder along with the another script.
After the file is moved, I need to run a second script that resides in the new folder. But I need the second script to use its path and NOT the path of where it is called/executed from. (the upload script)
This is how I have tried to execute the script from the upload script.
exec("php-cli uploads/".$dirname."/".$file_name."/generate_thumbs.php");
I have error reporting on but nothing is generated. Any ideas?
我有一个上传文件的脚本。 然后它创建一个新文件夹并将上传的文件与另一个脚本一起移动到该文件夹。 p>
移动文件后,我需要运行驻留在新文件夹中的第二个脚本 。 但是我需要第二个脚本来使用它的路径而不是它被调用/执行的路径。 (上传脚本) p>
这就是我尝试从上传脚本执行脚本的方法。 p>
我有错误报告但没有生成任何内容。 有什么想法吗? p>
div> exec(“php- cli uploads /".$ dirname。“/”。$ file_name。“/ generate_thumbs.php”); code> p>
If you want to change working directory, use chdir()
before exec()
.
You can pass either absolute or relative directory name.
To execute PHP from PHP you can use include
instead of exec()
.
edit your upload script to:
<?php
// ... upload the file ...
// ,,open'' the target directory
chdir("uploads/$dirname/$file_name/");
// start the thumbs script
// this filename is relative to "uploads/$dirname/$file_name/"
include "generate_thumbs.php";
?>
What you should do is within your second script to use path to where the script is.
As found in the answer: Get absolute path of current script
From there you can write your script to operate from where it is as long as you can resolve where it is.