dongqiao3927 2012-06-16 22:24
浏览 37
已采纳

PHP:为什么不获取我将插入数据库的图像数据?

I've the next problem: The code to validate the name, address and email works, but to validate the image does not work and do not know why that happens. As always prints "Select an image"

I did a test and set the line to print the temporary name of the image with "print $photo" but nothing prints!

form.php

<form id="formregister" name="formregister" action="register.php" method="post">
    <input id="name" name="name" type="text"/>
    <input id="address" name="address"  type="text"/>
    <input id="email" name="email" type="email" />
    <input id="photouser" name="imguser" type="file"/>
    <input id="bregister" name="bregister" type="submit" value="Registrer"/>
</form>

register.php

..............
$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];
$photo = $_FILES['imguser']['tmp_name'];
$errors = array();

    if(!isset($photo)) {
        $errors[] = "Select an image";
    } else {
        $fototemp = addslashes(file_get_contents($_FILES['imguser']['tmp_name']));
        $fotoname = addslashes($_FILES['imguser']['name']);
        $fotosize = getimagesize($_FILES['imguser']['tmp_name']);
        if ($fotosize == false) {
            $errors[] = "Invalid format";
        }
    } 
//Code to validate the other fields (name, address, email)
....................
  • 写回答

1条回答 默认 最新

  • douzi0609 2012-06-16 22:27
    关注

    You are missing the proper enctype for file uploads on your form. It should be enctype='multipart/form-data'

    <form id="formregister" name="formregister" action="register.php" method="post" enctype='multipart/form-data'>
    

    Rather than accessing the secondary tmp_name key directly, test if it is set first to avoid undefined index notices:

    if (isset($_FILES['imguser']) {
      $photo = $_FILES['imguser']['tmp_name'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题