Jump to content

PHP Upload script, random numbers


JimmyJam

Recommended Posts

Hi!

 

How can i get this script:

 

<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
&& ($_FILES["file"]["size"] < 20000))
  {
  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";
  }
?>

 

To add random numbers after the file name, so if people upload and happen to have named it the same thing, the random numbers after the file name will fix the problem?

Link to comment
https://forums.phpfreaks.com/topic/85083-php-upload-script-random-numbers/
Share on other sites

look up math properties on php.net

 

notably:

 

rand  - http://us3.php.net/manual/en/function.rand.php

srand  - http://us3.php.net/manual/en/function.srand.php

mt-rand  - http://us3.php.net/manual/en/function.mt-rand.php

 

Check the comments on them for good usage techniques also!

I usually do...

 

 


if(file_exists("upload/".$filename)){
$filename = explode(".",$filename); // << old way of doing this
$filename = $filename[0].rand(300,7000).$filename[1];
$filename = str_replace("%20","_",$filename);
$filename = str_replace(" ","_",$filename);
}

 

Although i have created my own script for this, much more complex and efficient. :D

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.