4

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

4

4 回答 4

1

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

<?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
}
?>
于 2013-04-29T18:13:16.117 回答
0

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

This link will help you sending files with AJAX or jQuery

于 2013-04-29T17:35:18.687 回答
0

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

于 2013-04-29T18:07:13.260 回答
0

你可以试试这个..首先你可以上传一个文件,然后在 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>
于 2013-09-01T19:23:48.753 回答