【问题标题】:PHP - Upload picture and display on pagePHP - 上传图片并显示在页面上
【发布时间】:2013-04-27 04:51:28
【问题描述】:

以下代码允许我将图片(使用 html 表单)上传到我服务器上的目录。

<?php 

    $target = "http://www.mockcourt.org.uk/user/htdocs/pics/2012/";
    $target = $target . basename( $_FILES['uploaded']['name']); 
    $ok=1; 

    if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
    {
        echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
    } 
    else
    {
        echo "Sorry, there was a problem uploading your file.";
    }
?>

有什么办法可以修改它,让它将图片添加到一个html页面吗?

谢谢

【问题讨论】:

  • 选择您的文本,然后按 Ctrl+K 以将整个文本块缩进四个空格,使其成为代码块。每行周围的单个反引号使其不可读!

标签: php html


【解决方案1】:

上传后,你可以使用javascript将它放在html页面上。

不过,我不太确定您的问题是什么

编辑:

所以,页面上的 html 表单:

<form action="imageUpload.php" method="post" enctype="multipart/form-data" target="upload_target"  onsubmit="jupload();"  id="form1" >
    <div style="visibility:'hidden';" class="imageholder"> <!-- a gif image to show that the process wasn't finished -->
    </div>
    <input type="file" name="imgfile" /> 
    <input type="submit" name="uploadButton" class="upbtn" value="Submit" />
</form>

上传图片的Javascript(JQUERY)代码:

function jupload()
{
    $(".imageholder").append('<img src="./images/loading.gif">');
}

function juploadstop(result)
{
    if(result==0)
    {
        $(".imageholder").html("");

    }
    // the result will be the path to the image
    else if(result!=0)
    {
        $(".imageholder").html("");
        // imageplace is the class of the div where you want to add the image  
        $(".imageplace").append("<img src='"+result+"'>");
    }   
}

php代码:

<?php
    $target = "http://www.mockcourt.org.uk/user/htdocs/pics/2012/";
    $target = $target . basename( $_FILES['uploaded']['name']) ; 
    $ok=1; 

    if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
    {
        $result=$target;
    } 
    else
    {
        $result=0;
    }
?>

<script language="javascript" type="text/javascript">
    window.top.window.juploadstop(<?php echo $result; ?>);
</script>

【讨论】:

  • 我想要一个允许用户上传图片并自动将图片放到页面上的脚本。如果上面有图片,则将新图片添加到页面中
  • 您可以使用uploadify uploadify.com/demos,上传后可以显示到html页面
  • 我希望它有所帮助,尽管 Rakesh Shetty 的解决方案可能更快、更容易理解
【解决方案2】:

假设您有 HTML 格式的表单来提交图像.. 制作提交按钮,而不是提交按钮,而只是一个按钮.. 例如

<input type='button' id='submit_form' value='upload' />

在javascript中,使用Ajax提交表单,使用Jquery在网页中显示

$('#submit_form')click(function(){
  $.ajax({
    type: 'POST',
    url: path/phpfile.php,
    data: image_input_name
  });

//after submitting, get the url of the image form the server

  $('#div_to_display_image').html("<img src='path/image_file.jpg' alt='this' />");

});

希望这会有所帮助:-)

【讨论】:

  • 将图像添加到 dom 应该在成功响应后的回调中进行。就目前而言,这将行不通。 api.jquery.com/jQuery.ajax
猜你喜欢
  • 2015-02-12
  • 2011-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-21
  • 2019-03-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多