通过上传的图像替换mysql中存储的图像

时间:2014-02-12 09:54:22

标签: php mysql

我最初通过mysql显示图像。现在我想通过上传的图像替换初始图像。我该怎么做?我使用此代码在PHP上的网页上传图像:

$UPLOADDIR = "upload/";

// 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
}
?>

1 个答案:

答案 0 :(得分:0)

只需放置更新查询当图像上传时,如果要删除现有图像,则使用unlink(PREVIOUS-FILE-NAME);这将删除以前的文件。

    if(move_uploaded_file($image_tmp,$image_file))
    {
        EXECUTE UPDATE QUERY TO REPLACE EXISTING FILE
    }
    else
    {
        echo "<h1>image file upload failed, image too big after compression</h1>";
    }