Jump to content

Recommended Posts

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 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>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/8562-help-randomizing-a-file-name/
Share on other sites

[!--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 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");
}
}
[/code]

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

Also - will this change the file extension too? Thats pretty important to have on there
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 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
[/code]
[!--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?
[!--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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.