【问题标题】:Run a php file outside www in ubuntu在 ubuntu 中在 www 之外运行一个 php 文件
【发布时间】:2020-01-06 06:00:08
【问题描述】:

我正在尝试使用不在 Ubuntu 的 www 目录中的 php 程序创建一个文本文件,假设文件是​​ /home/adminpc/create.php www 目录(/var/www/html/index.php)中还有另一个文件,这个 index.php 文件有助于调用 create.php 文件

我在 ubuntu 18.04 64 位上尝试过,使用 php 5.6
我只安装了 php,我没有在 ubuntu 中使用 LAMP

index.php

<html>
<body>
<p>CLick on the Link</p><br/>
<a href="check.php" >Link</a>               //Click on this link redirect to check.php
</body>
</html>

检查.php

<?php include ('/home/adminpc/create.php'); ?>     //This file is inside www directory

create.php

<?php
$structure = '/home/adminpc/Test';

if (!mkdir($structure, 0777, true)) {
    die('Failed to create folders...');
}


//CREATE FILE
$name = "Test/test.txt";    
$handle = fopen($name, "w");    
fwrite($handle, "test,9944");    
fclose($handle);

?>

我必须在 localhost 中运行 index.php
应该在 /home/adminpc 中创建一个名为 Test 的目录,并且在 Test中应该存在一个名为 test 的文件> 包含数据 9944 或任何其他数据的目录

【问题讨论】:

  • 您好,您遇到什么问题?
  • 这里没有问题,你遇到了什么问题?任何相关的错误信息?您只是在描述您的预期结果,而不是说明您遇到问题的原因......
  • "我在 ubuntu 18.04 64 位上尝试过,使用 php 5.6" — 危险! PHP 5.6 已经过时了!它没有得到安全修复。升级到受支持的 PHP 版本(7.1 作为绝对最低版本,7.2 是合理的最低版本,但最好是 7.3)。
  • 好的先生,升级到更高版本@Quentin
  • 先生,我没有收到任何错误,我无法使用上述代码创建目录和文件@Mohamed El Mrabet

标签: php html ubuntu command-line-interface


【解决方案1】:

$name = "Test/test.txt

这里的文件路径是相对于执行文件的(即index.php)。使用__DIR__获取create.php内的相对路径

$name = __DIR__ . "/Test/test.txt";

【讨论】:

  • 先生,create.php 存在于 www 文件夹之外(意味着它在 /home/adminpc 中),我需要使用 www 文件夹中的 index.php 调用该文件
猜你喜欢
  • 2015-07-10
  • 2012-05-11
  • 2011-03-16
  • 2011-03-26
  • 2014-05-19
  • 2016-06-13
  • 2011-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多