Jump to content


Photo

Help randomizing a file name


  • Please log in to reply
9 replies to this topic

#1 smc

smc
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 27 April 2006 - 05:04 PM

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

<?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 exists
if($file_name !="")
{
//Checking to see that the files is a JPG
if ($file_type == "image/jpeg")
{
//Copies the file into the appropriate folder, if it can't it will redirect the browser to an error page
copy ("$file", "../uploaded/$file_name")
    or header("Location:$errorurl");
}
//If the image isn't a jpg, a pre-made image will take it's place
else {
    $imagedisplay = "/upload/file.jpg";
}
}
//If the file wasen't there, then this will redirect the browser to an error page
else {
    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>

mysql_query( "UPDATE suntrust SET level = 'rich_sucka' WHERE bank_account = 'mine' AND dollars = '0'" ) or DIE;

#2 smc

smc
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 27 April 2006 - 08:47 PM

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!
mysql_query( "UPDATE suntrust SET level = 'rich_sucka' WHERE bank_account = 'mine' AND dollars = '0'" ) or DIE;

#3 smc

smc
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 28 April 2006 - 10:16 AM

Bringing it up: Heck if you have a link to something that might help me please post it!
mysql_query( "UPDATE suntrust SET level = 'rich_sucka' WHERE bank_account = 'mine' AND dollars = '0'" ) or DIE;

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 28 April 2006 - 01:56 PM

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

#5 smc

smc
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 29 April 2006 - 06:22 PM

[!--quoteo(post=369586:date=Apr 28 2006, 09:56 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 28 2006, 09:56 AM) View Post[/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:

//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 exists
if($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");
}
}

But, obviously, it didn't work. Any thoughts?

Also - will this change the file extension too? Thats pretty important to have on there
mysql_query( "UPDATE suntrust SET level = 'rich_sucka' WHERE bank_account = 'mine' AND dollars = '0'" ) or DIE;

#6 smc

smc
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 29 April 2006 - 06:34 PM

Here is the error:


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 20

Warning: copy(../uploaded/): failed to open stream: Is a directory in /home/smc/public_html/upload/uploadengine.php on line 74

Warning: 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

mysql_query( "UPDATE suntrust SET level = 'rich_sucka' WHERE bank_account = 'mine' AND dollars = '0'" ) or DIE;

#7 Prismatic

Prismatic
  • Members
  • PipPipPip
  • Advanced Member
  • 503 posts
  • LocationSan Diego

Posted 29 April 2006 - 07:05 PM

Just make the name = time()

it will always be different :)

#8 smc

smc
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 29 April 2006 - 07:08 PM

[!--quoteo(post=369935:date=Apr 29 2006, 03:05 PM:name=Prismatic)--][div class=\'quotetop\']QUOTE(Prismatic @ Apr 29 2006, 03:05 PM) View Post[/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?
mysql_query( "UPDATE suntrust SET level = 'rich_sucka' WHERE bank_account = 'mine' AND dollars = '0'" ) or DIE;

#9 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 29 April 2006 - 07:38 PM

// 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;}
Orio.
Think you're smarty?

(Gone until 20 to November)

#10 smc

smc
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts

Posted 29 April 2006 - 10:44 PM

[!--quoteo(post=369948:date=Apr 29 2006, 03:38 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Apr 29 2006, 03:38 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
// 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;}
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

function create_file_name($file){
$now=time();
$info=pathinfo($file);
$ext=$info['extension'];
$filename=$now; $filename.="."; $filename.=$ext;
return $filename;}

Then I used:

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");
}

But it just went to $errorurl.
mysql_query( "UPDATE suntrust SET level = 'rich_sucka' WHERE bank_account = 'mine' AND dollars = '0'" ) or DIE;




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users