dongzhuang2030 2018-03-25 14:50
浏览 84
已采纳

PHP个人资料图片更改已删除

I am trying to create a social networking website and I want the users to be able to upload an image . I previously got help with this but (like an idiot) I deleted the code by mistake . I am trying to let the user upload an image but this is what I get :

My error

My problem

That square about the change profile picture button is where I'm suppose to be seeing the picture but I'm not . When the user signs up they have a default profile picture called default.jpg but that doesn't show up . I have it as default for userPic in my database .

profile.php:

<form id="form2" method="post" enctype="multipart/form-data">
    <p id="p1">Change profile picture:</p> <br />
    <input type="file" name="fileToUpload" id="fileToUpload"><br />
    <br><input id="sub1" type="submit" value="Change profile picture" name="change"><br />
</form>

<center><h5><?php echo $_SESSION['username']; ?></h5></center>

<?php
$username = isset($_SESSION['username']) ? $_SESSION['username'] : "";

$info = date('Y-m-d_H-i-s');

if (!empty($username)) {
    if (isset($_FILES['change'])) {

        $errors = array();
        $file_name = $_FILES['image']['name'];
        $file_size = $_FILES['image']['size'];
        $width = 500;
        $height = 500;
        $file_tmp = $_FILES['image']['tmp_name'];
        $file_type = $_FILES['image']['type'];
        $tmp = explode('.', $_FILES['image']['name']);
        $file_ext = strtolower(end($tmp));

        $extensions = array("jpeg", "jpg", "png");

        if (in_array($file_ext, $extensions) === false) {
            $errors[] = "extension not allowed, please choose a JPEG or PNG file.";
        }

        if ($file_size > 2097152) {
            $errors[] = 'File size must be 2 MB';
        }

        if ($width > 500 || $height > 500) {
            // Cancel upload
        }

        if (empty($errors) == true) {
            move_uploaded_file($file_tmp, "uploads/" . date('Y-m-d_H-i-s') . $file_name);

            $store = "UPDATE users SET userPic='$file_name' AND date_time='" . $info . "' WHERE username='$username'";

            if (mysqli_query($conn, $store)) {

            } else {
                echo "Update failed!";
            }
        } else {
            print_r($errors);
            echo"Couldn't upload picture";
        }
    }
    ?>

    <?php
    $getimg = mysqli_query($conn, "SELECT userPic & date_time FROM users WHERE 
username='$username'");
    $rows = mysqli_fetch_array($getimg);
    $img = $rows['userPic'] ['date_time'];
    ?>

    <?php
} else {
    echo "Invalid Username";
}
?>

<img id="myImg" src="uploads/<?php echo $img ?>" alt="<?php echo $img ?>" width="200" height="150">

UPDATE !! :

<form id="form2" method="post" enctype="multipart/form-data">
<p id="p1">Change profile picture:</p> <br />
<input type="file" name="fileToUpload" id="fileToUpload"><br />
<br><input id="sub1" type="submit" value="Change profile picture" name="change"><br />
</form>

<center><h5><?php echo $_SESSION['username']; ?></h5></center>

<?php

$username = isset($_SESSION['username']) ? $_SESSION['username'] : "";

$info = date('Y-m-d_H-i-s');

if(!empty($username))
{
    if (isset($_FILES['fileToUpload'])) {

      $errors= array();
      $file_name = $_FILES['fileToUpload']['name'];
      $file_size = $_FILES['fileToUpload']['size'];
      $width = 500;
      $height = 500;
      $file_tmp = $_FILES['fileToUpload']['tmp_name'];
      $file_type = $_FILES['fileToUpload']['type'];
      $tmp = explode('.',$_FILES['fileToUpload']['name']); $file_ext=strtolower (end ($tmp));

      $extensions= array("jpeg","jpg","png");

      if(in_array($file_ext,$extensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }

      if($file_size > 2097152) {
         $errors[]='File size must be 2 MB';
      }

      if ($width > 500 || $height > 500) {
      // Cancel upload
      }

      if(empty($errors)==true)
      {
        move_uploaded_file($file_tmp,"uploads/".date('Y-m-d_H-i-s').$file_name);

         $store = "UPDATE users SET userPic='$file_name', date_time='" . $info . "' WHERE username='$username'";

        if(mysqli_query($conn, $store))
        {
        }
        else
        {
            echo "Update failed!";
        }

      }else{
         print_r($errors);
         echo"Couldn't upload picture";
      }

}}
else
{
    echo "Invalid Username";
}
?>

<?php
$getimg = mysqli_query($conn,"SELECT userPic, date_time FROM users WHERE 
username='$username'");
$rows=mysqli_fetch_array($getimg);
$img = $rows['date_time'].$rows['userPic'];
?>

 <?php  
?>

<img id="myImg" src="uploads/<?php echo $img?>" alt="<?php echo $img ?>" width="200" height="150"> 
  • 写回答

1条回答 默认 最新

  • dpfps86064 2018-03-25 15:17
    关注

    Syntax error with &. Removed that and replaced with ,

    From:

    "SELECT userPic & date_time FROM users WHERE 
    username='$username'"
    

    to:

    "SELECT userPic, date_time FROM users WHERE 
    username='$username'"
    

    Not valid variables.

    From:

    $img = $rows['userPic'] ['date_time'];
    

    To:

    $img = $rows['date_time'].$rows['userPic']; // how should this be formatted?
    

    Wrong $_FILES key used.

    From:

    isset($_FILES['change'])
    

    to :

    isset($_FILES['fileToUpload'])
    

    Wrong $_FILES key used to retrieve value.

    From:

    $_FILES['image']
    

    To:

    $_FILES['fileToUpload']
    

    Syntax error with AND operator

    From:

      $store = "UPDATE users SET userPic='$file_name' AND date_time='" . $info . "' WHERE username='$username'";
    

    To:

      $store = "UPDATE users SET userPic='$file_name', date_time='" . $info . "' WHERE username='$username'";
    

    To increase file upload size.

    Inside your PHP file:

    ini_set('upload_max_filesize', '40M');
    ini_set('post_max_size', '40M');
    

    OR in PHP.ini

    ; Maximum allowed size for uploaded files.
    upload_max_filesize = 40M
    
    ; Must be greater than or equal to upload_max_filesize
    post_max_size = 40M
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line