Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

File Upload script - Need help.

SpaceshipSpaceship
SpaceshipSpaceship
NamecheapNamecheap
Watch
Ive got a file upload script, how can I make it so it gives a random name or ads a few digits on the end instead of saying "change the filename" all the time.

Code:
<?
//set these variables-----------------------------------------------------------------
$path = "upload/";   //path to your targetfolder after your domain
$max_size = 500000;          //maximum filesize

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>file upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#990000" link="#990000" vlink="#990000" alink="#990000" leftmargin="20" topmargin="20" marginwidth="20" marginheight="20">

<FORM ENCTYPE="multipart/form-data" ACTION="index.php" METHOD="POST">
        <strong><font color="#990000" face="Geneva, Arial, Helvetica, sans-serif">IMAGE (jpg/gif) </font></strong><font color="#990000">:</font>
        <INPUT TYPE="file" NAME="userfile">
        <INPUT TYPE="submit" VALUE="Upload">
</FORM>

<br>
<?

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="#333333" face="Geneva, Arial, Helvetica, sans-serif">Find your file here: <strong><font color="#990000"><a href="http://<? echo $domain; ?>/<? echo "../".$path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?>" target="_blank"><br>
  http://<? echo $domain; ?>/<? echo $path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?><br>
  </a></font></strong><br>
<?
 }
echo "<font color=\"#333333\" 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>";
echo "<br><br><img src=\"http://".$domain."/".$path.$HTTP_POST_FILES['userfile']['name']."\">";
} else { echo "<font color=\"#333333\" face=\"Geneva, Arial, Helvetica, sans-serif\">Verkeerd bestandstype, enkel gif, jpg of png !!!</font><br>\n"; exit; }

}

?>


</body>
</html>
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
heres my script:

PHP:
Rand(1,9999999999)

it generates a random number between 1 and 9999999999 so you can have a lot of files hosted.
 
0
Close
Were would I add that?
Thanks.
 
0
Close
heres a script that I threw together:

The form on the page before:
Code:
<form action="upload.php" method="post" enctype="multipart/form-data" name="form1">
    <table width="414" border="0">
      <tr>
        <td width="158"><span class="style3">File Location: </span></td>
        <td width="246"><input name="uploadedfile" type="file" id="uploadedfile"></td>
      </tr>
    </table>
    <p>
      <input type="submit" name="Submit" value="Upload File">
</p>
  </form>

The php file (upload.php):
PHP:
<?php
 // Where the file is going to be placed temporarly
$target_path = "/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$_FILES['uploadedfile']['tmp_name']; // temp file

$target_path = "./uploads/";
$oldfile =  basename($_FILES['uploadedfile']['name']);


//Check the size
if($_FILES['uploadedfile']['size'] > 500000{
    die( "File too large!");
}

//new file name 

$newfile = Rand(1,9999999999) . $_FILES['uploadedfile']['name'];

// move the file to the final destination
$target_path = $target_path . basename($newfile);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
	 die( "File Uploaded");
} else{
     die("Upload Error");
}
?>

That should work
 
0
Close
Another way to do this is.

PHP:
$uniq = substr( md5(uniqid (rand())), 0, 10 );
$ext = strtolower( substr($_FILES['uploadedfile']['name'], -3));
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $att_path."/".$uniq.".".$ext );

Edited your code:
PHP:
<?php
// Where the file is going to be placed temporarly
$_FILES['uploadedfile']['tmp_name']; // temp file

$target_path = "./uploads/";//Make sure chmodded to 777
$oldfile =  basename($_FILES['uploadedfile']['name']);
$allowed_types = array("gif","jpg","png","jpeg","pdf","bmp");

//Check the size
if($_FILES['uploadedfile']['size'] > 500000{
    die( "File too large!");
}

//Check file type
if(!in_array(stristr($_FILES['uploadedfile']['name'], "."), "$allowed_types")) {
    die( "File type not allowed!");
}

//new file name
$uniq = substr( md5(uniqid (rand())), 0, 10 );
$ext = strtolower( substr($_FILES['uploadedfile']['name'], -3));
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $path."/".$uniq.".".$ext );
?>

I add file type check code as well

Good Luck!
 
0
Close
DomainEasy: flexible LTO options. Close more domain deals with financing.

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Filters for activity feeds
Your news feed, Latest activity, and the activity tab on profiles now have filter buttons: All, Posts, Quick replies, and Other. Your...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back