上载具有唯一名称的图像(PHP)

上载具有唯一名称的图像(PHP),php,Php,我目前正在编写一个简单的上传脚本 然而,我在用户从iPhone上传图像时遇到了一个问题,因为iPhone将所有图像命名为“image.jpg”,并且不能有两个同名图像 在下面的上传过程中,是否有任何方法向文件名添加随机字符串?我是PHP新手 $allowedExts = array("gif", "jpeg", "jpg", "png"); $temp = explode(".", $_FILES["file"]["name"]); $extension = end($temp); if ((

我目前正在编写一个简单的上传脚本

然而,我在用户从iPhone上传图像时遇到了一个问题,因为iPhone将所有图像命名为“image.jpg”,并且不能有两个同名图像

在下面的上传过程中,是否有任何方法向文件名添加随机字符串?我是PHP新手

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 3000000000)
&& in_array($extension, $allowedExts)) {
  if ($_FILES["file"]["error"] > 0) {
  } else {
    if (file_exists("../../upload/" . $_FILES["file"]["name"])) {
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../../upload/" . $_FILES["file"]["name"]);
      $image_link = 'http://www.martahedin.com/EveryLife/upload/' . $_FILES["file"]["name"];
   echo $image_link;
    }
  }
} else {
  echo "Invalid file";
}
$allowedExts=array(“gif”、“jpeg”、“jpg”、“png”);
$temp=explode(“.”,$_文件[“文件”][“名称”]);
$extension=end($temp);
如果(($_文件[“文件”][“类型”]=“图像/gif”)
||($_文件[“文件”][“类型”]=“图像/jpeg”)
||($_文件[“文件”][“类型”]=“图像/jpg”)
||($_文件[“文件”][“类型”]=“图像/pjpeg”)
||($_文件[“文件”][“类型”]=“图像/x-png”)
||($_文件[“文件”][“类型”]=“图像/png”))
&&($_文件[“文件”][“大小”]<30000000000)
&&in_数组($extension$allowedExts)){
如果($\u文件[“文件”][“错误”]>0){
}否则{
如果(文件存在(“../../upload/”$\u文件[“文件”][“名称”])){
}否则{
移动上传的文件($文件[“文件”][“tmp文件名”],
“../../upload/”$_文件[“文件”][“名称”]);
$image\u link='1http://www.martahedin.com/EveryLife/upload/“.$”文件[“文件”][“名称”];
echo$image\u链接;
}
}
}否则{
回显“无效文件”;
}

此行指定了服务器端文件名:

move_uploaded_file($_FILES["file"]["tmp_name"],
  "../../upload/" . $_FILES["file"]["name"]);
如果要将唯一字符串添加到该文件名,可以将其添加到该行。例如,一个选项可以是在现有文件名前面加上一个唯一的字符串:

move_uploaded_file($_FILES["file"]["tmp_name"],
  "../../upload/" . uniqid() . $_FILES["file"]["name"]);

这里是您的代码修改,以实现您的愿望。将$destination变量更改为所需的任何格式。将$uniquevalue更改为您希望用于唯一性的任何值

$allowedExts = array("gif", "jpeg", "jpg", "png");

// Get the file extension
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

// Get the file name without the extension
$filename = basename($_FILES["file"]["name"], ".$extension");

// Define your unique identifier - this example uses unix timestamp
$uniquevalue = time();

// Combine the above parts in the form you wish
$destination = $filename . '-' . $uniquevalue . '.' . $extension;

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 3000000000)
&& in_array($extension, $allowedExts)) {
  if ($_FILES["file"]["error"] > 0) {
  } else {
    if (file_exists("../../upload/" . $_FILES["file"]["name"])) {
    } else {
      // Changed $_FILES["file"]["name"] to $destination

      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../../upload/" . $destination);
      $image_link = 'http://www.martahedin.com/EveryLife/upload/' . $destination;
   echo $image_link;
    }
  }
} else {
  echo "Invalid file";
}
$allowedExts=array(“gif”、“jpeg”、“jpg”、“png”);
//获取文件扩展名
$extension=pathinfo($\u FILES['file']['name'],pathinfo\u扩展名);
//获取不带扩展名的文件名
$filename=basename($_FILES[“file”][“name”],“$extension”);
//定义您的唯一标识符-此示例使用unix时间戳
$uniquevalue=time();
//以您希望的形式组合上述部分
$destination=$filename.'-'$唯一值$扩大
如果(($_文件[“文件”][“类型”]=“图像/gif”)
||($_文件[“文件”][“类型”]=“图像/jpeg”)
||($_文件[“文件”][“类型”]=“图像/jpg”)
||($_文件[“文件”][“类型”]=“图像/pjpeg”)
||($_文件[“文件”][“类型”]=“图像/x-png”)
||($_文件[“文件”][“类型”]=“图像/png”))
&&($_文件[“文件”][“大小”]<30000000000)
&&in_数组($extension$allowedExts)){
如果($\u文件[“文件”][“错误”]>0){
}否则{
如果(文件存在(“../../upload/”$\u文件[“文件”][“名称”])){
}否则{
//将$\u文件[“文件”][“名称”]更改为$destination
移动上传的文件($文件[“文件”][“tmp文件名”],
“../../upload/”$destination);
$image\u link='1http://www.martahedin.com/EveryLife/upload/“.$目的地;
echo$image\u链接;
}
}
}否则{
回显“无效文件”;
}

如果您只想使用不同的文件名,请尝试: 兰特(09999)

像这样:

 move_uploaded_file($_FILES["file"]["tmp_name"],
      "../../upload/" .rand(0,99999). $_FILES["file"]["name"]);
更新:

$newName = rand(0,99999). $_FILES["file"]["name"];

move_uploaded_file($_FILES["file"]["tmp_name"],
      "../../upload/" . $newName);

$image_link = 'http://www.martahedin.com/EveryLife/upload/' . $newName;


   echo $image_link;

您可以更改这部分代码

move_uploaded_file($_FILES["file"]["tmp_name"], "../../upload/" . $_FILES["file"]["name"]);


希望对您有所帮助

该文件仍被命名为“image.jpg”。。。有什么想法吗?我想你可能称之为错误的,你能发布你的更新代码吗?对不起。图像有唯一的名称,但$image_链接;仍然是image.jpg,即使该文件被称为其他文件。有什么想法吗?不客气:)那个否决我的人能告诉我为什么吗?
move_uploaded_file($_FILES["file"]["tmp_name"], "../../upload/" . uniqid() . "_" . $_FILES["file"]["name"]);