Twist3d Posted June 10, 2010 Share Posted June 10, 2010 Hello. Well, I'm having quick a bit of trouble with this. What I'm trying to do is, this is a file-uploading script. This uploads all of the details of the file to the sql database, and the file to a folder. What I want to do, is when it moves the file over to the folder, I want a string to generate a random name for that file, but keep everything the same, like the contents and the format of that file example .PHP Why do I want to do this? Because if you have 2 files with the same name, you upload the first, then upload the second, the second completely replaces the first. So someone could loose something they needed. So, any help? Here is my code so fare: <?php $db = mysql_pconnect($dbhost,$dbuser,$dbpass); mysql_select_db("$dbname",$db); $uploadDir = 'uploads/'; if(isset($_POST['upload'])) { $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $filePath = $uploadDir . $fileName; $result = move_uploaded_file($tmpName, $filePath); if (!$result) { echo "Error uploading file"; exit; } if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); $filePath = addslashes($filePath); } $query = "INSERT INTO upload2 (name, size, type, path ) ". "VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')"; mysql_query($query) or die('Error, query failed : ' . mysql_error()); echo "<br>Files uploaded<br>"; } Thanks? Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/ Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2010 Share Posted June 10, 2010 http://www.php.net/manual/en/function.uniqid.php Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/#findComment-1070227 Share on other sites More sharing options...
Twist3d Posted June 10, 2010 Author Share Posted June 10, 2010 http://www.php.net/manual/en/function.uniqid.php This solves the problem which I don't have of Unique ID's in the database, but I was more looking for a random function/string to be applied on the name of the file in the folder Which I can't figure out how to do. Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/#findComment-1070229 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2010 Share Posted June 10, 2010 Did you bother to read the information at the link that was posted? Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/#findComment-1070230 Share on other sites More sharing options...
Twist3d Posted June 10, 2010 Author Share Posted June 10, 2010 Did you bother to read the information at the link that was posted? Yes. But I could easily make a random name generator. Its applying it to the name of the file that is uploaded. Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/#findComment-1070231 Share on other sites More sharing options...
trq Posted June 10, 2010 Share Posted June 10, 2010 Did you bother to read the information at the link that was posted? Yes. But I could easily make a random name generator. Its applying it to the name of the file that is uploaded. Then, on this line..... $filePath = $uploadDir . $fileName; Replace $fileName with your randomly generated file name. Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/#findComment-1070232 Share on other sites More sharing options...
Twist3d Posted June 10, 2010 Author Share Posted June 10, 2010 It worked, yay! But now, it got worse. It replaced the format of the file that was uploaded to x-generic of the file. In the SQL database it still says (I uploaded a PHP file) it still says its a PHP file, but the path is just the random string, and the file in the folder is the same. Help? Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/#findComment-1070235 Share on other sites More sharing options...
trq Posted June 10, 2010 Share Posted June 10, 2010 Its not rocket science. The variables you put in the database need to remain intact. eg; Don't override $fileName with your random string. Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/#findComment-1070265 Share on other sites More sharing options...
Twist3d Posted June 10, 2010 Author Share Posted June 10, 2010 Its not rocket science. The variables you put in the database need to remain intact. eg; Don't override $fileName with your random string. The database still recognizes the PHP file as a PHP file tho.. 4 post.php application/x-php 26 uploads/7kffyqjv Thats what it looks like. Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/#findComment-1070284 Share on other sites More sharing options...
trq Posted June 10, 2010 Share Posted June 10, 2010 What exactly is your actual problem now? Quote Link to comment https://forums.phpfreaks.com/topic/204361-php-random-name-for-uploaded-file/#findComment-1070305 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.