atticus Posted January 21, 2008 Share Posted January 21, 2008 The file is uploading to the server, however in order to keep files from being overwritten I want to generate a random file name. It is uploading to the file, however it is not generating the random name. <?php include("config.php"); if(isset($_POST['submit'])) { $uploadDir = 'upload/'; $id = mysql_escape_string($_POST['id']); $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; // get the file extension first $ext = substr(strrchr($fileName, "."), 1); // generate the random file name $randName = md5(rand() * time()); $filePath = $uploadDir . $fileName; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } Quote Link to comment https://forums.phpfreaks.com/topic/87114-solved-generate-random-file-name-on-upload/ Share on other sites More sharing options...
ratcateme Posted January 21, 2008 Share Posted January 21, 2008 you have just written your file path line wrong it needs to be like this $filePath = $uploadDir . $randName . '.' . $ext; Scott Quote Link to comment https://forums.phpfreaks.com/topic/87114-solved-generate-random-file-name-on-upload/#findComment-445527 Share on other sites More sharing options...
atticus Posted January 21, 2008 Author Share Posted January 21, 2008 you have just written your file path line wrong it needs to be like this $filePath = $uploadDir . $randName . '.' . $ext; Scott Scott, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/87114-solved-generate-random-file-name-on-upload/#findComment-445534 Share on other sites More sharing options...
ohdang888 Posted January 21, 2008 Share Posted January 21, 2008 i'm trying to do this myself as wel.... what does config.php do? Quote Link to comment https://forums.phpfreaks.com/topic/87114-solved-generate-random-file-name-on-upload/#findComment-445536 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.