【问题标题】:Displaying confirmation with file name after upload上传后显示带有文件名的确认信息
【发布时间】:2014-10-05 01:21:08
【问题描述】:

他,我试图弄清楚如何在上传后将文件名和扩展名显示为确认消息。

这就是我现在拥有的

    <?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('localhost', 'root', '', 'test_db');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }


        // Gather all required data
        $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
        $size = intval($_FILES['uploaded_file']['size']);

        // Create the SQL query
        $query = "
            INSERT INTO `file` (
                `name`, `mime`, `size`, `data`, `created`
            )
            VALUES (
                '{$name}', '{$mime}', {$size}, '{$data}', NOW()
            )";

        // Execute the query
        $result = $dbLink->query($query);

        // Check if it was successfull
        if($result) {
           echo "Your file has been uploaded";
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }

    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}

// Echo a link back to the main page
echo '<p>Click <a href="index.html">here</a> to go back</p>';
?>

而不是回显“您的文件已上传”,我希望它列出已上传文件的名称和扩展名,然后在末尾“已成功上传”。

示例:我上传contact.pdf。我喜欢显示“contact.pdf 已成功上传” 我希望这适用于任何文件和扩展名。 提前非常感谢。

【问题讨论】:

  • 您可以更改相应的echo 字符串。不过,在输出过于乐观的消息之前,您可能需要测试数据库存储的 temporary 文件名是否比另一个页面请求更持久。
  • 我该怎么做呢?我对这一切还是陌生的。

标签: php forms file-upload


【解决方案1】:

您需要从 FILES 变量中获取文件名:

http://php.net/manual/en/features.file-upload.post-method.php

您可能想要使用一些文件方法来获取您想要的文件名部分:

http://php.net/manual/en/ref.filesystem.php

【讨论】:

  • 我解决了我的问题 if($result) { echo "The file ".基本名称($_FILES['uploaded_file']['name'])。 "已成功上传。"; }`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
相关资源
最近更新 更多