写入tmp文件夹

Write to tmp folder

编辑 1

本题涉及MANGOPAY API的用户。我的问题是我无法让 API 写入 tmp 文件夹。

我已成功让 API 写入 http://fakedomain.com/apifolder/blank.txt 并获得适当的输出。

然后我 运行 http://fakedomain.com/apifolder/blank.txt 上的脚本 http://fake.com/apifolder/demos/iswrite.php 有最少的工作代码,我可以在 tmp 文件夹上测试。这是脚本中的代码:

<?php

$filename = 'blank.txt';


echo "<br>";


file_exists($filename);

echo "<br>";



if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}

echo "<br>";

if (is_writable($filename)) {
    echo 'The file is writable';
} else {
    echo 'The file is not writable';
}

?>

它给了我输出

The file blank.txt exists The file is writable

一切都很好。我使用 Filezilla 创建了具有以下权限的以下文件:

在脚本 http://fake.com/apifolder/demos/iswrite.php 中,我已将文件名变量更改为 $filename = 'http://fake.com/tmp/creative/blank.txt';。它给了我以下输出:

The file http://fake.com/tmp/creative/blank.txt does not exist The file is not writable

此外,allow_url_fopen 已开启。

我不完全理解 URL 结构,所以问题可能出在这里?我尝试访问的 tmp 文件夹与我的 public_html 文件夹处于同一级别。也许我写错了 URL?

换句话说,tmp文件夹是否必须在public_html文件夹之外?这样做有什么目的吗?或者我可以在 public_html 中创建自己的 tmp 文件夹吗?


原始问题

原题写得不好。请参阅编辑 1

我正在玩 API (MangoPay) 的沙箱。我已经包含了我的 ClientId 和 ClientPassword,它们似乎有效。

API 还说...

You also need to set a folder path in $api->Config->TemporaryFolder that SDK needs to store temporary files. This path should be outside your www folder. It could be /tmp/ or /var/tmp/ or any other location that PHP can write to.

我在以下位置创建了一个:

ftp://fakedomain@fakedomain/tmp/fakefoldername

我是 运行 从我的桌面使用终端的 php 脚本。它给我的输出是

Cannot create or write to file ftp://fakedomain@fakedomain/tmp/fakefoldername

即使我已将权限设置为 777。

知道为什么会出现此错误吗?

开箱即用的 Linux 机器 运行 Ubuntu 14.04 具有以下用户和权限设置

drwxrwxrwt 13 root root 4096 nov  9 00:56 tmp//

和虚拟目录

drwxrwxr-x 2 eric eric 4,0K nov  9 00:49 fakefoldername/

如果切换到 777 权限集(正如你所说,你已经这样做了),那就是

drwxrwxrwx 2 eric eric 4,0K nov  9 00:49 fakefoldername/

这里要注意的一点是,如果您使用 chmod 777 fakefoldername,它会将权限集 仅更改 到目录,而不会触及任何 files/directories fakefoldername(使用chmod -R 777 fakefoldername)。另一点要记住的是,通往 fakefoldername 的目录也需要有足够的权限。对于这种特殊情况,检查 /tmp 文件夹是否有它们,必要时修复

sudo chmod 1777 tmp/

此外,如上所述,我会尝试在 /var 目录中创建另一个目录,然后看看情况如何(一个很好的答案,为什么选择 /var/tmp 而不是 /tmp here).

对于其他人:使用的SDK大概是https://github.com/Mangopay/mangopay2-php-sdk

下面有些类似的答案可能会有一些提示:

我猜有问题的图书馆是 this one, and the error you are getting is this exception:

 if (!is_writable($this->GetPathToTemporaryFolder()))
            throw new \MangoPay\Libraries\Exception('Cannot create or write to file ' . $this->GetPathToTemporaryFolder());

所以基本上我们似乎在调试对 is_writable().

的调用
  1. 确保 allow_url_fopen 已开启
  2. 如果适用,请确保 URL includes the FTP password
  3. 确保文件夹存在并且 FTP 帐户具有写入权限(777 应该足够...)

很可能,您的 apache 没有写入该文件夹的权限,您必须确保您正在使用相同的用户(在您的情况下为 apache)和 chmod 创建该目录。

一般来说,临时目录应该与您的代码位于同一台机器上。使用 FTP 不太理想。您不能使用本地目录吗?

您会在 MangoPay 文档中注意到它显示了一个本地目录:

https://github.com/Mangopay/mangopay2-php-sdk

$api->Config->TemporaryFolder = '/some/path/';

您应该坚持这样做,而不是通过 FTP 使用远程服务器。