-2

我有一个上传到 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 (&pound;): </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 " 在图像名称的末尾)。

因为它们都相当小,所以我不会对调整缩略图的大小大惊小怪,因为代码已经完成了,我不想重建所有东西!

非常感谢任何帮助

谢谢京东

4

1 回答 1

0

如果您的图像文件被成功移动到位,我会采取这个策略:创建列is_uploadedis_thumb_createdis_image_created在您的数据库表中。成功上传并移动后,设置第一个。

然后运行 ​​cron 或其他后台系统,从上传的图像生成“主”和“拇指”视图(请记住,上传的图像对于普通屏幕大小的图片可能太大)。成功生成这些图像后,可以将相关列设置为“完成”,并且在发生这种情况之前,该行保持非活动状态。

顺便说一句,这种方法的可扩展性要大得多,因为它不会因昂贵的图像处理而阻塞您的 Web 请求。

于 2012-05-09T21:32:57.857 回答