如何在PHP中替换图像?

时间:2011-12-27 17:33:07

标签: php

我使用此代码替换图像,但图像未上传到上传文件夹中,但数据库已更新。我需要替换图像。

<?php
require("header.php");

$patientid = $_POST['patientid'];
$pname = $_POST['pname'];
$fname = $_POST['fname'];
$address = $_POST['address'];

if(!$pname || !$address) {
    echo "<center>";
    echo "Fillup required fields<br><br>";
    echo "</center>";
}
else {
    $ran2 = $patientid;
    $new_name2 = $ran2."b";
    $new_file_name2 = $new_name2."."."jpg";
    $path = "./members/".$new_file_name2;

    $copied = move_uploaded_file($_FILES['img_name']['tmp_name'], $path);

    if ($copied) {
        $sql = mysql_query("UPDATE info_store 
        SET pname='$pname', fname='$fname', address='$address', image1='$new_file_name2'
        WHERE patientid='$patientid'");
        return true;
    }
    else {
        echo "<center><h3>There are An Errors In Uploading!</h3></center>";
        return false;
    }
}
?>

请给我一个解决方案。

1 个答案:

答案 0 :(得分:2)

看一下(替换图片):

您的PHP代码:

<?php
$path = '/my/folder/uploads/';
$row = mysql_fetch_assoc("select source from images where id = X ");
$File_Name_Here  = $path . $row['source'];
@unlink( $path. $File_Name_Here );

$output = $path . 'md5(time()).jpg'

copy( $_FILE['image']['tmp_name'] , $output );
?>

请使用复制功能并阅读此文档:

PHP FUNCTION:COPY

PHP FUNCTION:UNLINK

您的HTML代码:

<form action="" method="post" enctype="multipart/form-data">
    ...Your fields here...
</form>

在表单上,​​定义为属性方法“POST”并添加属性“enctype”

您的PHP SQL代码:

mysql_query("UPDATE images SET source ='".mysql_real_escape_string( $output )."' WHERE id = X");

请使用MYSQL_REAL_ESCAPE_STRING

PHP FUNCTION:MYSQL_REAL_ESCAPE_STRING

您的PHP.INI代码

file_uploads = On
upload_max_filesize = 20M

如果需要,只有在您是管理员的情况下才增加php.ini中的限制。