PHP的$ _FILES [“文件”] [“名称”]功能不正常 [英] PHP's $_FILES["file"]["name"] not functioning properly

查看:100
本文介绍了PHP的$ _FILES [“文件”] [“名称”]功能不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上次我在这里问了一个问题,我搞砸了,但我想我现在明白了。请让我知道,如果我可以问得更好:D



我正在使用PHP的HTML表单,并试图获取文件名。我使用Bootstrap进行格式化,我的Form看起来像这样:

 < form role =formclass =form -horizo​​ntalaction =<?php echo $ _SERVER ['PHP_SELF']; ?>方法= POST > 
< div class =form-group>
< label for =fileclass =col-sm-3 control-label> Picture< / label>
< div class =col-sm-9>
< input style =margin-top:7.5px; type =filename =fileid =filerequired>
< / div>
< / div>
< button type =submitclass =btn btn-success btn-lg btn-block>提交< / button>
< / form>

然后在代码后直接输入:

 <?php echo文件:。 $ _FILES [ 文件] [ 名称]; ?> 

第一次访问页面时输出结果(这是正确的):

 档案:

然而,我选择了一个文件,并在最后提交php代码仍然只输出文件:

任何人都知道这是为什么发生?



非常感谢您

解决方案

您错过了 enctype =您的< form> 标记中的multipart / form-data属性。没有它,文件将不会上传。

 < form enctype =multipart / form-datarole =form
class =form-horizo​​ntalaction =<?php echo $ _SERVER ['PHP_SELF']; ?>方法= POST >


Last time I asked a question here I messed up but I think I got it now. Please let me know if I can ask better :D

I am using an HTML form with PHP and am trying to get a file name. I am using Bootstrap for the formatting, my Form looks like this:

<form role="form" class="form-horizontal" action=<?php echo $_SERVER['PHP_SELF']; ?> method="post">
  <div class="form-group">
    <label for="file" class="col-sm-3 control-label">Picture</label>
    <div class="col-sm-9">
      <input style="margin-top: 7.5px;" type="file" name="file" id="file" required>
    </div>
  </div>
  <button type="submit" class="btn btn-success btn-lg btn-block">Submit</button>
</form>

And then directly after that code I have this:

<?php echo "File: " . $_FILES["file"]["name"]; ?>

Which upon first visiting the page outputs just (which is correct):

File: 

However after I select a file and hit submit the php code at the end still only outputs "File: "

Anyone know why this is happening?

Thank you very much

解决方案

You're missing your enctype="multipart/form-data" attribute in your <form> tag. Without it the file will not upload.

<form enctype="multipart/form-data" role="form" 
    class="form-horizontal" action=<?php echo $_SERVER['PHP_SELF']; ?> method="post">

这篇关于PHP的$ _FILES [“文件”] [“名称”]功能不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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