上传图像 php mysql 两次,名称不同


Upload image php mysql twice with different names

我正在尝试通过不同名称的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"; 
  }
}
?> 

上面的代码将图像插入 mysql 数据库并将文件正确上传到服务器。但是,我正在尝试在"缩略图"版本上使用不同的命名约定上传相同的图像两次。我的 html 中的幻灯片脚本仅在文件名末尾以"t"命名时才识别缩略图,因此我的问题。

有人建议我查看 php copy(( 函数来实现这一点,但不清楚如何将这样的函数合并到我现有的代码中。如果需要,很乐意提供html或任何其他信息。

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

谢谢京东

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

您的代码应如下所示:

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

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

正如你被告知的,你应该使用 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";
    }
}
?>