search
Homephp教程php手册php上传图片代码(同时图片保存到数据库)

下面提供的php上传图片代码是一款利用php copy来上传文件的,他不但可以把图片上传到服务器,同时还把图片的地址保存到了mysql数据库里面哦。
 代码如下 复制代码

// 连接数据库
$conn = mysql_connect("localhost", "phpdb", "phpdb");
mysql_select_db("test",$conn);
?>

// 取得网页的参数
$id=$_post['id'];

// 判断该用户名是否已经存在
$checksql="select * from image where id='$id'";
$check_re=mysql_query($checksql,$conn);
$num=mysql_num_rows($check_re);
if($num!=0){
 echo "

";
 echo "该用户名已经存在,请选择另一个
";
    echo "上传失败!
返回";
    echo "
";
 exit();
}

// 方法二:只保存文件名,
// 保存文件名时,文件在php.ini配置文件中设置的upload临时目录中,也就是upload_tmp_dir 参数中

if ($photo""){ 
 if (($photo_type== "image/pjpeg")or($photo_type == "image/gif")){
  // c:winnt emp 使php.ini配置文件中设置的upload文件的临时目录
  $photodir="c:winnt emp/";
  if(!(file_exists($photo_name))){
   // 拷贝该图片文件到设定的上传文件临时目录中
     copy($photo,$photodir.$photo_name);
  }
 }
 else{
    echo "
";
  echo "或者
";
        echo "文件名已经存在,请为图片改一个文件名";
        exit;
 }
}
else{ 
 $photo_name="";
}
$sql="insert into image (id, photo) values('$id', '$photo_name')";

mysql_query($sql,$conn) or die ("插入数据失败: ".mysql_error());

// 关闭连接
mysql_close($conn);
// 显示上传图片成功
// 重定向到注册成功页面
header("location:display_image2.php?id=$id");

?>

代码二

// 连接数据库
$conn = mysql_connect("localhost", "phpdb", "phpdb");
mysql_select_db("test",$conn);
?>

// 取得网页的参数
$id=$_post['id'];

// 判断该用户名是否已经存在
$checksql="select * from image where id='$id'";
$check_re=mysql_query($checksql,$conn);
$num=mysql_num_rows($check_re);
if($num!=0){
 echo "

";
 echo "该用户名已经存在,请选择另一个
";
    echo "上传失败!
返回";
    echo "
";
 exit();
}
// 方法一:在mysql中保存图片文件,
// 如果有图片文件,打开图片文件,将图片文件中的数据用函数
// addslashes处理,然后传递给变量$data,
// addslashes函数是给字符串加入斜线,使字符串能够顺利写入数据库中
// 这样变量$data 中保存的就是图片文件的数据了
if ($photo""){
 $fp=fopen($photo,"r");
 $data=addslashes(fread($fp,filesize($photo)));
}
$password=md5($password);
$sql="insert into image (id,photo) values('$id','$data')";

mysql_query($sql,$conn) or die ("插入数据失败: ".mysql_error());

// 关闭连接
mysql_close($conn);
// 显示上传图片成功
// 重定向到注册成功页面
header("location:display_image1.php?id=$id");



永久链接:

转载随意!带上文章地址吧。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools