PHP - 上传图片并显示在页面上

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

PHP - Upload picture and display on page

phphtml

提问by Ross

The following code allows me to upload pictures (using a html form) to a directory on my server.

以下代码允许我将图片(使用 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.";
    }
?>

Is there any way to modify it so that it will add the pictures to a html page instead?

有什么方法可以修改它,以便将图片添加到 html 页面吗?

Thanks

谢谢

回答by Indra

Well after you upload it, you can use javascript to put it on the html page.

上传好了之后,就可以用javascript把它放到html页面上了。

I'm not quite sure what your question is, though

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

EDIT:

编辑:

So, html form on your page:

因此,您页面上的 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) code for the upload and image add:

用于上传和图片添加的 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 code:

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>

回答by mithilatw

Suppose you have the form in HTML to submit the image.. make the submit button, not a submit button, but just a button.. e.g.

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

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

and in the javascript, use Ajax to submit the form and Jquery to display it in the web page

在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' />");

});

Hope this helps :-)

希望这可以帮助 :-)