【问题标题】:Adding a file description while uploading a file上传文件时添加文件描述
【发布时间】:2019-01-26 09:18:41
【问题描述】:

我已经创建了一个基本的上传表单,上传一切正常,但我无法找到将文件描述添加到页面的方法。我希望它在“描述”下出现: 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.";
    }
}
?>

【问题讨论】:

  • 图片显示一个 jpeg 图片和一个 html 文件... html 文件是评论吗?
  • 你的意思是你希望描述出现在目录的apache目录列表中吗??
  • 表单是我使用的 HTML 页面。照片只是显示文件和描述应该去的上传页面。那里的文件正是我用来测试它是否正常工作的文件。编辑:是的,这就是我的意思。

标签: php file-upload field-description


【解决方案1】:

&lt;textarea&gt; 标记必须链接到表单。 以这种方式使用它:

<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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2012-01-31
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多