加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 注册表 – 上传图片

发布时间:2020-12-13 22:49:11 所属栏目:PHP教程 来源:网络整理
导读:我正在为网站注册表单,我正在尝试将照片上传到服务器并将文件路径传递到数据库. 这可能只是一个简单的问题,因为我是php和mysql的初学者. 这是我的标准表单,我们称之为register.php.我删除了除图像之外的所有其他输入. form name="reg" action="code_exec.php
我正在为网站注册表单,我正在尝试将照片上传到服务器并将文件路径传递到数据库.

这可能只是一个简单的问题,因为我是php和mysql的初学者.

这是我的标准表单,我们称之为register.php.我删除了除图像之外的所有其他输入.

<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
    <input type="file" id="inputImage" Name="photo">
    <button class="btn btn-large btn-success" type="submit">Register</button>
</form>

这是执行文件,我们称之为code_exec.php

<?php
session_start();
include('connection.php');

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mname = $_POST['mname'];
$address = $_POST['address'];
$contact = $_POST['contact'];
$pic = $_POST['pic'];
$username = $_POST['username'];
$password = $_POST['password'];
$skype = $_POST['skype'];
$email = $_POST['email'];

//This is the directory where images will be saved 
$target = "upload/"; 
$target = $target . basename( $_FILES['photo']['name']); 

//This gets all the other information from the form 
$pic = ($_FILES['photo']['name']); 

//Writes the photo to the server 
if (move_uploaded_file($_FILES['photo']['tmp_name'],$target)) { 
    //Tells you if its all ok 
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded,and your information has been added to the directory"; 
} else { 
    //Gives an error if its not 
    echo "Sorry,there was a problem uploading your file.";
} 

mysql_query("INSERT INTO member(fname,lname,gender,address,contact,picture,username,password,skype,email,photo)VALUES('$fname','$lname','$mname','$address','$contact','$pic','$username','$password','$skype','$email','$pic')");

header("location: index.php?remarks=success");
mysql_close($con);

?>

如何找到图像文件的路径?

解决方法

在代码中

Before

<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post">
    <input type="file" id="inputImage" Name="photo">
    <button class="btn btn-large btn-success" type="submit">Register</button>
</form>

After changes

<form name="reg" action="code_exec.php" onsubmit="return validateForm()" method="post" enctype="multipart/form-data">
    <input type="file" id="inputImage" Name="photo">
    <button class="btn btn-large btn-success" type="submit">Register</button>
</form>

你只需添加
你的html表单标签中的enctype =“multipart / form-data”

Description

1.当您从表单上传任何类型的文件时,您必须在表单元素中编写enctype属性.

谢谢

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读