php 在PHP中提交后如何显示或预览上传的图像而不保存到数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16284713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:50:52  来源:igfitidea点击:

How to display or preview a uploaded image after submitted in PHP without save to database?

php

提问by JCChan

There are actually 2 page involve in my project.The 1st page is a form that to submit the uploaded image into 2nd page based on action="2page.php"and will display and preview the image.It supposed to run at client-server which mean not involve database that save the image file into it and retrieve the particular image back to page that I want to perform.Actually I had been write the PHPscript like this :<?php echo $_GET[image]; ?>to display the uploaded image in 2nd page but it didn't work.

我的项目实际上涉及2个页面。第一个页面是将上传的图像提交到第二个页面的表单,action="2page.php"并将显示和预览图像。它应该在客户端-服务器上运行,这意味着不涉及保存的数据库将图像文件放入其中并将特定图像检索回我想要执行的页面。实际上我一直在编写这样的PHP脚本:<?php echo $_GET[image]; ?>在第二页中显示上传的图像但它没有用。

采纳答案by Andy Ericksen

if your goal is to upload an image and store it in a directory on the server then this code sample might help get you started (though I won't swear that it's bug free or secure). It's a single page form to upload, save, and display an image.

如果您的目标是上传图像并将其存储在服务器上的目录中,那么此代码示例可能会帮助您入门(尽管我不会发誓它没有错误或安全)。这是一个用于上传、保存和显示图像的单页表单。

<?php
// prevent timezone warnings
date_default_timezone_set('America/New_York');

// set the upload location
$UPLOADDIR = "images/";

// if the form has been submitted then save and display the image(s)
if(isset($_POST['Submit'])){
    // loop through the uploaded files
    foreach ($_FILES as $key => $value){
        $image_tmp = $value['tmp_name'];
        $image = $value['name'];
        $image_file = "{$UPLOADDIR}{$image}";

        // move the file to the permanent location
        if(move_uploaded_file($image_tmp,$image_file)){
            echo <<<HEREDOC
<div style="float:left;margin-right:10px">
    <img src="{$image_file}" alt="file not found" /></br>
</div>
HEREDOC;
        }
        else{
            echo "<h1>image file upload failed, image too big after compression</h1>";
        }
    }
}
else{
    ?>
<form name='newad' method='post' enctype='multipart/form-data' action=''>
    <table>
    <tr>
        <td><input type='file' name='image'></td>
    </tr>
    <tr>
        <td><input name='Submit' type='submit' value='Upload image'></td>
    </tr>
</table>
</form>
<?php
}
?>

回答by Abhijit Chatterjee

you can try this..here first u can upload a file and then take two divs in html one is for the upload button and another one is for the picture which you wana show... you can give background color for better clarity and understanding..

你可以试试这个..这里首先你可以上传一个文件,然后在html中取两个div,一个用于上传按钮,另一个用于你想要展示的图片......你可以提供背景颜色以获得更好的清晰度和理解..

    <?php

$submit=$_POST['sub'];
if(isset($submit))
{
$name=$_FILES['img']['name'];
$type=$_FILES['img']['type'];

$size=($_FILES['img']['size'])/1024;

$ext=end(explode('.',$name));
if (($ext == "gif")
|| ($ext == "jpeg")
|| ($ext == "jpg")
|| ($ext =="png")
&& ($size > 30))
{


##############################File Renaming ###################################################

$newname=uniqid();
//$ext=end(explode('.',$name));
$fullname=$newname.".".$ext;
$target="pics/";
$fulltarget=$target.$fullname;
if(move_uploaded_file($_FILES['img']['tmp_name'],$fulltarget))
{
    echo "Success";



}
else
{
    echo "Failed";
}
##############################File Renaming end ###################################################
}

else{
    echo "not successful";
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="abhi.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<div id="a1">
<form name="frm" method="post" enctype="multipart/form-data">
<input type="file" name="img" /><br />

<input type="submit" name="sub" value="Store" />
</form>
</div>
<div id="a2">
<?php echo "<img src='$fulltarget'>";?>
</div>
</body>
</html>

回答by Memolition

send your file to a php script with jQuery and return the temp name and path to jQuery and put the response on a <img>and then if the user clicks some button insert that temporal image to your database with a new jQuery $.post()to another php script so one php to get the file and another to insert it on your db

您的文件发送到jQuery的PHP脚本,并返回临时名称和路径jQuery和放在一个响应<img>,然后,如果用户点击插入该时间图像的一些按钮,你的数据库与新的jQuery$.post()到另一个PHP脚本,使一个PHP获取文件和另一个将其插入您的数据库

This link will help you sending files with AJAX or jQuery

此链接将帮助您使用 AJAX 或 jQuery 发送文件

回答by Jiminy Cricket

You would only save the file to your database when you choose to. When uploaded, the file will be saved in your temp directory. From there you will have to tell php to move it to a real directory using move_uploaded_file().

您只会在选择时将文件保存到数据库中。上传后,文件将保存在您的临时目录中。从那里你必须告诉 php使用 move_uploaded_file()将它移动到一个真实的目录。