1

我已经成功地为用户创建了 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 :)

4

0 回答 0