catherinePHP Posted April 23, 2013 Share Posted April 23, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277200-php-programming-help-upload-script/ Share on other sites More sharing options...
lemmin Posted April 23, 2013 Share Posted April 23, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/277200-php-programming-help-upload-script/#findComment-1426104 Share on other sites More sharing options...
Q695 Posted April 24, 2013 Share Posted April 24, 2013 (edited) This guys video was helpful for me when I couldn't get helpful replies on here: http://www.youtube.com/watch?v=T0Uv3VeTMR8 Edited April 24, 2013 by Q695 Quote Link to comment https://forums.phpfreaks.com/topic/277200-php-programming-help-upload-script/#findComment-1426234 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.