NameSilo

Need PHP Help.

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

hexthem4n

Account Closed
Impact
2
I need the following code to put a random number onto the file name. Like say you upload image.jpg, I want it to be like 213image.jpg after its uploaded. So that I dont have to worry about people getting the re-name your image error. Any help would be awsome! Here is the code.

PHP:
<?
//set these variables-----------------------------------------------------------------
$path = "test/uploads/";   //path to your targetfolder after your domain
$max_size = 1000000;          //maximum filesize

//optionally
$domain = $_SERVER["HTTP_HOST"];      //your domainname - change if necessary like "www.wza.be"

//------------------------------------------------------------------------------------
?>

<?

if (!isset($HTTP_POST_FILES['userfile'])) exit;

if (is_uploaded_file($HTTP_POST_FILES['userfile']['tmp_name'])) {

if ($HTTP_POST_FILES['userfile']['size']>$max_size) {
        echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">File is too big !</font><br>\n"; exit; }
if (($HTTP_POST_FILES['userfile']['type']=="image/gif") || ($HTTP_POST_FILES['userfile']['type']=="image/pjpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/jpeg") || ($HTTP_POST_FILES['userfile']['type']=="image/png")) {

        if (file_exists("../".$path . $HTTP_POST_FILES['userfile']['name'])) {
                echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">There already exists a file with this name, please rename your file and try again</font><br>\n"; exit; }

        $res = copy($HTTP_POST_FILES['userfile']['tmp_name'], "../".$path .$HTTP_POST_FILES['userfile']['name']);

        if (!$res) { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">Didn't work, please try again</font><br>\n"; exit; } else {
        ?>
<br>
<p> <font color="#FFFFFF" face="Geneva, Arial, Helvetica, sans-serif"><strong>Find 
          your file here: </strong></font><font color="#333333" face="Geneva, Arial, Helvetica, sans-serif"><strong><font color="#990000"><a href="http://<? echo $domain; ?>/<? echo "../".$path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?>" target="_blank"><br>
          </a></font></strong><font color="#990000"><a href="http://<? echo $domain; ?>/<? echo "../".$path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?>" target="_blank"><font color="#B6B66D">http://<? echo $domain; ?>/<? echo $path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?></font></a><strong><a href="http://<? echo $domain; ?>/<? echo "../".$path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?>" target="_blank"><br>
          </a></strong></font><br>
          <strong><font color="#FFFFFF">HTML:</font></strong><br>
          <font color="#B6B66D"><img src="http://<? echo $domain; ?>/<? echo $path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?>"></font><br>
  <br>
          <font color="#FFFFFF"><strong>BBCode: </strong><strong></strong></font><font color="#990000"><strong><br>
          </strong> <font color="#B6B66D">[img]http://<? echo $domain; ?>/<? echo $path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?>[/img]</font></font></font></p>
<?
 }
echo "<font color=\"#B6B66D\" face=\"Geneva, Arial, Helvetica, sans-serif\"><hr>";
echo "Name: ".$HTTP_POST_FILES['userfile']['name']."<br>\n";
echo "Size: ".$HTTP_POST_FILES['userfile']['size']." bytes<br>\n";
echo "Type: ".$HTTP_POST_FILES['userfile']['type']."<br>\n";
echo "</font>";
}

}

?>
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
0
Close
Ahahaha....I have this done on my own website... You'll need to change variables and what's highlighted.



PHP:
		$micro=substr (md5 (microtime ()), 1, 3);
    rename('[b]files/[/b]'.$uploaded_file_name, '[b]files/[/b]'.$micro. $uploaded_file_name);
	$uploaded_file_name=$micro.$uploaded_file_name;
 
0
Close
bttfpromo said:
Ahahaha....I have this done on my own website... You'll need to change variables and what's highlighted.



PHP:
		$micro=substr (md5 (microtime ()), 1, 3);
    rename('[b]files/[/b]'.$uploaded_file_name, '[b]files/[/b]'.$micro. $uploaded_file_name);
	$uploaded_file_name=$micro.$uploaded_file_name;


i would recomend changing $micro=substr(...) to something like $micro=rand(1,999) if you wish to keep the orginal file name in the "name". md5 makes hex numbers, meaning you can get the letters a-f in there as well FYI.
 
0
Close
Ya but how can I make it show the correct link after it uploads?
 
0
Close
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back