上传文件时添加文件描述

时间:2019-01-26 09:18:41

标签: php file-upload field-description

我已经创建了一个基本的上传表单,所有上传都可以正常进行,但是我找不到找到将文件描述添加到页面的方法。我希望它可以在“描述”下转到此处: uploads page

HTML表单:

<form action="/scripts/upload.php" method="POST" enctype="multipart/form-data">
Select a file to upload:
<input type="file" name="fileToUpload" id="fileToUpload"><br />
    <textarea id="FileDescription" name="FileDescription" rows="1" placeholder="*File description" required></textarea> <br />
<input type="submit" value="Upload File" name="submit">

脚本:

<?php
$target_dir  = "../uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk    = 1;
$df          = disk_free_space("../uploads/");

if (isset($_POST["submit"])) {
    $check = filesize($_FILES["fileToUpload"]["tmp_name"]);
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > $df) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has 
been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

1 个答案:

答案 0 :(得分:-1)

<textarea>标记必须链接到表单。 以这种方式使用它:

<form action="/scripts/upload.php" id="myForm" method="POST" enctype="multipart/form-data">
    Select a file to upload:
    <input type="file" name="fileToUpload" id="fileToUpload"><br />
    <textarea id="FileDescription" form="myForm" name="FileDescription" rows="1" placeholder="*File description" required></textarea> <br />
    <input type="submit" value="Upload File" name="submit">
</form>

只有这样,texarea的内容才会被传输。然后可以使用$_POST["FileDescription"]访问描述。要将其用作Apache描述,请参见http://httpd.apache.org/docs/2.2/mod/mod_autoindex.html#adddescription