【发布时间】:2012-05-18 08:05:27
【问题描述】:
我有一个上传到 php 中的 mysql 脚本,该脚本非常适合物业网站 - 插入所有相关字段和主图像。
但问题是,此图像用于将缩略图标识为 xxxxxt.png 的幻灯片中,例如,主图像为 xxxxx.png。
我的php代码是:
<?php include 'dbc.php'; page_protect();
if(!checkAdmin()) {header("Location: login.php");
exit();
}
$host = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF']));
$path = rtrim($login_path, '/\\');
foreach($_GET as $key => $value) {
$get[$key] = filter($value);
}
foreach($_POST as $key => $value) {
$post[$key] = filter($value);
}
$uniqid = md5(uniqid(mt_rand()));
?>
<?php
if($_FILES['photo']) //check if we uploading a file
{
$target = "images/properties/";
$target = $target . basename( $_FILES['photo']['name']);
$title = mysql_real_escape_string($_POST['title']);
$desc = mysql_real_escape_string($_POST['desc']);
$extra = mysql_real_escape_string($_POST['extra']);
$postcode = mysql_real_escape_string($_POST['postcode']);
$price = mysql_real_escape_string($_POST['price']);
$pandp = mysql_real_escape_string($_POST['pandp']);
$pic = "images/properties/" .(mysql_real_escape_string($_FILES['photo']['name']));
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{
mysql_query("INSERT INTO `furnishings` (`title`, `postcode`, `desc`, `extra`, `productcode`, `price`, `status`, `pandp`, `photo`) VALUES ('$title', '$postcode', '$desc', '$extra', '" . $uniqid . "', '$price', 'tolet.png', '$pandp', '$pic' )") ;
echo "The property has been added to the lettings portfolio";
}
else
{
echo "Error uploading new property - please ensure all the fields are correctly entered and the file is an image";
}
}
?>
上传表单的html代码为:
<form enctype="multipart/form-data" action="addlet.php" method="POST">
<table width="600px" border="2" cellpadding="5"class="myaccount">
<tr>
<td width="135">Title: </td>
<td width="427"><input name="title" type="text" size="40"/></td>
</tr>
<tr>
<td>Description: </td>
<td><textarea name = "desc" rows="3" cols="40"></textarea></td>
</tr>
<tr>
<td>Property Features: </td>
<td><textarea name = "extra" rows="3" cols="40"></textarea></td>
</tr>
<tr>
<td>Postcode: </td>
<td><input name = "postcode" type="text" size="40" /></td>
</tr>
<tr>
<td>Price per week (£): </td>
<td><input name = "price" type="text" size="40" /></td>
</tr>
<tr>
<td>Furnished/Unfurnished: </td>
<td><input name = "pandp" type="text" size="40" /></td>
</tr>
<tr>
<td>Main Image: </td>
<td><input type="file" name="photo" class="forms" /></td>
</tr> </table></p>
<p> <input type="submit" class="CMSbutton" value="Add" /></p>
</form>
是否有一种简单的方法可以添加额外的代码行,将两个图像插入服务器上的所需目标,(images/properties/) - 一个是图像的原始名称,一个是缩略图版本(带有图像名称末尾的“t”)。
由于它们都相当小,我不会因为代码已经完成而大惊小怪,我不想重新构建所有内容!
非常感谢任何帮助
谢谢 京东
【问题讨论】:
-
我不明白。您是否尝试使用与全尺寸图像和缩略图相同的图像,还是将图像调整为缩略图大小?
-
这很简单 - 编写代码告诉 PHP 将文件移动到哪里。您已经获得了基本的
move_uploaded_file()调用 - PHP 将移动文件放置在何处完全取决于 您。 -
我只是想将图像的 2 个版本上传到服务器 - 一个名为 image.png,一个名为 imaget.png....抱歉,如果我不清楚
-
为什么这个问题得了负分!?它遵循所有准则,我只是在一些帮助下!
-
image.png和image.png会是同一张图片但名称不同吗?
标签: php mysql thumbnails image-uploading