多个域的PHP临时上传目录

标签 php apache .htaccess ubuntu tmp

我在 Ubuntu 18.04 上的 Apache Web 服务器上托管了多个域,但我无法为 PHP 设置 tmp 上传目录。

我尝试将其放入其中一个域的 .htaccess 中,但是在我测试时没有存储任何内容。

php_value upload_tmp_dir/var/www/example.com/tmp/

我对/var/www/example.com/tmp/文件夹的权限设置为 Chmod 775

是否有一种有效的方法可以在 .htaccess 或域的 .conf 文件中进行设置?

最佳答案

我在我的本地机器上为我的虚拟主机制作了类似的东西。请注意,所有内容都在我的虚拟主机中定义,因为您无法在运行时更改 upload_tmp_dirsys_temp_dir

<VirtualHost *:80>
    ServerName example.local
    ServerAlias www.example.local
    
    UseCanonicalName On
    
    <Directory /mnt/storage/Server/example>
        DirectoryIndex index.php
        #Options +Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
    
    php_admin_value upload_tmp_dir /mnt/storage/Server/example/temp/
    php_admin_value sys_temp_dir /mnt/storage/Server/example/temp/
    
    DocumentRoot "/mnt/storage/Server/example"
    
    ErrorLog ${APACHE_LOG_DIR}/example.error.log
    CustomLog ${APACHE_LOG_DIR}/example.access.log combined
</VirtualHost>

如何确认一切正常:

创建简单文件:

$dir = sys_get_temp_dir();
$file = tempnam($dir, rand());
var_dump(get_current_user());
var_dump($dir);
var_dump('is_writable: ' . (int)is_writable($dir));
var_dump('is_readable: ' . (int)is_readable($dir));
var_dump($file);

上传脚本:创建名为 upload.php 的文件

<?php
if (isset($_POST['submit'])) {
    var_dump($_FILES);
}
?>
<!DOCTYPE html>
<html>
    <body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
        Select image to upload:
        <input type="file" name="upload" id="upload">
        <input type="submit" value="Upload Image" name="submit">
    </form>
    </body>
</html>

关于多个域的PHP临时上传目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65471147/

相关文章:

apache - 如何在 Apache 代理(SSL 连接)上配置到 Tomcat

.htaccess - 是否有一个 Nginx 环境变量相当于 Apaches 的 {ENV :REDIRECT_STATUS}?

apache - .htaccess 使用子文件夹重写规则强制 SSL

apache - 使用查询字符串 htaccess 重定向 URL

Apache proxypass 不解析图像和 css 等资源的 url

php - 根据出版商选择图书时,动态下拉搜索仅返回一行

javascript - 当服务器文件(如php)不存在时如何创建ajax请求?

php - slim 的尊重验证问题

python - 使用 Apache Webserver 从 Python CGI 文件运行 .bat 文件

php - 将 2 个相似的分组 MySql 查询(同一个表)组合在一个并循环中以有效的方式获取数组