从不同页面上传图片

时间:2016-05-09 11:46:37

标签: php mysqli

我想上传图片并将其从不同页面插入数据库。我想创建一个管理面板,您可以在其中将图像上传到图像边,但图像滑块位于不同的页面中。当我的表单方法在index.php中时,它正在工作,但当我把它放到我的admin.php时,它不起作用。我想我需要一个GET方法?

有人可以告诉我哪种方法,要求这样做?我是php和sql的新手。

这是我的index.php代码,这是我想要显示幻灯片的地方。

<?php
//for connecting db
include('connect.php');
if (!isset($_FILES['image']['tmp_name'])) {
echo "";
}
else
{
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"gallery/" . $_FILES["image"]["name"]);
$photo="gallery/" . $_FILES["image"]["name"];


$query = mysqli_query($mysqli, "INSERT INTO images(photo)VALUES('$photo')");
$result = $query; 

echo '<script type="text/javascript">alert("image successfully uploaded ");window.location=\'index.php\';</script>';
}
?>
<!DOCTYPE html>
<html>
    <head>
        <link href="css/style.css" rel="stylesheet" />
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
        <script src="js/slider.js"></script>
        <script>
        $(document).ready(function () {
        $('.flexslider').flexslider({
        animation: 'fade',
        controlsContainer: '.flexslider'
        });
        });
        </script>
    </head>
    <body>
    <div class="container">
        <div class="flexslider">
            <ul class="slides">
                <?php
                    // Creating query to fetch images from database.
                   $query = mysqli_query($mysqli, "SELECT * from images order by id desc limit 5");
                    $result = $query;
                    while($r = mysqli_fetch_array($result)){ 
                ?>
                    <li>
                    <img src="<?php echo $r['photo'];?>" width="400px" height="300px"/>
                    </li>
                <?php 
                } 
                ?>
            </ul>
        </div>
    </div>
    </body>
</html>

这是我的connect.php代码。

<?php
// hostname or ip of server
$servername='localhost';
// username and password to log onto db server
$dbusername='root';
$dbpassword='';
// name of database
$dbname='pegasus';

////////////// Do not  edit below/////////
$mysqli = new mysqli($servername,$dbusername,$dbpassword,$dbname);
if($mysqli->connect_errno){
    printf("Connect failed: %s\n", $mysql->connect_error);
    exit();
}

?>

这是我的admin.php代码,这是我想上传图片的地方。

<form class="form" action="" method="POST" enctype="multipart/form-data">
        <div class="image">
            <p>Upload images and try your self </p>
        <div class="col-sm-4">
              <input class="form-control" id="image" name="image" type="file" onchange='AlertFilesize();'/>
              <input type="submit" value="image"/>
            </div>
        </div>
        </form>

这是我的index.php这是我想要显示幻灯片的地方。 enter image description here

这是我的admin.php,我想上传图像滑块的图像。 enter image description here

1 个答案:

答案 0 :(得分:0)

我通过将php代码放入admin.php

来解决这个问题
<?php
//for connecting db
include('connect.php');
if (!isset($_FILES['image']['tmp_name'])) {
echo "";
}
else
{
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"gallery/" . $_FILES["image"]["name"]);
$photo="gallery/" . $_FILES["image"]["name"];


$query = mysqli_query($mysqli, "INSERT INTO images(photo)VALUES('$photo')");
$result = $query; 

echo '<script type="text/javascript">alert("image successfully uploaded ");window.location=\'admin.php\';</script>';
}
?>
    <form class="form" action="" method="POST" enctype="multipart/form-data">
        <div class="image">
            <p>Upload images and try your self </p>
        <div class="col-sm-4">
              <input class="form-control" id="image" name="image" type="file" onchange='AlertFilesize();'/>
              <input type="submit" value="image"/>
            </div>
        </div>
        </form>