upload few files and have them rename with rand() php coding

rated by 0 users
This post has 3 Replies | 2 Followers

Top 25 Contributor
Points 301
SnakZ Posted: 12-07-2010 12:52 PM

Ok para u always been the come to guy for things like this so how about u ? lol and any one who think they may know how to code this together

I have 2 sites here that does what i need them to do but i dont know how to put the 2 codes together  

i got it to upload all files and used rand() to give the files a # but it ends up giving them all the same # lol so they overwrite them selfs lol


This site shows how to upload mltiple fiels with php.
http://www.ehow.com/how_6345068_uplo...files-php.html


This site shows how to rename only 1 file uploaded with rand()
http://php.about.com/od/advancedphp/...ame_upload.htm


i pray this is not to hard to do and if any one gets bored after words can you show how to block a file type from being
uploaded ? like php/exe types

PS)i would call my self a php newbe lol and ty you for your time and help

Code from http://php.about.com/od/advancedphp/...ame_upload.htm

<form enctype="multipart/form-data" action="upload.php" method="POST"> 
Please choose a file: <input name="uploaded" type="file" /><br /> 
<input type="submit" value="Upload" /> 
</form> 


//This function separates the extension from the rest of the file name and returns it  
function findexts ($filename) 
{ $filename = strtolower($filename) ; 
$exts = split("[/\\.]", $filename) ; 
$n = count($exts)-1; $exts = $exts[$n]; 
return $exts; 
} 

//This applies the function to our file  
$ext = findexts ($_FILES['uploaded']['name']) ;

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.  
$ran = rand () ; 
//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended. 
$ran2 = $ran."."; 
//This assigns the subdirectory you want to save into... make sure it exists! 
$target = "images/";
//This combines the directory, the random file name, and the extension 
$target = $target . $ran2.$ext; 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { 
echo "The file has been uploaded as ".$ran2.$ext; } 
else { 
echo "Sorry, there was a problem uploading your file."; 
}

The code from http://www.ehow.com/how_6345068_uplo...files-php.html
 
<form action="upload.php" method="post" enctype="multipart/form-data">
File: <input type="file" name="file[]"/><br/>
File: <input type="file" name="file[]"/><br/>
File: <input type="file" name="file[]"/><br/>
<input type="submit" name="submit" value="Upload"/>
</form>



$destpath = "upload/" ;


while(list($key,$value) = each($_FILES["file"]["name"])) {
if(!empty($value)){
f ($_FILES["file"]["error"][$key] > 0) {
echo "Error: " . $_FILES["file"]["error"][$key] . "<br/>" ;
}
else {
$source = $_FILES["file"]["tmp_name"][$key] ;
$filename = $_FILES["file"]["name"][$key] ;
move_uploaded_file($source, $destpath . $filename) ;
echo "Uploaded: " . $destpath . $filename . "<br/>" ;
}
}
}

  • | Post Points: 23
Top 25 Contributor
Points 301

using the code in this way look like it worked for me only thing now is how to make it look to see if the file type is right lol

 

Change: i used the form from the e-how site then used the codes for renaming but with the other part of it i just rename all the lines of codes with a "z" lol what in the end made it work but i recall that i try this before and it didnt work ...hmm idk but whatever it works

if you know of any better way to do this then plz tell :)

 

$ext = findexts  ($_FILES['file']['name']) ;

$extz = findexts  ($_FILES['filez']['name']) ;

$ran = rand () ; 

$ranz = rand () ; 

$ran2 = $ran."."; 

$ran2z = $ranz."."; 

$target = "upload/";

$target = "upload/";

$target = $target . $ran2.$ext;

$target = $target . $ran2z.$extz; 

 

if(move_uploaded_file($_FILES['file']['tmp_name'], $target)) { 

echo "The file has been uploaded as ".$ran2.$ext; 

} else { 

echo "Sorry, there was a problem uploading your file.<br>"; 

}

if(move_uploaded_file($_FILES['filez']['tmp_name'], $target)) { 

echo "The file has been uploaded as ".$ran2z.$extz; 

} else { 

echo "Sorry, there was a problem uploading your file."; 

}

  • | Post Points: 5

Easiest way to rename files is to just name them sequentially, instead of random. Which unless you make the number large enough you will always risk duplicate names.

Start with any number you want then just add 1 to it. You will need to be able to either read the file names from the file system or store the "Seed" in a database or file. You can't just store it in memory because when you restart the web app memory will be cleared. It requires a persistent store.

You would use a random file name if you were generating a one time use download link to a file. in that case you would make an internal reference to the real file name and the random one. once the file is downloade delete the reference.

I hope this helps you out!

Feel free to jump in the Teamspeak server if you need any of it clarified.

www.ParadisesGarage.com 
Remember... Where ever you go, there you are...

  • | Post Points: 23
Top 25 Contributor
Points 301

for me the thing is i dont know how to code in php lol its a video uploader thing so im trying to make sure that videos that are uploaded cant overwrite the other  what if u have let say 200 videos and one gets overwrite its going to be harder to tell what one it was

one way i was thinking of is to use random plus a timestamp some how this one guy made it so the file name stayed with it not sure how he did it lol 

he talked about using a md5 ? not sure how to use that yet lol

  • | Post Points: 5
Page 1 of 1 (4 items) | RSS
Copyright {MC}ParaDOX