如何修复共享主机上的 mkdir() "Read-only file system"?



我正在尝试设置允许用户将文件上传到服务器上的目录(共享主机(的功能。用户可以键入上载文件夹名称并选择文件。如果文件夹不存在,则会创建该文件夹。

请注意,这不是一个大的上传系统,但只适用于一个用户。

我的代码在localhost上运行良好。使用公共服务器(共享主机(,可以创建文件夹。但里面没有文件。

上传功能,process.php:

// run code only if post request went through
if ($_SERVER['REQUEST_METHOD'] === 'POST' ) {
if (isset($_POST['folder'])) {
$folder = $_POST['folder'];
} else {
$folder = 'new-folder';
}
// ensure that there are files inside the array to actually upload
if (isset($_FILES['files'])) {
$errors = [];
$path = '/dir/data/uploads/images/' . $folder . "/";
// if folder does not exist, create it
if (!file_exists($path)) {
mkdir($path, 0777, true);
} else {
//stuff
}
$all_files = count($_FILES['files']['tmp_name']);
for ($i = 0; $i < $all_files; $i++) {
$file_name = $_FILES['files']['name'][$i];
$file_tmp = $_FILES['files']['tmp_name'][$i];
$file_type = $_FILES['files']['type'][$i];
$file_size = $_FILES['files']['size'][$i];
$tmp = explode('.', $_FILES['files']['name'][$i]);
$file_ext = strtolower(end($tmp));
$file = $path . $file_name;
// here follow multiple verifications on the file name, type, size, etc...
// removed to keep code shorter
if (empty($errors)) {
move_uploaded_file($file_tmp, $file);
}
}
if ($errors) print_r($errors);
} 
}

尝试和观察:

  • 在"网络"选项卡中,我得到status code 200

  • 服务器上目标文件夹images的读写执行权限(暂时用于测试!(设置为777(rwxrwxrwx)

  • 我测试了不同的文件和不同的文件夹名称(新的和现有的(,但我试图上传的文件永远不会出现。

  • 我注意到,尽管在我的代码中使用mkdir($path, 0777, true);来创建文件夹,但服务器上的文件夹的权限设置为705(rwx---r-x)

  • 与在localhost上运行良好的版本相比,我对此代码所做的唯一更改是更改了$path

我收到的错误消息:

Warning: mkdir(): Read-only file system in /path/to/scripts/process/process.php on line 31
/folder/data/uploads/images/test/image.webp -- 
Warning: move_uploaded_file(/folder/data/uploads/images/test/image.webp): failed to open stream: No such file or directory in /path/to/scripts/process/process.php on line 75

因此:

Warning: move_uploaded_file(): Unable to move (...)
  • 31号线为mkdir($path, 0777, true);
  • 75号线为move_uploaded_file($file_tmp, $file);

我为Linux文件系统找到了几个解决此问题的修复程序。但是在使用共享主机时,我该如何处理呢

这是我第一次尝试,非常感谢您的帮助。

我搜索并尝试了更多。这项工作:

$path = $_SERVER['DOCUMENT_ROOT'] . '/data/uploads/images/' . $folder . "/";

我现在可以将文件上载到此文件夹中。

谢谢你的帮助!我肯定会研究你建议的事情(并调整访问权限(,因为PHP仍然不是我的强项。:(

相关内容

最新更新