Mr Chris Posted March 1, 2007 Share Posted March 1, 2007 Hi Guys, I have an upload script, which works great. It uploads media to a folder and saves the details in a MYSQL database: <?php if($_POST["x"] == "Upload"){ $target_path = "../../../story_media/video/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { header("Location: http://www.thevillager.co.uk/cms_admin/editorial/edit_list.php"); // Start the connection to the database include('***************'); $can_i_connect = db_connect(); // by db_connect function is in my include file if(!$can_i_connect) { $msg = "Could not connect to the database"; } // End the connection to the database // insert into database $date_uploaded = date("m d, Y"); $directory = "video/"; $file_name = basename( $_FILES['uploadedfile']['name']); $video_caption = stripslashes(trim($_POST['video_caption'])); $video_credit = stripslashes(trim($_POST['video_credit'])); $story_id = $_GET['story_id']; $sql_query = mysql_query("INSERT INTO cms_videos (date_uploaded,directory,file_name,video_caption,video_credit,story_id) VALUES ('$date_uploaded','$directory','$file_name','$video_caption','$video_credit','$story_id')"); if (!$sql_query) { exit("<p>Error performing query: " . mysql_error() . "</p>"); } } else{ echo "There was an error uploading the file, please try again!"; } exit; } ?> However, what I’d also like it to do is generate a randomly generated name So by doing some research I put this together: <?php if($_POST["x"] == "Upload"){ $Fname=$_FILES['uploadedfile']['name']; //Function to strip the extension of a filename string function strip_ext($name) { $ext = strrchr($name, '.'); if($ext !== false) { $name = substr($name, 0, -strlen($ext)); } return $name; } // This will get the extension of the filename $FnameExt= end(explode('.',$Fname)); function createRandomName() { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime()*10000000); $i = 0; $pass = '' ; while ($i <= 7) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $pass = $pass . $tmp; $i++; } return $pass; } // Usage $randomName = createRandomName(); //retrieve file name // Where the file is going to be placed $target_path = "../../../story_media/video/"; /* Add the original filename to our target path. Result is "uploads/filename.extension" */ $target_path = "../../../story_media/video/"; $_FILES['uploadedfile']['tmp_name']; if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path.'/'.$randomName.".".$FnameExt)) { header("Location: edit_list.php"); // Start the connection to the database include('******************'); $can_i_connect = db_connect(); // by db_connect function is in my include file if(!$can_i_connect) { $msg = "Could not connect to the database"; } // End the connection to the database // insert into database $date_uploaded = date("m d, Y"); $directory = "video/"; $file_name = basename( $_FILES['uploadedfile']['name']); $video_caption = stripslashes(trim($_POST['video_caption'])); $video_credit = stripslashes(trim($_POST['video_credit'])); $story_id = $_GET['story_id']; $sql_query = mysql_query("INSERT INTO cms_videos (date_uploaded,directory,file_name,video_caption,video_credit,story_id) VALUES ('$date_uploaded','$directory','$file_name','$video_caption','$video_credit','$story_id')"); if (!$sql_query) { exit("<p>Error performing query: " . mysql_error() . "</p>"); } } else{ echo "There was an error uploading the file, please try again!"; } exit; } ?> …However, the page just hangs when you try to load it (ie continues to try and load forever!). Can anyone advise? Thanks Chris Link to comment https://forums.phpfreaks.com/topic/40669-generate-random-filename/ Share on other sites More sharing options...
Orio Posted March 1, 2007 Share Posted March 1, 2007 If you want to generate a unique filename, you can use tempnam() to help you: <?php function unique_name() { $name = tempnam("/", ""); unlink($name); return $name; } ?> Orio. Link to comment https://forums.phpfreaks.com/topic/40669-generate-random-filename/#findComment-196815 Share on other sites More sharing options...
Mr Chris Posted March 1, 2007 Author Share Posted March 1, 2007 Thanks, But i've tried putting this piece of code into my first upload code as I thought the function would simply overwrite the existing name, which works and thought it would replace the name, but it throws up a move_uploaded_file() error? <?php if($_POST["x"] == "Upload"){ $target_path = "../../../story_media/video/"; function unique_name() { $name = tempnam("/", ""); unlink($name); return $name; } $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { header("Location: http://www.thevillager.co.uk/cms_admin/editorial/edit_list.php"); // Start the connection to the database include('***************'); $can_i_connect = db_connect(); // by db_connect function is in my include file if(!$can_i_connect) { $msg = "Could not connect to the database"; } // End the connection to the database // insert into database $date_uploaded = date("m d, Y"); $directory = "video/"; $file_name = basename( $_FILES['uploadedfile']['name']); $video_caption = stripslashes(trim($_POST['video_caption'])); $video_credit = stripslashes(trim($_POST['video_credit'])); $story_id = $_GET['story_id']; $sql_query = mysql_query("INSERT INTO cms_videos (date_uploaded,directory,file_name,video_caption,video_credit,story_id) VALUES ('$date_uploaded','$directory','$file_name','$video_caption','$video_credit','$story_id')"); if (!$sql_query) { exit("<p>Error performing query: " . mysql_error() . "</p>"); } } else{ echo "There was an error uploading the file, please try again!"; } exit; } ?> Link to comment https://forums.phpfreaks.com/topic/40669-generate-random-filename/#findComment-196845 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.