gpt4 book ai didi

php - 表单提交后显示 $_FILES ['image' ]

转载 作者:行者123 更新时间:2023-12-02 06:21:39 26 4
gpt4 key购买 nike

提交表单后如何显示上传的图片?提交表单后,它将是一个预览页面,因此我不会在 MySQL 中存储图像类型 BLOB。我如何显示 $_FILES['image']?

<?php

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

//preview page

$info = getimagesize($_FILES['image']['tmp_name']);
$imagetype = $info['mime'];
$tmpname = $_FILES['image']['tmp_name'];
$fp = fopen($tmpname,'r');
$data = fread($fp,filesize($tmpname));
$data = addslashes($data);
fclose($fp);

} else {
?>

<form enctype="multipart/form-data" action="http://www.example.com/submit" method="POST">
<input type="file" name="image" value="" size="50" />
<input type="submit" name="submit" value="submit" />
</form>

<?php
}

?>

最佳答案

如果您不想将图像存储在服务器上,您可以像这样预览:

<?php
if (isset($_FILES['image'])) {
$aExtraInfo = getimagesize($_FILES['image']['tmp_name']);
$sImage = "data:" . $aExtraInfo["mime"] . ";base64," . base64_encode(file_get_contents($_FILES['image']['tmp_name']));
echo '<p>The image has been uploaded successfully</p><p>Preview:</p><img src="' . $sImage . '" alt="Your Image" />;
}

但是如果你想把它存储在服务器上,只需存储它并显示它,而无需更改页面。

关于php - 表单提交后显示 $_FILES ['image' ],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8103486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com