【问题标题】:Upload file to server using form periodically定期使用表单将文件上传到服务器
【发布时间】:2015-03-04 09:03:43
【问题描述】:

我正在尝试定期将 txt 文件上传到 MYSQL 数据库,因为 txt 文件内容随时间而变化。我有 2 个文件:db.php 和 uploader.php。下面是 db.php 的代码

<html>
<head>
<title>File Uploading Form</title>
</head>
<body>
<h3>File Upload:</h3>
Select a file to upload: <br />
<form action="uploader.php" method="post"
                        enctype="multipart/form-data">
<input type="file"  onchange="this.form.submit()" name="file"  size="50" />
<br /> 
</form>
</body>
</html>

还有 uploader.php 的代码

<?php
$contents = file_get_contents($_FILES['file']['tmp_name']) 
or die( "Could not copy file!");

$username = "username";
$password = "password";
$hostname = "localhost";

//connection to the database
$connection=mysql_connect ($hostname, $username, $password)
or die("Unable to connect to MySQL");

//select a database to work with
$selected = mysql_select_db("test",$connection)
or die("Could not select examples");

$result2 = mysql_query("INSERT INTO testtable(data) VALUES (". $contents. ")");
mysql_close($connection);
?>

运行程序后,我必须浏览我的 txt 文件,并将被重定向到 uploader.php。然后将数据发送到我的数据库。由于数据正在定期写入我的电脑中的 txt 文件,我需要再次自动将文件上传到我的数据库,而无需再次浏览 txt 文件。问题是,我该怎么做?如果我要定期手动刷新网页,新数据将被发送到我的数据库,但当然,我不想手动刷新页面,因为它不切实际。我尝试添加&lt;META HTTP-EQUIV="refresh" CONTENT="5"&gt;,它每 5 秒刷新一次页面,但这不起作用

我对 php 很陌生,所以如果答案简单易懂,我将不胜感激。非常感谢您的帮助。

【问题讨论】:

  • 为什么不让正在写入文件的任何进程也写入数据库?通过网络表单做这种事情似乎非常错误
  • 写入文本文件的内容是什么?您总是可以让它直接写入数据库。否则,除非数据库与文件托管在同一台计算机上,否则您会被我的看法所困扰。
  • @Phil 如果我弄错了,请纠正我,我用谷歌搜索了很多,似乎访问本地文件(在我的笔记本电脑 C 驱动器中)并将其发送到服务器的唯一方法是使用 web形式。
  • @MattShirley 正在写入文本文件的硬件没有 wifi 连接。
  • 对于你的问题 - 使用客户端 Python 或 Java 等发送数据

标签: php mysql


【解决方案1】:

您遇到的问题是,浏览器的 HTML 表单界面旨在与人类交互 - 但您希望它与计时器或类似机制交互。

所以基本上,你必须用可以轻松自动化的东西替换浏览器 - 我建议你使用wget:

  • 使用 shell 脚本、客户端 PHP 或任何可用的工具来创建 POST 数据
  • 使用wget --method=POST --post-data="$POST" http://your.domain/and.url转账
  • 使用cron 自动进行设置

【讨论】:

  • 我在命令提示符下运行了 'wget --method=POST --post-data="$POST" my.domain/and.url' 并收到错误说明 wget: unrecognized option '--method=POST '。
  • 实际上不知道wget 可以发布数据。我可能只是使用 curl 发送文件而不是创建 POST 数据字符串
  • @Sakuya 也许你有一个不同版本的wget - 试试把这个选项去掉
  • @Phil 我推荐wget 而不是curl,因为它更容易并且有一个不错的Windows 端口。我个人也会使用curl。
  • @Phil 如何使用 curl 将本地 txt 文件发送到浏览器?即我必须在命令提示符中输入什么?提前致谢
【解决方案2】:

这不是一个完整的解决方案。这只是一个示例,说明如何每隔nn 秒重复一次表单提交。请参阅底部的重要说明,然后考虑 Eugen Rieck 的解决方案。

要执行您的要求,您可以使用 javascript/jQuery。代码如下所示:

<html>
<head>
    <title>File Uploading Form</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
        $(function(){
            setTimeout(function(){
                myTimeoutFunction();
            },100000); //delay 100 seconds before first submit
        });
        myTimeoutFunction(){
            $('form').submit();
            setTimeout(myTimeoutFunction, 1000000); //repeat every 100 seconds
        }
    </script>
</head>
<body>
    <h3>File Upload:</h3>
    Select a file to upload: <br />
    <form action="uploader.php" method="post" enctype="multipart/form-data">
        <input type="file"  onchange="this.form.submit()" name="file"  size="50" />
        <br /> 
    </form>
</body>
</html>

注意事项:

  1. 这将要求浏览器在此网页上保持打开状态。一旦页面/浏览器关闭,这将停止工作。

  2. 如您所见,用户每次都必须选择文件。

  3. 在&lt;head&gt;中加载jQuery库

  4. 调用 jQuery submit() 方法以编程方式提交表单

  5. 将其包装在 setTimeout 中以在重新提交之间延迟 nn 毫秒

参考:

setTimeout or setInterval?

【讨论】:

  • 感谢您的帮助。我需要它工作,以便用户只选择一次文件并定期发送数据。
【解决方案3】:

我认为您可以使用 javascript/jquery 每隔 x 秒重复一次表单上传。

<body>
<head>
    <script src="https://code.jquery.com/jquery-2.1.3.js"></script>
</head>
<h3>File Upload:</h3>
Select a file to upload:
<br />
<form id="upload-form" action="uploader.php" method="post" enctype="multipart/form-data">
    <input type="file" onchange="this.form.submit()" name="file" size="50" />
    <br />
</form>

<script>
    /*
     * On form submission we will use an ajax request to submit the form, then if the ajax request is successful we will resubmit the form every 60 seconds.  Not sure if the file that is being uploaded will be changed if the filesystem changes.
     */
    $('#upload-form').submit(function (e) {
        e.preventDefault();
        var form = $('#upload-form'),
            formData = new FormData(form.get(0));

        $.ajax({
            url: form.attr('action'),
            type: form.attr('method'),
            data: formData,
            processData: false,
            contentType: false
        }).done(function (response) {
            setTimeout($('#upload-form').submit(), 60000);//Resubmit form every 60 seconds
            console.log('File Uploaded Successfully');
        }).fail(function (jqXhr) {
            alert('File Upload failed for some reason');
            console.error('File Upload Failed');
        }).always(function() {
        });

        return false;
    });
</script>
</body>

【讨论】:

  • 感谢您的帮助。我尝试运行代码,但是,表单似乎不是每 60 秒自动重新提交一次,因为每 60 秒没有新数据发送到我的数据库。
猜你喜欢
  • 2011-08-27
  • 2016-08-26
  • 1970-01-01
  • 1970-01-01
  • 2016-09-07
  • 2023-03-06
  • 2014-11-16
  • 2021-02-03
  • 1970-01-01
相关资源
最近更新 更多