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!

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

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了