duanchun5520 2015-04-11 15:41 采纳率: 0%
浏览 17
已采纳

我如何使用PHP从表单中获取信息

html code:

<form  enctype="multipart/form-data" action="upload_file.php"
method="POST">
<p>&nbsp;</p>
<p>Browse for file to upload: <br>
<input name="file" type="file" id="file" size="80"> <br>
<input type="submit" id="u_button" name="u_botton" value="Upload the File">
</p>
</form>

php code:

<?php
$file_result = "";

 if ($_FILES["file"]["error"] > 0)
 {
 $_file_result .= "No File Upload or Invalid File";
 $_file_result .= "Error Code: " . $_FILES["file"]["error"] . "<br>";
 } else {

 $file_result .=
 "Upload: " . $_FILES["file"]["name"] . "<br>" .
 "Type: " . $_FILES["file"]["type"] . "<br>" .
 "Size: " . ($_FILES["file"]["size"] / 1024) . "Kb<br>" .
 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>" .

 move_uploaded_file($_FILES["file"]["tmp_name"],
 "" . $_FILES["file"]["name"]);

 $file_result .= "File Upload Successful !";
 }
print " Now just go to www.example.com/ the name of the thing you uploaded "
?>

I am trying to get information from an html form, I am allowing people to upload to my server and I am trying to get the name of the file they uploaded

How do I do that?

  • 写回答

1条回答 默认 最新

  • donglu8344812 2015-04-11 16:03
    关注

    Just var_dump it. It will show all the information you need.

    <?php
    echo "<pre>";
    var_dump($_FILES["file"]);
    echo "</pre>";
    ?>
    
    $fileName = $_FILES['file']['name'];
    
    echo "File name is : " . $fileName;
    

    It will print,

    array(5) {
      ["name"]=>
      string(10) "assume.png"
      ["type"]=>
      string(9) "image/png"
      ["tmp_name"]=>
      string(14) "/tmp/phpe9vaQc"
      ["error"]=>
      int(0)
      ["size"]=>
      int(12969)
    }
    
    File name is : assume.png
    

    Here, assume.png was the original file name which i chose from my computer.

    EDIT : Since your question is solely based on how to get the filename which was submitted from the form, the answer is above.

    If you want to store the image to database, then

    1. Move the file to some folder like upload or images & get the upload path.
    2. Store the path to database like upload/someone.jpg.

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿