[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 07-04-2006, 09:49 PM   #1 (permalink)
Account Closed
 
hexthem4n's Avatar
 
Join Date: Jan 2005
Posts: 78
1.00 NP$ (Donate)

hexthem4n is on a distinguished road


Need PHP Help.

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 Code:
<?
//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">&lt;img src=&quot;http://<? echo $domain; ?>/<? echo $path; ?><? echo $HTTP_POST_FILES['userfile']['name']; ?>&quot;&gt;</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>";
}

}

?>
hexthem4n is offline  
Old 07-05-2006, 04:08 AM   #2 (permalink)
NamePros Regular
 
Noobie's Avatar
 
Join Date: Feb 2006
Location: Montreal, Quebec, Canada
Posts: 324
66.75 NP$ (Donate)

Noobie is on a distinguished road


you can use uniqid()
__________________
Goldkey.com is a scam
What's your BMI? | Timestamp Generator
Noobie is offline  
Old 07-05-2006, 07:45 PM   #3 (permalink)
New Member
 
Join Date: Jul 2006
Posts: 13
0.00 NP$ (Donate)

bttfpromo is an unknown quantity at this point


Ahahaha....I have this done on my own website... You'll need to change variables and what's highlighted.



PHP Code:
        $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;
bttfpromo is offline  
Old 07-07-2006, 12:47 AM   #4 (permalink)
NamePros Regular
 
-PS-'s Avatar
 
Join Date: Jun 2006
Location: Sydney
Posts: 251
5.95 NP$ (Donate)

-PS- will become famous soon enough-PS- will become famous soon enough


Quote:
Originally Posted by bttfpromo
Ahahaha....I have this done on my own website... You'll need to change variables and what's highlighted.



PHP Code:
        $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.
-PS- is offline  
Old 07-07-2006, 02:39 AM   #5 (permalink)
Account Closed
 
hexthem4n's Avatar
 
Join Date: Jan 2005
Posts: 78
1.00 NP$ (Donate)

hexthem4n is on a distinguished road


Ya but how can I make it show the correct link after it uploads?
hexthem4n is offline  
Old 07-07-2006, 09:09 AM   #6 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
What is the URL supposed to be when they upload?

Like http://site.com/image.php?image=_______ ??
Dan is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 03:46 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85