Jump to content

Automatically creating file names on upload


Dryan

Recommended Posts

I have some php here that allows me to upload a .zip file to my server in /upload. It checks the filename to see if it already exists, and returns an 'invalid file' error if the file is already there. If the filename isn't used, it adds the .zip to my server as whatever name it was uploaded as.

 

I'd like to change it so that it checks to see if 1.zip already exists on my server, if it doesn't exist, I'd like the uploaded file to automatically be named 1.zip. If 1.zip already exists, I want it to save as 2.zip. If 2.zip exists, then save as 3.zip, etc.

 

<?php
error_reporting(E_ALL);
if (($_FILES["file"]["type"] == "application/zip")
&& ($_FILES["file"]["size"] < 4000000))
  {
  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";
  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.