1

我正在尝试通过具有不同名称的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 或任何其他信息。

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

谢谢京东

4

2 回答 2

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

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

于 2012-05-09T21:49:43.313 回答
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";
    }
}
?>
于 2012-05-09T21:19:25.327 回答