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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化