ShoeLace1291 Posted May 20, 2007 Share Posted May 20, 2007 I have a script where the form uploads a user's file onto the server and inserts it into the database. How would I make it so that if my database row count code results as more than 0, that a few random letters/numbers are added onto the end of the filename but before the extension? Quote Link to comment https://forums.phpfreaks.com/topic/52178-php-upload-script/ Share on other sites More sharing options...
chigley Posted May 20, 2007 Share Posted May 20, 2007 <?php $q = mysql_query("SELECT id FROM table WHERE filename = '$filename'") or die(mysql_error()); // Select a single value from the table $rows = mysql_num_rows($q) or die(mysql_error()); // Then count the rows if($rows > 0) { // If filename already exists $filename .= "abc"; // Add abc to the end of it (you may need to explode() the filename to add it before the extension } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52178-php-upload-script/#findComment-257395 Share on other sites More sharing options...
ShoeLace1291 Posted May 20, 2007 Author Share Posted May 20, 2007 I'm sorry, but I'm confused. If this is my script, what would I have to do? <?php require_once('config.php'); if(empty($_POST['submitBtn'])){ echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'DTD/xhtml1-transitional.dtd'> <html> <body> <form action='upload.php' method='post' name='fileForm' enctype='multipart/form-data'> File to upload: <table> <tr><td><input name='upfile' type='file'></td></tr> <tr><td><input type='submit' name='submitBtn' value='Upload'></td></tr> </table> </form> </body>"; } if (isset($_POST['submitBtn'])){ // Path to upload the file $target_path = "uploads/"; // Create the file name with path $target_path = $target_path . basename( $_FILES['upfile']['name']); $filename = basename( $_FILES['upfile']['name']); $findimage = mysql_query("SELECT * FROM recent_files WHERE filename = $filename") or die("Error ".mysql_error()); $imagesfound = mysql_num_rows($findimage) or die(mysql_error()); //If an image with the filename was found in the database... if($imagesfound != 0){ //Generate 5 random characters to place before the file extension function generate_filename($length) { $string = md5(time()); $highest_startpoint = 5-$length; $filename = $filename .substr($string,rand(0,$highest_startpoint),$length); return $filename; } // Try to move the file from the temporay directory to the defined. if(move_uploaded_file($_FILES['upfile']['tmp_name'], $target_path)){ //Generate session id for that user function generate_session($length) { $string = md5(time()); $highest_startpoint = 32-$length; $session = substr($string,rand(0,$highest_startpoint),$length); return $session; } $session = generate_session(10); //Insert the image into the database $query = mysql_query("INSERT INTO recent_files (filename,session) values('$filename', '$session')") or die("Error: ".mysql_error()); $_SESSION['mysession'] = $randomString; header("Location:image.php?img=$filename"); } else{ echo "There was an error uploading the file, please try again!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/52178-php-upload-script/#findComment-257702 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.