浏览文件后php上传自动提交 [英] php upload auto submit after browse a file

查看:135
本文介绍了浏览文件后php上传自动提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这里是我的代码

我想制作一个简单的网站,让您浏览mp3并自动上传。

 <?php 

if(isset($ _ POST ['submit'])){
$ file = $ _FILES [ 'MP3'] [ '名称'];
$ tmp = $ _FILES ['mp3'] ['tmp_name'];
echo$ file;
move_uploaded_file($ tmp,member / mp3 /\".$文件);
回声成功;

}
?>

< html>

< form enctype =multipart / form-datamethod =POSTaction =upload.php>

< input type =filename =mp3/>< br />
< input type =submitname =submitvalue =Upload/>
< / form>

< / html>

这里的代码是我的原始尝试,但仍然使用手动表单。



我怎样才能自动上传? 可能使用类似这样的内容: p>

 < input type =filename =mp3onchange =this.form.submit(); /> 

尽管我不会宽恕内联事件处理程序,但它只是一个示例,可以确保它适用于您。重点是捕捉文件输入的 onchange 事件,然后提交它的表单。不用 this.form 或其他任何东西,你可以通过ID获取表单 submit()就可以了。



更新: b

为了保持现有的PHP代码正常工作,给你的提交按钮一个 id 属性(除submit之外的其他东西),并尝试使用它: / p>

 < input type =filename =mp3onchange =document.getElementById('submit_button_id')。click ); /> 

再次说明,如果您将其设置为函数调用,则可以更易于维护/ p>

 < input type =filename =mp3onchange =submitForm(); /> 
< input type =submitname =submitid =submit_button_idvalue =Upload/>

< script type =text / javascript>
函数submitForm(){
//然而,您需要提交表单

document.getElementById(submit_button_id)。click(); //或任何
}
< / script>

如果您想更改PHP,可以使用原始的 this。 form.submit(); (或者以任何方式在表单元素上使用 .submit())。在PHP中,你真正关心的是检查是否设置了 $ _ FILES ['mp3'] ,所以如果你需要检查提交按钮的存在。我忘了 isset()是如何工作的,所以我不想假设并告诉你一些错误。您可能可以使用其他逻辑,但是您关心的真实情况是如果文件是在提交中发送的,即使如果确保使用按钮来提交文件,它可能会使其更好(尽管没有必要)。


I want to make a simple site that would let you browse for an mp3 and upload it automatically.

here is my code

<?php

if(isset($_POST['submit'])){
$file = $_FILES['mp3']['name'];
$tmp = $_FILES['mp3']['tmp_name'];
echo "$file";
move_uploaded_file($tmp, "member/mp3/".$file);
echo"Success";

}
    ?>

    <html>

<form enctype="multipart/form-data" method="POST" action="upload.php">

<input type="file" name="mp3" /><br />
<input type="submit" name="submit" value="Upload" />
</form>

 </html>

This code here is my original attempt however this still uses a manual form.

How can I make this upload automatically?

解决方案

Maybe use something like this:

<input type="file" name="mp3" onchange="this.form.submit();" />

Although I don't condone inline event handlers, it's just an example to make sure it works for you. The point is to catch the onchange event of the file input and then submit its form. Instead of using this.form or whatever, you can grab the form by ID (after giving it one) and call submit() on it.

UPDATE:

To keep your existing PHP code working, give your submit button an id attribute (something other than "submit") and try using this:

<input type="file" name="mp3" onchange="document.getElementById('submit_button_id').click();" />

Again, this could be more maintainable/readable if you made it a function call, like:

<input type="file" name="mp3" onchange="submitForm();" />
<input type="submit" name="submit" id="submit_button_id" value="Upload" />

<script type="text/javascript">
    function submitForm() {
        // However you need to submit the form

        document.getElementById("submit_button_id").click(); // Or whatever
    }
</script>

If you would rather change the PHP, you can use the original this.form.submit(); (or just use .submit() on the form element in whatever way). In the PHP, what you really care for is to check if $_FILES['mp3'] is set, so it's up to you if you need to check for the submit button's presence. I forget exactly how isset() works, so I don't want to assume and tell you something wrong. You might be able to use other logic, but the real thing you care about is if the file was sent in the submission, even though it might make it "better" if you make sure a button was used to submit it (though not necessary).

这篇关于浏览文件后php上传自动提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆