【问题标题】:Upload image php mysql twice with different names两次使用不同的名称上传图像php mysql
【发布时间】:2012-05-10 07:14:48
【问题描述】:

我正在尝试将图像上传到服务器(在 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 或任何其他信息。

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

谢谢 京东

【问题讨论】:

标签: php mysql file-upload file-rename


【解决方案1】:

如果我理解正确,您不需要上传此文件两次。您的服务器上已经有了这个文件。所以你应该把它复制到你服务器的硬盘上(可以选择做一些转换,让它更像一个缩略图)并更新数据库。

您的代码应该与此类似:

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

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

【讨论】:

    【解决方案2】:

    正如您所知道的,您应该使用 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";
        }
    }
    ?>
    

    【讨论】:

    • 更好的是,如果操作系统支持,您可以使用 symlink() 而不是 copy()。这将节省磁盘空间。我相信参数是一样的。
    猜你喜欢
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 2021-02-03
    • 2015-10-04
    • 2018-10-10
    • 1970-01-01
    • 2015-04-02
    • 1970-01-01
    相关资源
    最近更新 更多