用不同的名称上传图片php mysql两次

时间:2012-05-09 21:00:49

标签: php mysql file-upload file-rename

尝试上传图片到服务器(在mysql表中有一个路径)两次通过php 使用不同的名称。图像的一个版本为“xxxx.png”,另一个版本的图像为“xxxxt.png”。

我的php是:

<?php

if ($_FILES['photo']) {
  $target = "images/properties/";  
  $target = $target . basename( $_FILES['photo']['name']); 

  $pic = "images/properties/" .(mysql_real_escape_string($_FILES['photo']['name']));

  if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
    mysql_query("INSERT INTO `images` (`productcode`, `photo`) VALUES ('$productcode', '$pic' )");
    echo "The new image has been added successfully"; 
  } else { 
    echo "Error uploading new image - please check the format and size"; 
  }
}

?> 

上面的代码将图像插入到mysql数据库中,并正确地将文件上传到服务器。 然而,我试图在“缩略图”版本上使用不同的命名约定两次上载相同的图像。我的html中的幻灯片显示脚本只识别缩略图,如果在文件名的末尾以“t”命名,那么我的问题。

我被建议查看php copy()函数来实现这一点,但是如何将这样的函数合并到我现有的代码中是非常不清楚的。 很高兴在需要时提供HTML或任何其他信息。

任何帮助非常感谢。我确实有另一个线程试图发现同样的事情,但我不是很清楚!

由于 JD

2 个答案:

答案 0 :(得分:2)

如果我正确理解你,你不需要两次上传这个文件。您已在服务器上拥有此文件。因此,您应该在服务器的硬盘驱动器和更新数据库上复制它(可选择执行一些转换以使其更像缩略图)。

您的代码应该与此类似:

<?php 
if($_FILES['photo'])
{
  $target_dir = "images/properties/";

  $upload_file_name = basename( $_FILES['photo']['name']);
  $upload_file_ext = pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION);

  $target_file = $target_dir . $upload_file_name . '.' . $upload_file_ext;
  $target_file_sql = $target_dir . mysql_real_escape_string($upload_file_name . '.' . $upload_file_ext);
  $target_thumb = $target_dir . $upload_file_name . 't.' . $upload_file_ext;
  $target_thumb_sql = $target_dir . mysql_real_escape_string($upload_file_name . 't.' . $upload_file_ext);

  if (move_uploaded_file($_FILES['photo']['tmp_name'], $target_file)) 
  {  
    mysql_query("INSERT INTO `images` (`productcode`, `photo`) VALUES ('$productcode', '$target_file_sql' )");
    echo "The new image has been added successfully"; 

    if (copy($target_file, $target_thumb))
    {
        mysql_query("INSERT INTO `images` (`productcode`, `photo`) VALUES ('$productcode', '$target_thumb_sql' )");
        echo "The new thumb image has been added successfully";
    } else 
    {
        echo "Error copying thumb file";
    }

  } else 
  { 
    echo "Error uploading new image - please check the format and size"; 
  }
}

同样,这个想法是你不需要连续两次上传文件。您只需将其复制到服务器上即可。

答案 1 :(得分:1)

正如您所知,您应该使用copy()。我没有对此进行全面测试,但试一试:

<?php

if ($_FILES['photo'])
{
    $target = "images/properties/";

    $ext = array_pop(explode('.', $_FILES['photo']['name']));
    $copy = $target . basename($_FILES['photo']['name'], '.' . $ext) . 't.' . $ext;

    $target = $target . basename($_FILES['photo']['name']);

    $pic = "images/properties/" .(mysql_real_escape_string($_FILES['photo']['name']));

    if (move_uploaded_file($_FILES['photo']['tmp_name'], $target))
    {
        copy($target, $copy);
        mysql_query("INSERT INTO `images` (`productcode`, `photo`) VALUES ('$productcode', '$pic' )");
        echo "The new image has been added successfully";
    }
    else
    {
        echo "Error uploading new image - please check the format and size";
    }
}
?>