1

我的html表单:

<form action='' method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>

我的 php 文件:

if ($_POST['submit'] == "Submit") {
            echo "Upload: " . $_FILES["file"]["name"] . "<br>";
            echo "Type: " . $_FILES["file"]["type"] . "<br>";
            echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
            echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
            if (file_exists("/downloads/" . $_FILES["file"]["name"]))
            {
                echo $_FILES["file"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["file"]["tmp_name"],
                "/downloads/" . $_FILES["file"]["name"]);
                echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];
            }
}

我的 public_html: 中有 2 个文件夹:tmpdownloads777 权限(只是为了测试)

此文件位于下载 (public_html/downloads/new/update.php) 内的 .htaccess 保护文件夹中,我希望将 zip 文件上传到下载目录中。

此代码不会给我任何错误,但不会上传文件。为什么?

4

3 回答 3

2

尝试添加

if(move_uploaded_file(...)){
 echo "it works";
} else {
 echo "NOPE";
}

并替换:

 move_uploaded_file($_FILES["file"]["tmp_name"], "/downloads/" . $_FILES["file"]["name"]);

和:

 move_uploaded_file($_SERVER['DOCUMENT_ROOT'].'/'.$_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT']."/downloads/" . $_FILES["file"]["name"]);
于 2013-04-05T21:25:07.380 回答
1

PHP 需要 MAX_FILE_SIZE 来接收上传的文件

<form action='' method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="10240000">
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>

使用此表单,您可以上传 10 MB 或更少的 zip 存档...

您还必须将 php.ini 中的 max_post_size 和 upload_max_filesize 设置为相同或更高的值才能上传文件

使用这个 PHP 代码

if ((isset($_POST['submit']) && $_POST['submit'] == "Submit") AND isset($_FILES)) {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
        if (file_exists("/downloads/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " already exists. ";
        }
        else
        {
            if(move_uploaded_file($_FILES["file"]["tmp_name"],
            "/downloads/" . $_FILES["file"]["name"]))
                   echo "Stored in: " . "/downloads/" . $_FILES["file"]["name"];

            else echo "file could not be processed";
        }

}

于 2013-04-05T22:04:51.790 回答
0

这就是我解决它的方法:

HTML:

<form enctype="multipart/form-data" method="post" action="">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="file" id="file">
<input type="submit" name="submit" value="Submit">
</form>

PHP:

if($_POST['submit'] == "Submit") {
    $filename = $_FILES["file"]["name"];
    $source = $_FILES["file"]["tmp_name"];
    $type = $_FILES["file"]["type"];
    $name = explode(".", $filename);
    $target_path = "../".$filename;
    if(move_uploaded_file($source, $target_path)) {
        $message = "Your .zip file was uploaded";
    } else {    
        $message = "ERROR";
    }
        if($message) echo $message;
}
于 2013-04-06T09:08:49.617 回答