服务器迁移后无法上传文件 - 可能是权限问题

问题描述 投票:0回答:1

过去三天,我在处理涉及将文件和文本上传到我的服务器的项目时遇到了问题。每次用户上传内容时,都会在服务器上创建一个格式为 YearMonthDay_HourMinuteSecond 的新文件夹。

以前,在我运行 Ubuntu 22.04 和 Apache 的旧服务器上,代码运行良好。但是,在更改服务器后,我遇到了无法上传文件但内容已上传的问题。我怀疑问题可能与文件夹和文件权限有关。

以下是目录和文件的权限:

Apache 文件夹权限:

drwxr-xr-x 13 root root 4096 html/

英文文件夹权限:

drwxr-xr-x 3 root root 4096 en/

上传页面和上传脚本的权限:

\-rw-r--r-- 1 root root 15498 upload.html
\-rw-r--r-- 1 root root 2935  upload.php

上传目录权限:

drwxrwxrwx 4 www-data www-data 4096 test_upload/

我正在使用最新的 Apache2 和 PHP 版本,并且运行的是 Ubuntu 22.04。

我认为该问题可能与服务器上的权限有关。任何有关解决此问题的帮助或建议将不胜感激。预先感谢您!

上传.html:

        <form action="upload.php" method="post" class="upload-form">
            <div class="alo">
                <div class="form-content">
                    <h2>*********</h2>
                    <div class="file-input">
                        <input class="pop" id="file" multiple name="file[]" type="file">
                        <input class="tit" name="title" placeholder="Title" type="text">
                        <textarea cols="50" name="story" placeholder="Story" rows="4"></textarea>
                    </div>
                    <div class="options">
                        <div class="alog">
                            <label class="anon">
                                <input id="anonymous" name="anonymous" type="checkbox" value="true"> Stay Anonymous
                            </label>
                        </div>
                        <div class="username-section">
                            <input id="username" name="username" placeholder="Your Name (Optional)" type="text">
                        </div>
                    </div>
                    <div class="center">
                        <div class="alig">
                            <button class="submit-comment" type="submit">Send Data</button>
                        </div>
                    </div>                        
                </div>
            </div>    
        </form>

upload.php代码:

<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST["title"];
    $story = $_POST["story"];
    $username = isset($_POST["username"]) ? $_POST["username"] : "Anonymous"; 

    // Get the current date and time in the desired format (YearMonthDay_HourMinuteSecond)
    $currentDateTime = date("Ymd_His");

    // Directory to store files and content
    $uploadDir = "/var/www/html/test_upload/" . $currentDateTime . "/";

    if (!file_exists($uploadDir)) {
        mkdir($uploadDir, 0777, true);
    }

    // Check if files were uploaded
    if (!empty($_FILES["file"]["name"][0])) {
        foreach ($_FILES["file"]["name"] as $key => $fileName) {
            $uploadFile = $uploadDir . basename($fileName);

            // Move uploaded file with proper file type validation
            $fileTmp = $_FILES["file"]["tmp_name"][$key];
            $fileType = mime_content_type($fileTmp);

            // Add more file type validations if needed
            if ($fileType === 'video/mp4' || $fileType === 'video/mpeg') {
                if (move_uploaded_file($fileTmp, $uploadFile)) {
                    // File uploaded successfully
                } else {
                    // File upload failed
                    echo "File upload failed. Debugging information:<br>";
                    echo "Upload file: $uploadFile<br>";
                    echo "Temporary file: $fileTmp<br>";
                    echo "File type: $fileType<br>";
                    echo "Check directory permissions and server configuration.";
                    exit();
                }
            } else {
                // Invalid file type
                echo "Invalid file type. Only MP4 and MPEG videos are allowed.";
                exit();
            }
        }
    }

    // Save the content and user information to a text file in the directory (without geolocation and user agent)
    $content = "Title: $title\nStory: $story\nUsername: $username";
    file_put_contents($uploadDir . "content.txt", $content);

    // Redirect to success page
    header("Location: success.html");
    exit();
} else {
    // Redirect to error page
    header("Location: error.html");
    exit();
}
?>

活动 php.ini 行:

cat /etc/php/8.1/apache2/php.ini | grep -E "^[^;#]+"
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
zend.exception_ignore_args = On
zend.exception_string_param_max_len = 0
expose_php = Off
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2000M
max_file_uploads = 200000
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
   extension=mysqli
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[imap]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.cookie_samesite =
session.serialize_handler = php
session.gc_probability = 0
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = -1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[dba]
[opcache]
[curl]
[openssl]
[ffi]

如有任何问题请帮忙!

php apache file-permissions directory-permissions
1个回答
0
投票

这样做

<form action="upload.php" method="post" class="upload-form" enctype="multipart/form-data">
© www.soinside.com 2019 - 2024. All rights reserved.