Jump to content

PHP Programming help [Upload Script]


catherinePHP

Recommended Posts

Hello phpfreaks :)

 

I found a basic php upload script which i find really useful but I have a problem, I'm new to php programming and need help in this code:

<?
$upload = $_GET["upload"];
if ($upload == "now") {
  $path1 = "uploads/";
  $path="$path1".$_FILES['ufile']['name'];
  if($ufile !=none) {
    if(copy($_FILES['ufile']['tmp_name'], $path)) {
    echo "Success!<BR/>";
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"2; URL=uploads/\">";
    } else {
    echo "<font color=red><b>Error!</b></font>";
    }
  }
} else {

?>
<table width="250" border="0" align="left" cellpadding="0" cellspacing="1">
<tr>
<form action="?upload=now" method="post" enctype="multipart/form-data">
<td>
<table  border="0" cellpadding="3" cellspacing="1">
<tr>
<td>Select file
<input name="ufile" type="file" id="ufile" /></td>
</tr>
<tr>
<td align="left"><input type="submit" name="Submit" value="Upload" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<a href="uploads">View Uploads</a>
<?   }   ?>

I want the script to change the name of a file to random numbers after being uploaded for example:

If a person uploaded a file called: "catherine.jpg"

The script will automatically rename it to: "1231421421421.jpg" while providing a link to the uploaded file. How can I do that? any help would be appreciated :)

 

Thanks in advance, and sorry for my bad english.

Link to comment
Share on other sites

The copy() function is what is naming and saving it. You are telling it to name it the original name, so just change that to whatever you want. You could use md5 hash to generate a pseudo-random name. Just extract the file extension first:

preg_match('/\.[^\.]+$/', $_FILES['ufile']['name'], $match);
$ext = $match[0];
$path = "uploads/" . md5($_FILES['ufile']['name']) . $ext;

You might want to look over line 6. You probably meant to put "none" in quotes. Although, $ufile doesn't seem to be set so it probably isn't really doing anything.

Link to comment
Share on other sites

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.