Jump to content

PHP Programming help [Upload Script]


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/";
  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">
<form action="?upload=now" method="post" enctype="multipart/form-data">
<table  border="0" cellpadding="3" cellspacing="1">
<td>Select file
<input name="ufile" type="file" id="ufile" /></td>
<td align="left"><input type="submit" name="Submit" value="Upload" /></td>
<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. Are you sure you have something important to add to it?

Join the conversation

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

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.