smc Posted April 27, 2006 Share Posted April 27, 2006 Alright so I'm making a file uploader script. What I need to be able to do is randomize the name of the file when it copies it to the server, while at the same time making sure that it dosen't randomize to something that is already taken.To be honest, I haven't a clue where to start... and I really don't know how to check to see if that file name exsists... would I have to make a thing that enters the file name in MySQL and then checks to see if the file name is taken?Thanks for all your help, Below is my code-SMC[code]<?php//Defines variables for use later on$formurl = "/upload/";$linkurl = "../uploaded/$file_name";$displayurl = "/uploaded/$file_name";$invalidurl = "/upload/invalid.html";$nofileurl = "/upload/nofile.html";$errorurl = "/upload/error.html";$imagedisplay = "$linkurl";//Checks to make sure the file existsif($file_name !=""){//Checking to see that the files is a JPGif ($file_type == "image/jpeg"){//Copies the file into the appropriate folder, if it can't it will redirect the browser to an error pagecopy ("$file", "../uploaded/$file_name") or header("Location:$errorurl");}//If the image isn't a jpg, a pre-made image will take it's placeelse { $imagedisplay = "/upload/file.jpg";}}//If the file wasen't there, then this will redirect the browser to an error pageelse { header("Location: $nofileurl"); exit;}?>//The HTML part of it all<html><center><head><title>PCF Upload Centre :: Upload Complete</title></head><body bgcolor="#000000" link="#465263" vlink="#465263" alink="#C0C0C0"><center><img border="0" src="../upload/datauploadgfx.jpg" width="469" height="114"></center><table border="1" width="480" height="26"> <tr> <td height="26" width="470"><b><font color="#FFFFFF" size="5">File upload succeeded...</font></b></td> </tr> <tr> <td height="26" width="470"><font color="#FFFFFF"><ul><li><p align="left"><font color="#FFFFFF">Sent: <?php echo "$file_name"; ?><li><p align="left"><font color="#FFFFFF">Size: <?php echo "$file_size"; ?> bytes<li><p align="left"><font color="#FFFFFF">Type: <?php echo "$file_type"; ?></font></ul> </td> </tr> <tr> <td height="26" width="470"> <p align="center"><img src="<?php echo "$imagedisplay" ?>"></p> <p align="center"><font color="#FFFFFF"></br><b><u><font size="4">File Address:</font></u></b><br><br><b><font color="#3366FF">Url:</font></b><input type="text" name="myimg" value="http://wwww.smc.xeeblo.net<?php echo "$displayurl"; ?>" onClick="highlight(this);" size="25"><b> ||</b> <font color="#3366FF"><b>Img:</b></font><font color="#FFFFFF"><input type="text" name="myimg" value="[IMG]http://wwww.smc.xeeblo.net<?php echo "$displayurl"; ?>[/IMG]" onClick="highlight(this);" size="25"></font></td></tr></table></body></center></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/ Share on other sites More sharing options...
smc Posted April 27, 2006 Author Share Posted April 27, 2006 I noticed a user note on php.net about this but couldn't get it to work. Any help would be GREATLY appreciated as this is impeeding my abilitiy to finish this script once and for all! Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/#findComment-31447 Share on other sites More sharing options...
smc Posted April 28, 2006 Author Share Posted April 28, 2006 Bringing it up: Heck if you have a link to something that might help me please post it! Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/#findComment-31610 Share on other sites More sharing options...
kenrbnsn Posted April 28, 2006 Share Posted April 28, 2006 Did you look at the function [a href=\"http://www.php.net/manual/en/function.tempnam.php\" target=\"_blank\"]tempnam()[/a]? This function looks like it meets all of your criteria.Ken Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/#findComment-31674 Share on other sites More sharing options...
smc Posted April 29, 2006 Author Share Posted April 29, 2006 [!--quoteo(post=369586:date=Apr 28 2006, 09:56 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 28 2006, 09:56 AM) [snapback]369586[/snapback][/div][div class=\'quotemain\'][!--quotec--]Did you look at the function [a href=\"http://www.php.net/manual/en/function.tempnam.php\" target=\"_blank\"]tempnam()[/a]? This function looks like it meets all of your criteria.Ken[/quote]It didn't work when I tried it. Here is how I put it in:[code]//Defines variables for use later on$formurl = "/upload/";$linkurl = "../uploaded/$file_name";$displayurl = "/uploaded/$file_name";$invalidurl = "/upload/invalid.html";$nofileurl = "/upload/nofile.html";$errorurl = "/upload/error.html";$imagedisplay = "$linkurl";$filesizevar = "filesize($file_name)";$sizeerror = "/upload/sizeerror.html";$tempfile = tempnam("/uploaded", "");//Checks to make sure the file existsif($file_name !=""){if ($file_type == "application/x-msdownload" || $file_type == "audio/mpeg" || $file_type == "audio/mp3"){ header("Location:$invalidurl");}else{//Checking to see that the files is a JPG//Copies the file into the appropriate folder, if it can't it will redirect the browser to an error page//uploadfile($file_name,'../uploaded/',$_FILE['file']['tmp_name'])//chmod("/uploaded", 0777)copy ("$file", "../uploaded/$tempfile")//chmod("/uploaded", 0755) or header("Location:$errorurl");}}[/code]But, obviously, it didn't work. Any thoughts?Also - will this change the file extension too? Thats pretty important to have on there Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/#findComment-32002 Share on other sites More sharing options...
smc Posted April 29, 2006 Author Share Posted April 29, 2006 Here is the error:[code]Warning: tempnam(): open_basedir restriction in effect. File(/uploaded) is not within the allowed path(s): (/home/smc:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/smc/public_html/upload/uploadengine.php on line 20Warning: copy(../uploaded/): failed to open stream: Is a directory in /home/smc/public_html/upload/uploadengine.php on line 74Warning: Cannot modify header information - headers already sent by (output started at /home/smc/public_html/upload/uploadengine.php:20) in /home/smc/public_html/upload/uploadengine.php on line 76//The HTML part of it all[/code] Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/#findComment-32005 Share on other sites More sharing options...
Prismatic Posted April 29, 2006 Share Posted April 29, 2006 Just make the name = time()it will always be different :) Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/#findComment-32009 Share on other sites More sharing options...
smc Posted April 29, 2006 Author Share Posted April 29, 2006 [!--quoteo(post=369935:date=Apr 29 2006, 03:05 PM:name=Prismatic)--][div class=\'quotetop\']QUOTE(Prismatic @ Apr 29 2006, 03:05 PM) [snapback]369935[/snapback][/div][div class=\'quotemain\'][!--quotec--]Just make the name = time()it will always be different :)[/quote]Then how would I seperate then re-attach the file extension? Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/#findComment-32011 Share on other sites More sharing options...
Orio Posted April 29, 2006 Share Posted April 29, 2006 [code]// I am using $uploaded_file as the uploaded file :)function create_file_name($uploaded_file){$now=time();$info=pathinfo($uploaded_file);$ext=$info['extension'];$filename=$now; $filename.="."; $filename.=$ext;return $filename;}[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/#findComment-32022 Share on other sites More sharing options...
smc Posted April 29, 2006 Author Share Posted April 29, 2006 [!--quoteo(post=369948:date=Apr 29 2006, 03:38 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Apr 29 2006, 03:38 PM) [snapback]369948[/snapback][/div][div class=\'quotemain\'][!--quotec--][code]// I am using $uploaded_file as the uploaded file :)function create_file_name($uploaded_file){$now=time();$info=pathinfo($uploaded_file);$ext=$info['extension'];$filename=$now; $filename.="."; $filename.=$ext;return $filename;}[/code]Orio.[/quote]Alright I tried that but it wouldn't work. It just defaulted to "Cannot copy file" which is my code should the copying fail.Since the file that is being copied (ie. when they click browse that file) is $file I used[code]function create_file_name($file){$now=time();$info=pathinfo($file);$ext=$info['extension'];$filename=$now; $filename.="."; $filename.=$ext;return $filename;}[/code]Then I used:[code]if($file_name !=""){if ($file_type == "application/x-msdownload" || $file_type == "audio/mpeg" || $file_type == "audio/mp3"){ header("Location:$invalidurl");}else{create_file_name($file);copy ("$filename", "../uploaded/$file_name") or header("Location:$errorurl");}[/code]But it just went to $errorurl. Quote Link to comment https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/#findComment-32049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.