Jump to content

[SOLVED] Rename Function


a1amattyj

Recommended Posts

Hi,

 

Im currently in the process of coding a image upload script. However, i would like it to rename the file to somethign random. Here is my code:

 

 

<?php
$imagename = $_FILES["file"]["name"];
$image2 = "<a href=\"http://www.optclan.info/upload/$imagename\">$imagename</a>";
?>



<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < "$maxsize")
&& ($_FILES["file"]["size"] > "$minsize"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {


    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo "$txt30";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
    echo "$txt32";
    echo "<br /><br /><center>";
    echo "<img src=\"http://www.$txt0/upload/$imagename\" alt=\"Your Image\" max-width=400 max-height=400 />";
    echo "</center><br /><br />";
    echo "Your Image is named: ";
    echo $image2;
    echo "<br />";
    echo "File Size: " . ($_FILES["file"]["size"] / 2024) . " Kb<br /><br /><br />";
    echo "<center>$txt33<br />";
    echo "<textarea name=\"textarea\" cols=\"50\" rows=\"3\">[url=http://$txt0][img]http://$txt0/upload/";
    echo "$imagename";
    echo "[/img]";
    echo "[/url]";
    echo "</textarea><br /><br /><br />";
    echo "<center>$txt34<br />";
    echo "<textarea name=\"textarea\" cols=\"50\" rows=\"3\">http://$txt0/upload/";
    echo "$imagename";
    echo "</textarea><br /><br /><br />";
      }
    }
  }
else
  {
  echo "$txt31";
  }
?>

 

 

So where too would i put the rename function?

rename(oldname,newname)

 

Also, i dont know how to code a random name with numbers and images which is 10 charcs long.

 

Thank you for all!

Link to comment
https://forums.phpfreaks.com/topic/84759-solved-rename-function/
Share on other sites

Change:

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

To:

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

That will give you a random name.

 

Hi papa, thanks it worked.

 

Now, it uploads the file with a random name, how do i get the specific random name so the user knows what it is called?

<?php
$imagename = md5(rand()) . $_FILES["file"]["name"];
$image2 = "<a href=\"http://www.optclan.info/upload/$imagename\">$imagename</a>";
?>



<?php
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/png")
&& ($_FILES["file"]["size"] < "$maxsize")
&& ($_FILES["file"]["size"] > "$minsize"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {

    if (file_exists("upload/" . $imagename))
      {
      echo "$txt30";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $imagename);
    echo "$txt32";
    echo "<br /><br /><center>";
    echo "<img src=\"http://www.$txt0/upload/$imagename\" alt=\"Your Image\" max-width=400 max-height=400 />";
    echo "</center><br /><br />";
    echo "Your Image is named: ";
    echo $image2;
    echo "<br />";
    echo "File Size: " . ($_FILES["file"]["size"] / 2024) . " Kb<br /><br /><br />";
    echo "<center>$txt33<br />";
    echo "<textarea name=\"textarea\" cols=\"50\" rows=\"3\">[url=http://$txt0][img]http://$txt0/upload/";
    echo "$imagename";
    echo "[/img]";
    echo "[/url]";
    echo "</textarea><br /><br /><br />";
    echo "<center>$txt34<br />";
    echo "<textarea name=\"textarea\" cols=\"50\" rows=\"3\">http://$txt0/upload/";
    echo "$imagename";
    echo "</textarea><br /><br /><br />";
      }
    }
  }
else
  {
  echo "$txt31";
  }
?>

Like that.

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

 

Stored in a variable, you could save it in a session, stick it in a cookie, stored in a database logged with the user's IP address, w/e you want xD

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.