【问题标题】:Retrieving the image using PHP使用 PHP 检索图像
【发布时间】:2020-01-28 07:42:43
【问题描述】:

我是PHPAjax query 的初学者。我正在尝试使用croppie.js plugin 上传profile image。当我上传image 时,图像会像1580192100.png 一样保存。但我的问题是,根据useridretrieve the image 是不可能的。

这是我尝试过的代码。 profilepic.php页面

<?php

session_start();
require_once "../auth/dbconnection.php";

if (isset($_POST['image'])) {

    $croped_image = $_POST['image'];
    list($type, $croped_image) = explode(';', $croped_image);
    list(, $croped_image)      = explode(',', $croped_image);
    $croped_image = base64_decode($croped_image);
    $image_name = time().'.png';


 // Valid file extensions
 $allowTypes = array( 'bmp', 'jpg', 'png', 'jpeg' , 'JPG');

// if(in_array($fileType, $allowTypes)){

    $stmt = $conn->prepare("UPDATE users SET image = ? WHERE user_id= ?");
    $stmt->bind_param("si", $image_name, $_SESSION['user_id']);
    $stmt->execute();
    if($stmt->affected_rows === 0);


      file_put_contents('blog/'.$image_name, $croped_image);
    echo 'Cropped image uploaded successfully.';




    }else{

        echo "ERROR: Could not prepare query: $stmt. " . mysqli_error($conn);

    }

    $stmt->close();
//    mysqli_stmt_close($stmt);



?>

php fetch 代码是;

<?php

require_once 'auth/dbconnection.php';
$sql="SELECT image FROM users WHERE user_id= '".$_SESSION['user_id']."'";
        if($result = mysqli_query($conn, $sql)){
        if(mysqli_num_rows($result) > 0){
        while($row = mysqli_fetch_array($result)){

$out= '<img src="data:image/png;base64,'.base64_encode($row['image']).'" alt="">';
echo $out;

        }
    }
}
?>

我不知道我哪里错了。请帮帮我。

【问题讨论】:

  • 请澄清问题。您是否希望使用$userid 而不仅仅是时间来保存图像?
  • @RamRaider 检索image时出现问题
  • @NasikThaheed 你得到$_SESSION['user_id']$row['image'] 的值了吗?你也错过了图像的路径。
  • @DroidDev 当然。我得到了价值..
  • @NasikThaheed 你能在浏览器中查看图片吗? http://example.com/images/1580196780.png 带有完整路径。

标签: javascript php mysql ajax croppie


【解决方案1】:

看起来update 语句在数据库中设置了图像名称,而不是任何 base64 编码数据(最好只保存名称,否则表格会变得很大),因此当您尝试显示图像时,您需要阅读图像数据。我将上面的内容修改为使用prepared statement

<?php
    if( !empty( $_SESSION['user_id'] ) ){

        require_once 'auth/dbconnection.php';


        $sql='select `image` from `users` where `user_id`=?';
        $stmt=$conn->prepare( $sql );
        $stmt->bind_param( 's',$_SESSION['user_id'] );

        # determine the correct path for the image
        $filepath=$_SERVER['DOCUMENT_ROOT'] . '/profile/blog/';

        $res=$stmt->execute();
        if( $res ){
            $stmt->store_result();
            $stmt->bind_result($filename);

            while( $stmt->fetch() ){
                /* 
                    You store the filename ( possibly path too ) 
                    so you need to read the file to find it's
                    raw data which you will use as image source.
                    Use the filepath to find the image!!
                */
                printf(
                    '<img src="data:image/png;base64, %s" alt="" />',
                    base64_encode( file_get_contents( $filepath . $filename ) )
                );      
            }
            $stmt->free_result();
            $stmt->close();
            $conn->close();
        }
    }
?>

【讨论】:

  • 我插入了这段代码。错误显示为Warning:file_get_contents(1580196780.png): failed to open stream: No such file or directory in C:\xampp\htdocs\Hospital\adminpanel\edit-profile.php on line 412。我的包含文件夹是profile/blog/
  • ok - 不清楚路径是什么,或者它是否存储在数据库中,或者它是否只是名称。在file_get_contents之前为文件名加上正确的路径前缀
  • 好东西,很高兴它有帮助。
猜你喜欢
  • 2011-09-06
  • 2011-08-25
  • 1970-01-01
  • 2011-03-09
  • 2013-09-26
  • 2015-05-31
  • 1970-01-01
  • 2015-11-20
  • 2012-08-05
相关资源
最近更新 更多