PHP - 上传图片并在页面上显示 [英] PHP - Upload picture and display on page

查看:647
本文介绍了PHP - 上传图片并在页面上显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < p> ?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))
{
echoThe file。 basename($ _FILES ['uploadedfile'] ['name'])。 已上传;
}
else
{
echo抱歉,上传文件时出现问题。;
}
?>

有没有办法修改它,以便将图片添加到html页面? / p>

谢谢

解决方案

把它放在html页面上。



我不太清楚你的问题是什么,尽管

编辑:



所以,在您的页面上使用html表单: $ b

 < form action =imageUpload.phpmethod =postenctype =multipart / form-datatarget =upload_targetonsubmit =jupload(); id =form1> 
< div style =visibility:'hidden';类= imageHolder的原因 > <! - 显示过程未完成的gif图像 - >
< / div>
< input type =filename =imgfile/>
< input type =submitname =uploadButtonclass =upbtnvalue =Submit/>
< / form>

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

 函数jupload()
{
$(。imageholder)。append('< img src =./ images / loading .gif注意>');
}

函数juploadstop(结果)
{
if(result == 0)
{
$(。imageholder) html的( );

$ b //结果将是图像的路径
else if(result!= 0)
{
$(。imageholder )。html的();
// imageplace是要添加图像的div的类
$(。imageplace)。append(< img src ='+ result +'>) ;


$ / 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 =javascripttype =text / javascript>
window.top.window.juploadstop(<?php echo $ result;?>);
< / script>


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

<?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?

Thanks

解决方案

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

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

EDIT:

So, html form on your page:

<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:

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
    $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>

这篇关于PHP - 上传图片并在页面上显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆