使用不同的名称上载两次图像php mysql

使用不同的名称上载两次图像php mysql,php,mysql,file-upload,file-rename,Php,Mysql,File Upload,File Rename,我试图通过php两次以不同的名称将图像上传到服务器(mysql表中有一个路径)。图像的一个版本为“xxxx.png”,另一个版本为“xxxxt.png” 我的php是: <?php if ($_FILES['photo']) { $target = "images/properties/"; $target = $target . basename( $_FILES['photo']['name']); $pic = "images/properties/" .(my

我试图通过php两次以不同的名称将图像上传到服务器(mysql表中有一个路径)。图像的一个版本为“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"; 
  }
}

?> 

正如建议您使用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";
    }
}
?>

如果我正确理解您的意思,您无需将此文件上载两次。您的服务器上已经有此文件。所以,您应该将其复制到服务器的硬盘上(可以选择进行一些转换,使其更像缩略图),并更新数据库

您的代码应该与此类似:

<?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"; 
  }
}

这可能会有所帮助:如果操作系统支持,您可以使用symlink()而不是copy()。这样可以节省磁盘空间。我相信参数是一样的。