上传时重命名个人资料图片并插入数据库

时间:2013-05-15 09:56:32

标签: php

有些人可以帮我实现我的上传脚本,以下代码能够将图像上传到数据库中,因此我想将上传的图像更改/重命名为将图像作为个人资料图片上传到的用户ID避免文件覆盖,严格只上传.jpg和.png文件 感谢您给予的帮助和建议,我非常感谢。

<?php
require("connection.php");
if(@$_POST ['submit'])
{
$file = $_FILES ['file'];
$name1 = $file ['name'];
$type = $file ['type'];
$size = $file ['size'];
$tmppath = $file ['tmp_name']; 
if($name1!="")
{
if(move_uploaded_file ($tmppath, 'users/'.$name1))
{
$query="INSERT INTO profiles set photo='$name1'";
mysql_query ($query) or die ('could not updated:'.mysql_error());
echo "Profile picture updated";
}
}
}
?>

1 个答案:

答案 0 :(得分:0)

<?php
require("connection.php");
if(@$_POST ['submit'])
    {
        $file = $_FILES ['file'];
        $type = $file ['type'];
        $name1 = $user_id.$type;
        $size = $file ['size'];
        $tmppath = $file ['tmp_name'];
        if($type != 'jpg' || $type != 'png') {
            echo "File type must be JPG or PNG."
           }else{
            if($name1!="") {
                if(move_uploaded_file ($tmppath, 'users/'.$name1))
                {
                    $query="INSERT INTO profiles set photo='$name1'";
                    mysql_query ($query) or die ('could not updated:'.mysql_error());
                    echo "Profile picture updated";
                }
            }
         }//must be jpg or png
      }
}
?>