通过 PHP 上传图像和缩略图
所以我需要一个非常简单的脚本来调整图像大小并在 PHP 中上传缩略图。请不要推荐库,因为我宁愿能够轻松地编辑它并从中学习。
我自己编写了一个小的 PHP 脚本来上传图像并以其在数据库中的 ID 命名它,并打算使拇指像在名称末尾添加“thumb”一样简单(例如 123thumb.jpg),但我试图在缩略图中工作却有点毁了它。
它看起来是这样的:
mysql_query('
INSERT INTO art (
artist,
title,
extension)
VALUES (
1,
"Penguins",
"'.end(explode('.',$_FILES['art']['name'])).'")') ;
move_uploaded_file($_FILES['art']['tmp_name'],'images/'.mysql_insert_id()..end(explode('.',$_FILES['art']['name']))
So I need a very simple script to resize an image and upload the thumbnail in PHP. Please don't recommend a library because I'd rather be able to edit and learn from it with ease.
I had written a small PHP script on my own to upload an image and name it after its ID in the database and intended to make the thumb as simple as adding "thumb" to the end of the name (such as 123thumb.jpg), but I sort of ruined it trying to work in thumbnails.
Here's what it somewhat looked like:
mysql_query('
INSERT INTO art (
artist,
title,
extension)
VALUES (
1,
"Penguins",
"'.end(explode('.',$_FILES['art']['name'])).'")') ;
move_uploaded_file($_FILES['art']['tmp_name'],'images/'.mysql_insert_id()..end(explode('.',$_FILES['art']['name']))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
糟糕的设计。 1. 没有对查询进行错误检查,并在查询中使用原始用户提供的数据,导致存在 SQL 注入漏洞。 2. 使用原始用户提供的文件名来存储文件,即使它前面确实有一个数据库 ID。 3. 盲目地假设插入查询有效,这样 msyql_insert_id 可能不会返回任何内容。 3. 盲目假设移动命令有效。
更好/更安全的方法是:
可以进行更多/更好的验证/错误处理,但这是比您的版本更安全/更可靠的东西的骨架。
Bad bad design. 1. No error checking on the query and using raw user-provided data in the query, leading to a gaping sql injection vulnerability. 2. Using raw user-provided filenames to store the files in, even if it does have a database ID prepended. 3. Blindly assuming the insert query worked, such that msyql_insert_id might not return anything. 3. blindly assuming the move command works.
A better/safer methodology is:
There's more/better validation/error handling that can be done, but this is the skeleton of something far safer/more reliable than your version.
除了脚本的所有潜在利用之外(请参阅其他答案),此页面可能对于教您如何调整图像大小以获得缩略图非常有帮助。您必须使用 GD(内置于 PHP)之类的库来调整图像大小。
http://salman-w.blogspot.com /2008/10/resize-images-using-phpgd-library.html
(在另一个SO问题中找到它。)
编辑:当然,不要忘记 PHP 手册。 http://php.net/manual/en/book.image.php
Besides all of the potential exploits of your script (see other answer) this page might be very helpful for teaching you how to resize the image to get a thumbnail. You will have to use a library like GD (built in to PHP) to resize the image.
http://salman-w.blogspot.com/2008/10/resize-images-using-phpgd-library.html
(Found it in another SO question.)
EDIT: And, of course, don't forget the PHP manual. http://php.net/manual/en/book.image.php