图像路径保存在数据库中但图像不在目标文件夹中

时间:2017-05-29 09:07:28

标签: php

你好我这里有这个问题我在这里有一个任务我需要上传文件夹中的图像并保存图像的路径在数据库中。现在我有这个代码,路径正在保存在数据库中,如果我尝试上传我上传之前的相同文件,它给我文件的消息已存在但在目标文件夹中没有显示图像。请帮助我。这是我的代码

目录image.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>image in directory</title>
</head>

<body>
<form method="post" action="directory-imagedatabase.php" enctype="multipart/form-data">

<label>Choose File to Upload:</label><br />

<input type="hidden" name="id" />

<input type="file" name="uploadimage" /><br />

<input type="submit" value="upload" id="upload" />

</form>
</body>
</html>

目录imagedatabase.php

<?php
$target_Folder = '/officework/php-startup/images/';

$uid = $_POST['id'];

$target_Path = $target_Folder.basename( $_FILES['uploadimage']['name'] );

$savepath = $target_Path.basename( $_FILES['uploadimage']['name'] );

    $file_name = $_FILES['uploadimage']['name'];

    if(file_exists('/officework/php-startup/images/'.$file_name))
{
    echo "That File Already Exisit";
    }
    else
    {

        // Database 
    $con=mysqli_connect("localhost","root","sal123","test"); //Change it if required

//Check Connection
        if(mysqli_connect_errno())
        {
            echo "Failed to connect to database" .     mysqli_connect_errno();
        }

        $sql = "INSERT INTO directoryimage (id,image, image_name)
                    VALUES     ('','$target_Folder$file_name','$file_name') ";

        if (!mysqli_query($con,$sql))
        {
            die('Error: ' . mysqli_error($con));
        }
        echo "1 record added successfully in the database";
        echo '<br />';
        mysqli_close($con);

        // Move the file into UPLOAD folder

        move_uploaded_file( $_FILES['uploadimage']['tmp_name'],     $target_Path );

        echo "File Uploaded <br />";
        echo 'File Successfully Uploaded to:&nbsp;' . $target_Path;
        echo '<br />';  
        echo 'File Name:&nbsp;' . $_FILES['uploadimage']['name'];
        echo'<br />';
        echo 'File Type:&nbsp;' . $_FILES['uploadimage']['type'];
        echo'<br />';
        echo 'File Size:&nbsp;' . $_FILES['uploadimage']['size'];

    }
?>
<a href="show-directory.php">Show Image</a>

2 个答案:

答案 0 :(得分:2)

如果你的主文件相同的文件夹然后更新

$target_Folder = '/officework/php-startup/images/';

$target_Folder = 'officework/php-startup/images/';

那么这是你的总代码

<?php
$target_Folder = 'officework/php-startup/images/';

$uid = $_POST['id'];

$target_Path = $target_Folder.basename( $_FILES['uploadimage']['name'] );

$savepath = $target_Path.basename( $_FILES['uploadimage']['name'] );

    $file_name = $_FILES['uploadimage']['name'];

    if(file_exists('officework/php-startup/images/'.$file_name))
{
    echo "That File Already Exisit";
    }
    else
    {

        // Database
    $con=mysqli_connect("localhost","root","sal123","test"); //Change it if required

//Check Connection
        if(mysqli_connect_errno())
        {
            echo "Failed to connect to database" .     mysqli_connect_errno();
        }

        $sql = "INSERT INTO directoryimage (id,image, image_name)
                    VALUES     ('','$target_Folder.$file_name','$file_name') ";

        if (!mysqli_query($con,$sql))
        {
            die('Error: ' . mysqli_error($con));
        }
        echo "1 record added successfully in the database";
        echo '<br />';
        mysqli_close($con);

        // Move the file into UPLOAD folder

        move_uploaded_file( $_FILES['uploadimage']['tmp_name'],     $target_Path );

        echo "File Uploaded <br />";
        echo 'File Successfully Uploaded to:&nbsp;' . $target_Path;
        echo '<br />';
        echo 'File Name:&nbsp;' . $_FILES['uploadimage']['name'];
        echo'<br />';
        echo 'File Type:&nbsp;' . $_FILES['uploadimage']['type'];
        echo'<br />';
        echo 'File Size:&nbsp;' . $_FILES['uploadimage']['size'];

    }
?>
<a href="show-directory.php">Show Image</a>

答案 1 :(得分:2)

$sql = "INSERT INTO directoryimage (id,image, image_name)
                    VALUES     ('','$target_Folder$file_name','$file_name') ";

将其更改为:

$sql = "INSERT INTO directoryimage (id,image, image_name)
                    VALUES     ('','".$target_Folder."/".$file_name."','".$file_name."') ";