表单上传图片存储到数据库
我要做一个向数据库写入数据的功能 数据项目有编号 名称之类的 其中有图片
想请教如何上传图片并将地址写在对应表单中 之后提交每一项都存入数据库
回复讨论(解决方案)
具体可查看php上传图片,并将路径写入数据库。
http://blog.163.com/leiliuxing2010@126/blog/static/2986935420099291022486/
具体可查看php上传图片,并将路径写入数据库。
http://blog.163.com/leiliuxing2010@126/blog/static/2986935420099291022486/
求详解
上传图片可以选择很老土的php上传,自行百度。
也可以选择插件,包括专业的上传图片插件(推荐plupload)和文本编辑器插件(推荐kindeditor)(就像最下方提交回复的框就可以插入图片)。
在企业里开发会直接使用开源框架,根本就不需要考虑上传的问题。
如果你要学习,可以先做最原始的php上传
给你百度的, 你应该可以看懂吧
http://zhidao.baidu.com/link?url=wyTYBtPqh5KjrsWVBbElDKEMvlggc3IAXdOvhG9-DX42uZfHPFybSnSqHkLLko-rNIu1gy0_suV3e3RtgbgxKAvp2KKNk_TdPXf8fDMlt-a
有例子,有解释,有代码。
http://blog.csdn.net/fdipzone/article/details/39915131
给你百度的, 你应该可以看懂吧
http://zhidao.baidu.com/link?url=wyTYBtPqh5KjrsWVBbElDKEMvlggc3IAXdOvhG9-DX42uZfHPFybSnSqHkLLko-rNIu1gy0_suV3e3RtgbgxKAvp2KKNk_TdPXf8fDMlt-a
这个看过
我还需要存入数据库并读出图片地址来赋值给表单 请问怎么做
有例子,有解释,有代码。
http://blog.csdn.net/fdipzone/article/details/39915131
这个是将图片转为二进制流存到数据库的
我希望是将图片上传到服务器一个文件夹中,然后表单内赋值图片路径地址
以上通过点击上传 出现选择文件框 选择图片确认 表单出现图片保存的地址 然后填好其他信息 提交
请问有这种的吗
是不是要先上传(用和那个一样的方法) 产生临时文件 有一个绝对路径 然后用move_uploaded_file()移动到我想指定的目录
然后表单内赋值 目录/$图片名称.jpg 这样的啊
这样的话求教如何实现,还是有代码有实感点...初学者见谅
是不是要先上传(用和那个一样的方法) 产生临时文件 有一个绝对路径 然后用move_uploaded_file()移动到我想指定的目录
有例子,有解释,有代码。
http://blog.csdn.net/fdipzone/article/details/39915131
这个是将图片转为二进制流存到数据库的
我希望是将图片上传到服务器一个文件夹中,然后表单内赋值图片路径地址
以上通过点击上传 出现选择文件框 选择图片确认 表单出现图片保存的地址 然后填好其他信息 提交
请问有这种的吗
是不是要先上传(用和那个一样的方法) 产生临时文件 有一个绝对路径 然后用move_uploaded_file()移动到我想指定的目录
然后表单内赋值 目录/$图片名称.jpg 这样的啊
这样的话求教如何实现,还是有代码有实感点...初学者见谅
是不是要先上传(用和那个一样的方法) 产生临时文件 有一个绝对路径 然后用move_uploaded_file()移动到我想指定的目录
是的,只要上传,就需要使用move_uploaded_file来移动到指定目录。
写了个例子:
CREATE TABLE IF NOT EXISTS `member` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `username` varchar(50) NOT NULL, `photo` varchar(200) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
client.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title> Demo </title> </head> <body> <form name="form1" id="form1" method="post" enctype="multipart/form-data" action="server.php"> <p>name:<input type="text" name="username" ></p> <p>photo:<input type="file" name="photo" id="photo"></p> <p><input type="submit" name="b1" value="submit"></p> <input type="hidden" name="send" value="true"> </form> <div id="result"></div> </body> </html>
server.php
<?php //打开数据库 function opendb(){ $conn=@mysql_connect("localhost","root","") or die(mysql_error()); @mysql_select_db('test',$conn) or die(mysql_error()); } //关闭数据库 function closedb(){ @mysql_close() or die("關閉數據庫出錯!"); } opendb(); // 上传图片并提交到数据库 if(isset($_POST['send'])=='true'){ $username = isset($_POST['username'])? $_POST['username'] : ''; $filename = time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.')); if(move_uploaded_file($_FILES['photo']['tmp_name'], $filename)){ $sqlstr = "insert into member(`username`,`photo`) values('".addslashes($username)."','".addslashes($filename)."')"; @mysql_query($sqlstr) or die(mysql_error()); } } echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">'; $sqlstr = "select * from member"; $query = mysql_query($sqlstr) or die(mysql_error()); while($thread=mysql_fetch_assoc($query)){ $result[] = $thread; } if($result){ foreach($result as $val){ echo $val['username'].' <img src="/static/imghw/default1.png" data-src="'.$val['photo'].'" class="lazy" .$val['photo'].'" alt="表单上传图片存储到数据库" ><br>'; } }?>
数据库名,连接用户名密码改为你自己的。
server.php 的 $filename那一段没看明白 那是要移动到的指定路径吗 怎么改
server.php 的 $filename那一段没看明白 那是要移动到的指定路径吗 怎么改
$filename 就是定义,你上传的文件,放在服务器哪里,文件名称是什么。
例如 $filename= "1.jpg";
图片上传后,就会在当前目录生成1.jpg。
server.php 的 $filename那一段没看明白 那是要移动到的指定路径吗 怎么改
$filename 就是定义,你上传的文件,放在服务器哪里,文件名称是什么。
例如 $filename= "1.jpg";
图片上传后,就会在当前目录生成1.jpg。
那上面那个就是放在当前目录,并且名称更改为一个唯一的由数字组成的名称
如果我要保存到 当前目录/wx/ss 文件夹下 那这句要怎么改
$filename = "../wx/ss/time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.'));" 不行 做了些改动也没成功
另外如图 添加页面
name那一行 和submit不要
点击上传后选择好图片确定 图片地址表单中出现图片的路径链接
标题到是否有效都是个表中的 所以想将name,photo两项加进来 不显示
以后编辑只需要再上传一张图片图片地址就改变
另外删除的话怎样保证数据库的记录和文件夹里的图片都删除呢
$filename = time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.'));
改为
$filename = 'wx/ss'. time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.'));
wx/ss 这个文件夹要存在
新手,我是来学习的!
上面的大大给的代码赞,
已经实现表单带上传图片存入数据库了,感动QAQ

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
