php-提交时自动向用户显示上载的文件

php-提交时自动向用户显示上载的文件,php,file-upload,Php,File Upload,我已经成功地为用户创建了PHP脚本,以便将文件上载到数据库以及本地主机上的文件夹。在用户刷新页面之前,他们无法看到上载的图像。这是我的密码 <?php error_reporting(E_ALL & ~E_NOTICE); //connect to database $connection = mysql_connect("localhost", "root", "") or die("can't make connection : " .

我已经成功地为用户创建了PHP脚本,以便将文件上载到数据库以及本地主机上的文件夹。在用户刷新页面之前,他们无法看到上载的图像。这是我的密码

<?php
error_reporting(E_ALL & ~E_NOTICE);
//connect to database
$connection = mysql_connect("localhost", "root", "") or die("can't make connection : " .                           mysql_error());
$database = mysql_select_db ("uploads", $connection) or die ("Could not select database");

//save the name of image in table
$query = mysql_query("select * from tbl_img") or die(mysql_error());

//retrieve all image from database and store them in a variable
while($row = mysql_fetch_array($query))
{
    $img_name = $row['img'];
    $image = "<img src='site_images/$img_name' /><br />";

    //store all images in one variable
    $all_img = $all_img . $image;
}
?>

<html>
<body>

<h1>Your Images</h1>
<?php echo $all_img;?>

</body>
</html>


<!DOCTYPE html>
<html lang="en">


Upload your image:<br />
<input name="img_field" type="file" id="img_field" /><br /><br />

<input type="submit" name="submit" id="submit" value="Submit" />


<?php
//get the posted image when the submit button is clicked
if(isset($_POST['submit']))
{
$file = $_FILES['img_field'];
$file_name = $_FILES['img_field']['name'];
$file_tmp_name = $_FILES['img_field']['tmp_name'];        

//save the image in img table
//connect to database
$con = mysql_connect("localhost", "root", "") or die("can't make connection : " . mysql_error());
$db = mysql_select_db ("uploads", $con) or die ("Could not select database");

//save the name of image in table
$query = mysql_query("INSERT INTO tbl_img(img) VALUES('$file_name')") or die(mysql_error());


//upload images to this folder (complete path)
$path = "site_images/$file_name";

//use move_uploaded_file function to upload or move file to the given folder or path
if(move_uploaded_file($file_tmp_name, $path)) 
{ 
    echo "File Successfully uploaded";
}
else
{
    echo "Please select a file";
}
}
?>

非常感谢您的帮助,thanx:

在您回显标记之前将处理上传文件的代码移动到是否解决了您的问题?