【问题标题】:include thumbnail upload in php sql code在 php sql 代码中包含缩略图上传
【发布时间】: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 (&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”)。

由于它们都相当小,我不会因为代码已经完成而大惊小怪,我不想重新构建所有内容!

非常感谢任何帮助

谢谢 京东

【问题讨论】:

  • 我不明白。您是否尝试使用与全尺寸图像和缩略图相同的图像,还是将图像调整为缩略图大小?
  • 这很简单 - 编写代码告诉 PHP 将文件移动到哪里。您已经获得了基本的 move_uploaded_file() 调用 - PHP 将移动文件放置在何处完全取决于
  • 我只是想将图像的 2 个版本上传到服务器 - 一个名为 image.png,一个名为 imaget.png....抱歉,如果我不清楚
  • 为什么这个问题得了负分!?它遵循所有准则,我只是在一些帮助下!
  • image.pngimage.png 会是同一张图片但名称不同吗?

标签: php mysql thumbnails image-uploading


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2013-03-18
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    • 2019-01-06
    • 2013-05-14
    • 2018-11-05
    • 2011-10-30
    • 2012-09-21
    相关资源
    最近更新 更多