[advanced search]
Results from the most recent live auction are here.
16 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Programming > CODE
User Name
Password

Old 08-13-2005, 12:07 PM   · #1
Ericsson
Senior Member
 
Location: Belfast
Trader Rating: (55)
Join Date: Aug 2005
Posts: 1,196
NP$: 5.85 (Donate)
Ericsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to behold
Save a Life Child Abuse Cystic Fibrosis Third World Education AIDS/HIV Third World Education Save a Life Save a Life Child Abuse
File Upload script - Need help.

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>


Please register or log-in into NamePros to hide ads
Ericsson is offline   Reply With Quote
Old 08-13-2005, 12:33 PM   · #2
Barrucadu
Formally Mikor.
 
Barrucadu's Avatar
 
Name: Michael Walker
Location: East Yorkshire, England
Trader Rating: (7)
Join Date: Aug 2005
Posts: 2,546
NP$: 171.25 (Donate)
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
heres my script:

PHP Code:
Rand(1,9999999999)


it generates a random number between 1 and 9999999999 so you can have a lot of files hosted.
__________________
Me | Last.fm | F@h | Archlinux.co.uk

archlinux User
Barrucadu is offline   Reply With Quote
Old 08-13-2005, 01:25 PM   · #3
Ericsson
Senior Member
 
Location: Belfast
Trader Rating: (55)
Join Date: Aug 2005
Posts: 1,196
NP$: 5.85 (Donate)
Ericsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to beholdEricsson is a splendid one to behold
Save a Life Child Abuse Cystic Fibrosis Third World Education AIDS/HIV Third World Education Save a Life Save a Life Child Abuse
Were would I add that?
Thanks.
Ericsson is offline   Reply With Quote
Old 08-13-2005, 01:34 PM   · #4
Barrucadu
Formally Mikor.
 
Barrucadu's Avatar
 
Name: Michael Walker
Location: East Yorkshire, England
Trader Rating: (7)
Join Date: Aug 2005
Posts: 2,546
NP$: 171.25 (Donate)
Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold
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 Code:
<?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
__________________
Me | Last.fm | F@h | Archlinux.co.uk

archlinux User
Barrucadu is offline   Reply With Quote
Old 08-14-2005, 07:51 PM   · #5
iNod
Eating Pie
 
iNod's Avatar
 
Name: Steve
Location: Canada
Trader Rating: (66)
Join Date: Nov 2004
Posts: 2,284
NP$: 91.30 (Donate)
iNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud ofiNod has much to be proud of
Special Olympics AIDS/HIV Cystic Fibrosis Save The Children Baby Health Cystic Fibrosis
Another way to do this is.

PHP Code:
$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 Code:
<?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!
__________________
RegisterDub.com - 200th Customer Milestone
ZuneParts.net - International Zune Parts and Accessories
FeaturedFont.com - One Top Free Font a Day!
iNod is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for Form File Upload Script Kfkm09 Programming 0 12-27-2004 06:40 AM
60.000 Templates, scripts, fonts, banners etc. $9.95 atkims Web Development Wanted 19 11-16-2004 09:48 AM
file upload csmaster2005 Programming 8 06-18-2004 05:43 AM

Site Sponsors
Traffic Down Under EscrowDNS Traffic Down Under
Advertise your business at NamePros
All times are GMT -7. The time now is 10:06 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0