dozabg1616 2013-02-03 17:14
浏览 70
已采纳

php逻辑问题:上传文件和更改名称

I am new to php and trying to figure out how to perform the following process: Using the below code to upload images submitted by users, I want to change the name of the file based on user session data, and if the newly named file already exists, then replace it with the current upload.

At the point where the following snippet is taken from is where I think this process should happen. However, instead of checking if the file exists, I want to just accept the file, rename it to something like ($_GET['session_name'] . "-avatar" . $extension), and if that file already exists in the uploads folder, then replace it with the current uploaded image. And finally, the file's extension shouldn't be a factor in determining a files existence, this way only one file per user will exist no matter the type of file.

// edit following to rename file, and if exists already, replace with current
if (file_exists("uploads/" . $_FILES["file"]["name"]))
  {
  echo $_FILES["file"]["name"] . " already exists. ";
  }
else
  {
  move_uploaded_file($_FILES["file"]["tmp_name"],
  "uploads/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
  }

Complete code below (source):

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
  • 写回答

2条回答 默认 最新

  • dongzhang8680 2013-02-03 17:20
    关注

    just change

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

    to

    move_uploaded_file($_FILES['file']['tmp_name'], 'upload/'.$UserSessionData);
    

    to rename the file to your user session data thingy. Do the same with if(file_exists("uploads/" . $_FILES["file"]["name"])) [...].

    btw: You should use singlequotes (') in php, because doublequotes (") are parsed by php and therefore slower than singlequotes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
  • ¥15 r语言神经网络自变量重要性分析
  • ¥15 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?