Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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";
  }
?>
share|improve this question
You can use the glob() function to look for filenames matching a pattern. So remove the extension from the filename, and use glob($basename.'.*'). – Barmar Feb 3 at 17:29

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
Thanks for this! but what about append the file extension to the custom name? fileName = $_SESSION['user_name'] . '-avatar' . $needFileExtension; – lambneck Feb 3 at 18:31
got it! $ext = end(explode(".", $_FILES["file"]["name"])); $avatar = $_SESSION['user_name'] . '-avatar' . '.' . $ext; – lambneck Feb 3 at 21:14

To check that the file exists without looking at the extension your have two easy solutions :

  • Create a directory per user and put the file in it, so you can iterate or count users files.
  • Use a database
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.