dourao3960 2016-01-04 12:54
浏览 21
已采纳

通过PHP上传OO Profile图片

I have developed a registration form using object-oriented techniques, it works fine except when I try and incorportate a file upload so that the user can have a picture saved in the database.

In my database I am using the BLOB format and using file_get_contents to retrieve the file uploaded.

I previously had $profilepic = $_POST['photo']; which is why $_POST['photo'] is still in the second if statement.

I'm a little confused what format to use as I've not done a lot of object oriented.

Thanks in advance

<center>
<?php
session_start();
include 'registrationform.php';
include 'connection.php';

if (isset($_POST['regsubmit'])) 
    {
    $firstname = $_POST['firstname'];
    $firstname = ucfirst($firstname);
    $lastname = $_POST['lastname'];
    $lastname = ucfirst($lastname);
    $user = $_POST['username'];
    $user = ucfirst($user);
    $pass = $_POST['password'];
    $spass = $_POST['secondpassword'];
    $profilepic = file_get_contents($_FILES['photo']['tmp_name']);

    if($_POST['firstname'] && $_POST['lastname'] && $_POST['photo'] && $_POST['username'] && $_POST['password'] && $_POST['secondpassword'])
    {
    if ($spass == $pass)
    {
    $query = "INSERT INTO users (firstname, lastname, photo, username, password) VALUES(?, ?, ?, ?, ?)";
    $statement = $connection->prepare($query);
    $statement->bind_param('ssbss', $firstname, $lastname, $profilepic, $user, $pass);

    if($statement->execute()){
        print 'Success!'; 
        }else  
        {
        die('Error : ('. $mysqli->errno .') '. $mysqli->error);
        }
        $statement->close();
        }
        else
        {
           print 'The passwords do not match!';
        }
    }
    else
    {
        print 'Enter all fields please';
    }
    }
?>
</center>
  • 写回答

1条回答 默认 最新

  • dongxing4805 2016-01-04 13:09
    关注

    You can move uploaded image in folder using move_uploaded_file() and store image name in database.
    You can get image name as following:

    $image_name = $_FILES["photo"]["name"];
    

    For showing image you can retrieve image name.
    Then in html you can show image like following:
    <img src="YOUR_IMAGE_FOLDER_PATH/".$IMAGE_NAME>

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?