PHP文件上载脚本失败

时间:2016-09-26 10:35:41

标签: php

我一直在关注文件上传到我的服务器的教程。该脚本似乎可以工作,但是当我查看上传目录时,那里什么都没有。

<?php

$file_result="";

if ($_FILES["file"]["error"] > 0)
{
$file_result .= "No file uploaded or invalid file.";
$file_result .= "Error code: ". $_FILES["file"]["error"]. "<br>";
} else {

$file_result .=
    "Upload: " . $_FILES["file"]["name"] . "<br>" . 
    "Type: " . $_FILES["file"]["type"] . "<br>" . 
    "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>" ;
    "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    move_uploaded_file($_FILES["file"]["tmp_name"],
    "full/path/on/server" . $_FILES["file"]["name"]); //Actual address removed

    $file_result .= "File Upload Successful!";
} echo $file_result;

?>`

HTML看起来像这样:`

<form enctype="multipart/form-data" action="upload_file.php" method="post">
Browse Files:<br>
    <input name="file" type="file" id="file" size="80"><br>
    <input type="submit" id="u_button" name="u_button" Value = "Upload the File">
</form>

`

1 个答案:

答案 0 :(得分:0)

使用以下代码进行上传:

if (Isset($_FILES["yourfilename"]["name"])) {
    $uploadOk = 1;
    $target_dir = "Upload/";
    --path
    $tempname = basename($_FILES["yourfilename"]["name"]);
    $target_file = $target_dir.basename($_FILES["yourfilename"]["name"]);
    $imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    if ($tempname != "") {
        if ($_FILES["yourfilename"]["size"] > 20000000) {
            echo "Sorry, Maximum 20 mg.<br>";
            $uploadOk = 0;
        }
        if ($imageFileType != "doc" && $imageFileType != "docx" && $imageFileType != "txt") {
            echo "Soory, Doc-Docx-Txt Only Format  Sopport.<br>";
            $uploadOk = 0;
        }

        if (move_uploaded_file($_FILES["yourfilename"]["tmp_name"], $target_file);
        }
    }