php 点击上传后显示图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21929511/
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 04:32:40  来源:igfitidea点击:

Display image after clicking upload

php

提问by user3335903

How to display images right after they clicked the upload button?

单击上传按钮后如何立即显示图像?

Here is my HTML Code:

这是我的 HTML 代码:

form.php

表单.php

<html>
<body>

<form action="uploadFile.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file"><br>
    <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

And here is my uploadFile.php code:(updated, but still not getting any result)

这是我的uploadFile.php代码:(更新,但仍然没有得到任何结果)

<?php
       // prevent timezone warnings
       date_default_timezone_set('Philippines/Manila');

      // 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_type=$value['type'];
    $image = $value['name'];
    $image_file = "{$UPLOADDIR}{$image}";

 //check if there's existing file name
  if ($image  != 0){
echo 'File Already Exists!';
}

  else {
    // move the file to the permanent location

  if(move_uploaded_file($image_tmp,$image_file)){

// this is where the displaying part goes
    echo <<<HEREDOC

    <div style="float:left;margin-right:10px">
        <img src="$editedShear" alt="This is your image" height=200 width=200/></br>
    </div>

HEREDOC;

    }
    else{
        echo "<h1>image file upload failed, image too big after compression</h1>";
         }
        }
}}
 else{
  ?>

<?php
 }
?>

回答by Viktor Todorov

To do that you need to upload the image using some kind of ajax upload. if you don't know what is that, please read here http://en.wikipedia.org/wiki/Ajax_%28programming%29. You can find some free solutions and examples online. Here I found a stack overflow question that might help you with links to such a solutions

为此,您需要使用某种 ajax 上传来上传图像。如果您不知道那是什么,请在此处阅读http://en.wikipedia.org/wiki/Ajax_%28programming%29。您可以在网上找到一些免费的解决方案和示例。在这里,我发现了一个堆栈溢出问题,它可能会帮助您找到此类解决方案的链接

Ajax file upload

ajax文件上传

回答by Bamz3r

Add an iframeafter your form:

iframe在表单后面添加:

<iframe name="resultFrame" width="500" height="300"></iframe>

<iframe name="resultFrame" width="500" height="300"></iframe>

And add the target="resultFrame"attribute in your form like this:

target="resultFrame"在您的表单中添加属性,如下所示:

<html>
    <body>    
        <form action="uploadFile.php" method="post"
enctype="multipart/form-data" target="resultFrame">
            <label for="file">Filename:</label>
            <input type="file" name="file" id="file"><br>
            <input type="submit" name="submit" value="Submit">
        </form>

        <iframe name="resultFrame" width="500" height="300"></iframe>

    </body>
</html>

回答by user4055477

Upload your image in a folder which contains your php/html file, and in img tag write the img name:

将您的图片上传到包含您的 php/html 文件的文件夹中,并在 img 标签中写入 img 名称:

<img src="<?php echo $img;?>" width="400" height="600"/>

回答by jundell agbo

/* i put image so that it has default

    <img id="blah" src="images/male.png" alt="your image" style="width:250px;height:200px;" />

/*put accept and its format so that you can only choose the image only

<input type='file' name="image"  onchange="readURL(this);" accept="image/gif,image/png,image/jpg,image/jpeg" />

/*changing the url of an image you can do it in jquery
function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#blah')
                    .attr('src', e.target.result)
/*set the width and height of an image
                    .width(250)
                    .height(200);
            };

            reader.readAsDataURL(input.files[0]);
        }
    }

after that do your upload code

之后做你的上传代码

回答by user7507414

What is the easiest way to upload images and display the uploaded images on same page? page "index.php"

上传图像并在同一页面上显示上传的图像的最简单方法是什么?页面“index.php”

<body>
<div id="body">
 <form action="upload.php" name="img_compress" method="post" enctype="multipart/form-data">
 <input type="file" name="file" />
 <button type="submit" name="btn-upload">upload</button>
 </form>
    <br /><br />
    <?php
 if(isset($_GET['success']))
 {
  ?><br><br><br>
        
    <?php
 $sql="SELECT * FROM tbl_uploads ORDER BY id DESC";
 $result_set=mysql_query($sql);
 while($row=mysql_fetch_array($result_set))
    {
  ?>
     <img src="uploads/<?php echo $row['file'] ?>" width="100%" height="100%">
    <?php
}
 ?>
   
        <?php
 }
 else if(isset($_GET['fail']))
 {
  ?>
        <label>Problem While File Uploading !</label>
        <?php
 }
 else
 {
  ?>
        <label>Try to upload any files(PDF, DOC, EXE, VIDEO, MP3, ZIP,etc...)</label>
        <?php
 }
 ?>
</div>
</body>

code for upload "upload.php"

上传代码“upload.php”

<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{    
     
 $file = $_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
 $file_size = $_FILES['file']['size'];
 $file_type = $_FILES['file']['type'];
 $folder="uploads/";
 
 $new_size = $file_size/1024;  
 
 $new_file_name = strtolower($file);
 
 $final_file=str_replace(' ','-',$new_file_name);
 
 if(move_uploaded_file($file_loc,$folder.$final_file))
 {
  $sql="INSERT INTO tbl_uploads(file,type,size) VALUES('$final_file','$file_type','$new_size')";
  mysql_query($sql);
  ?>
  <script>
        window.location.href='index.php?success';
        </script>
  <?php
 }
 else
 {
  ?>
  <script>
  alert('error while uploading file');
        window.location.href='index.php?fail';
        </script>
  <?php
 }
}
?>
下面一个是用于存储图像的字段 enter image description here在此处输入图片说明