-markie- Posted January 4, 2011 Share Posted January 4, 2011 How would i do a random file name for my upload script $type = $_FILES['uploadedfile']['type']; // Where the file is going to be placed $target_path ="/***/***/public_html/****/lofslidernews/images/"; /* Add the original filename to our target path. Result is "/images/uploads/filename.extension" */ $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); $uploadedfile2 = $_FILES['uploadedfile']['name']; if (empty($quicktitle) || empty($maintitle ) || empty($description ) || empty($uploadedfile)) { $_SESSION["promoerror"] = "Please Select an image and enter a Quick Title, Main Title and Description!" ; header("Location: promo.php"); //This sets the redirection information //echo "Going to login.php, username or password is empty"; exit(); //Ends the script and redirects to above } $query = "INSERT INTO promotion (title, maintitle, description, image) VALUES ('$quicktitle','$maintitle','$description', '$uploadedfile2')"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if ( ( $type != "image/jpeg") && ($type != "image/gif") ) { die ("That format is not allowed"); } else { move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path); header("Location: promo.php"); $_SESSION["promoerror"] = "Added Successfully!"; } Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/ Share on other sites More sharing options...
MadTechie Posted January 4, 2011 Share Posted January 4, 2011 You could use uniqid or md5_file Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154713 Share on other sites More sharing options...
-markie- Posted January 4, 2011 Author Share Posted January 4, 2011 so if i add the following after this line: $uploadedfile2 = $_FILES['uploadedfile']['name']; md5_file($uploadedfile2) Is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154714 Share on other sites More sharing options...
MadTechie Posted January 4, 2011 Share Posted January 4, 2011 Something like this $uploadedfile2 = $_FILES['uploadedfile']['name']; $path_part = pathinfo($uploadedfile2); $uploadedfile2 = uniqid(md5_file($uploadedfile2)).".".$path_part['extension']; should work, it really depends on what's being uploaded, MD5 will give the same result if the same file is used, the above will add some randomness to that, normally i would add the username/id or something else that's unique. Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154717 Share on other sites More sharing options...
-markie- Posted January 4, 2011 Author Share Posted January 4, 2011 Thanks for your help so far. I added that and it gave the following error: [04-Jan-2011 17:25:45] PHP Warning: md5_file(test.jpg) [<a href='function.md5-file'>function.md5-file</a>]: failed to open stream: No such file or directory in /home/*****/public_html/*****/addpromoitem.php on line 35 Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154718 Share on other sites More sharing options...
MadTechie Posted January 4, 2011 Share Posted January 4, 2011 oops, $uploadedfile2 = uniqid(md5_file($uploadedfile2)).".".$path_part['extension']; should be $uploadedfile2 = uniqid(md5_file($_FILES['uploadedfile']['tmp_name'])).".".$path_part['extension']; Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154731 Share on other sites More sharing options...
-markie- Posted January 4, 2011 Author Share Posted January 4, 2011 Thanks. Its correctly insert a random file name into the database. However its not renaming the actual image file.. its staying the default name. Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154749 Share on other sites More sharing options...
MadTechie Posted January 4, 2011 Share Posted January 4, 2011 move $target_path under uploadedfile2 and change to $target_path = $target_path . $uploadedfile2; Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154785 Share on other sites More sharing options...
-markie- Posted January 4, 2011 Author Share Posted January 4, 2011 Hmm still not changing the actual file name // create query $query = "SELECT * FROM promotion"; $quicktitle = $_POST["quicktitle"]; //Check its ok - if not then add an error message to the error string if (empty($quicktitle)) $errorString = $errorString."<br>Please enter a Quick Title."; $maintitle = $_POST["maintitle"]; //Check its ok - if not then add an error message to the error string if (empty($maintitle)) $errorString = $errorString."<br>Please enter a Main Title."; $description = $_POST["description"]; //Check its ok - if not then add an error message to the error string if (empty($description)) $errorString = $errorString."<br>Please enter a Description."; $type = $_FILES['uploadedfile']['type']; // Where the file is going to be placed $target_path ="/home/g*****/public_html/****/lofslidernews/images/"; /* Add the original filename to our target path. Result is "/images/uploads/filename.extension" */ $uploadedfile2 = $_FILES['uploadedfile']['name']; $target_path = $target_path . $uploadedfile2; $path_part = pathinfo($uploadedfile2); $uploadedfile2 = uniqid(md5_file($_FILES['uploadedfile']['tmp_name'])).".".$path_part['extension']; if (empty($quicktitle) || empty($maintitle ) || empty($description ) || empty($uploadedfile2)) { $_SESSION["promoerror"] = "Please Select an image and enter a Quick Title, Main Title and Description!" ; header("Location: promo.php"); //This sets the redirection information //echo "Going to login.php, username or password is empty"; exit(); //Ends the script and redirects to above } $query = "INSERT INTO promotion (title, maintitle, description, image) VALUES ('$quicktitle','$maintitle','$description', '$uploadedfile2')"; $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); if ( ( $type != "image/jpeg") && ($type != "image/gif") ) { die ("That format is not allowed"); } else { move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path); header("Location: promo.php"); $_SESSION["promoerror"] = "Added Successfully!"; } //header("Location: loggedon.php?username=".mysql_insert_id() ); // (6) close connection mysql_close($connection); ?> Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154797 Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 echo $target_path to make sure it's what you expect. make sure that path exists and is writable. Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154807 Share on other sites More sharing options...
-markie- Posted January 4, 2011 Author Share Posted January 4, 2011 echo $target_path to make sure it's what you expect. make sure that path exists and is writable. The path exists as its placing the file in the correct location it just got changing the filename to the one stored in the database Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154814 Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 oh, i see. $uploadedfile2 = $_FILES['uploadedfile']['name']; //$target_path = $target_path . $uploadedfile2; $path_part = pathinfo($uploadedfile2); $uploadedfile2 = uniqid(md5_file($_FILES['uploadedfile']['tmp_name'])).".".$path_part['extension']; $target_path = $target_path . $uploadedfile2; Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154819 Share on other sites More sharing options...
-markie- Posted January 4, 2011 Author Share Posted January 4, 2011 Perfect. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1154828 Share on other sites More sharing options...
MadTechie Posted January 5, 2011 Share Posted January 5, 2011 oh, i see. $uploadedfile2 = $_FILES['uploadedfile']['name']; //$target_path = $target_path . $uploadedfile2; $path_part = pathinfo($uploadedfile2); $uploadedfile2 = uniqid(md5_file($_FILES['uploadedfile']['tmp_name'])).".".$path_part['extension']; $target_path = $target_path . $uploadedfile2; didn't i say that ? move $target_path under uploadedfile2 and change to $target_path = $target_path . $uploadedfile2; oh i did! Quote Link to comment https://forums.phpfreaks.com/topic/223382-file-upload-random-file-name/#findComment-1155050 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.