ChrisMartino Posted January 20, 2010 Share Posted January 20, 2010 Ok, So with my script, It involves uploading songs but i need it to replace the spaces with _'s can somebody show me how? heres my script: The variable what contains the filename is "$filename". <?php include "Main/Database/base.php"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta name="Description" content="Information architecture, Web Design, Web Standards." /> <meta name="Keywords" content="your, keywords" /> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name="Distribution" content="Global" /> <meta name="Author" content="Erwin Aligam - [email protected]" /> <meta name="Robots" content="index,follow" /> <link rel="stylesheet" href="images/MarketPlace.css" type="text/css" /> <LINK REL="SHORTCUT ICON" HREF="favicon.ico"> <title>Musicians Village - <?=$_SESSION['Username']?>'s Profile</title> </head> <body> <!-- wrap starts here --> <div id="wrap"> <!--header --> <div id="header"> <div id="header-links"> <p> <?php if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) { ?> <p>Logged in as: <a href="profile.php"><b><?=$_SESSION['Username']?></a></b> | Account: <?=$_SESSION['AccountType']?> | <a href="logout.php">Logout</a> </p> <?php } else { ?> <p>Welcome <b>Guest</b>, Please <a href="login.php">Login</a> or <a href="register.php">Register</a></p> <?php } ?> </p> </div> <!--header ends--> </div> <div id="header-photo"></div> <!-- navigation starts--> <div id="nav"> <ul> <li><a href="index.php">Home</a></li> <li><a href="login.php">Login</a></li> <li><a href="register.php">Register</a></li> <li><a href="track.php?action=upload">Submit Track</a></li> <li><a href="track.php?action=listen">Listen To Tracks</a></li> <li><a href="/forums">Forums</a></li> <li><a href="contact.php">Contact</a></li> </ul> <!-- navigation ends--> </div> <!-- content-wrap starts --> <div id="content-wrap" class="three-col" > <div id="sidebar"> <h1>Sponsors</h1> <ul class="sidemenu"> <li><a href="index.html">Home</a></li> <li><a href="#TemplateInfo">Template Info</a></li> <li><a href="#SampleTags">Sample Tags</a></li> <li><a href="http://www.styleshout.com/">More Free Templates</a></li> <li><a href="http://www.4templates.com/?aff=ealigam">Premium Templates</a></li> </ul> <!-- sidebar ends --> </div> <div id="rightcolumn"> <h1>Forum Stats</h1> <p>Top poster stuff here</p> <h1>*Newest track</h1> <p>it is here too! </p> </div> <div id="main"> <a name="TemplateInfo"></a> <?php if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username'])) { ?> <h1><?=$_SESSION['Username']?>'s Profile</h1> <p> <?php $Administrator = "Administrator"; $AccountQurey = $_SESSION['AccountType']; $User = $_SESSION['Username']; $Email = $_SESSION['EmailAddress']; $AccTyp = $_SESSION['AccountType']; $AccountIP = $_SESSION['AccountIP']; $action = $_GET['action']; $Song = $_GET['song']; $Upload = "upload"; $Password = "password"; $Start = ""; ?> <?php if ($action == $Start) { ?> <form action="track.php?action=upload" method="post" enctype="multipart/form-data"> <p> <label for="SongName">Track Name:</label><input type="text" name="SongName" id="SongName" /><br /> <label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br /> <button>Upload Track</button> <p> </form> <?php } elseif($action == $Upload) { $FileCheck = "/home/chrismar/public_html/Websites/Music/Songs/$User"; if (file_exists($FileCheck)) { // Configuration - Your Options $allowed_filetypes = array('.mp3','.wma','.bmp','.png'); // These will be the types of file that will pass the validation. $max_filesize = 52428888; // Maximum filesize in BYTES (currently 0.5MB). $upload_path = "/home/chrismar/public_html/Websites/Music/Songs/$User/"; // The place the files will be uploaded to (currently a 'files' directory). $HTTPath = "Songs/$User"; $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension). $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename. // Check if the filetype is allowed, if not DIE and inform the user. if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); // Now check the filesize, if it is too large then DIE and inform the user. if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); // Check if we can upload to the specified path, if not DIE and inform the user. if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); // Upload the file to your specified path. if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)) { $SongName = mysql_real_escape_string($_POST['SongName']); $SongAuthour = mysql_real_escape_string($_SESSION['Username']); $SongDIRR = "http://chrismartino.x10hosting.com/Websites/Music/Songs/$User/$filename"; $SongDIR = mysql_real_escape_string($SongDIRR); $TrackQuery = mysql_query("INSERT INTO TrackUploads (TrackAuthor, TrackUrl, TrackName) VALUES('".$SongAuthour."', '".$SongDIR."', '".$SongName."')"); $SongQurey = mysql_query("SELECT * FROM TrackUploads WHERE TrackUrl = '".$SongDIR."'"); $row = mysql_fetch_array($SongQurey); $SongID = $row['id']; echo "<h1>Success</h1>"; echo "<p>You have successfully uploaded your track!, You can listen to it <a href=\"track.php?song=$SongID\">Here</a>.</p>"; } else { echo 'There was an error during the file upload. Please try again.'; // It failed . } } else { mkdir($FileCheck, 0777); } } } ?> <?php if(isset($_GET['song'])) { if(is_numeric($_GET['song'])) { // get the member id $memberID = (int) $_GET['song']; $memberIDinfo = mysql_real_escape_string($memberID); $checklogin = mysql_query("SELECT * FROM TrackUploads WHERE id = '".$memberIDinfo."'"); $row = mysql_fetch_array($checklogin); $TrackUrl = $row['TrackUrl']; $TrackName = $row['TrackName']; $TrackAuthor = $row['TrackAuthor']; // Select only the row that matches memberID being requested, eg 123 $query = "SELECT * FROM TrackUploads WHERE id = '$memberID'"; $result = mysql_query($query); // check that the query returned one row, meaning a match was found if(mysql_num_rows($result) == 1) { ?> <h1>Track : <?=$TrackName?></h1> <center> <embed type="application/x-shockwave-flash" src="http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=<?=$TrackUrl?>" width="400" height="27" allowscriptaccess="never" quality="best" bgcolor="#ffffff" wmode="window" flashvars="playerMode=embedded" /> </center> <br /> <?php echo "<b>Artist:</b> $TrackAuthor"; ?> <br /> <?php echo "<b>Track Name:</b> $TrackName"; ?> <br /> <?php } // a match was not found. Display an error instead else { echo '<p style="color:red">Song does not exists</p>'; } } // Either the member id was not declared or it was invalid. Display an error else { echo 'Invalid song id specified'; } } else { ?> <h1>Error</h1> <p>You need to be logged in to see your profile!</p> <?php } ?> <br /> </p> </div> <!-- content-wrap ends--> </div> <!-- footer starts --> <div id="footer-wrap"><div id="footer"> <p> © 2006 <strong>Your Company</strong> | Design by: <a href="http://www.styleshout.com/">styleshout</a> | Valid <a href="http://validator.w3.org/check?uri=referer">XHTML</a> | <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a> <a href="index.html">Home</a> | <a href="index.html">Sitemap</a> | <a href="index.html">RSS Feed</a> </p> </div></div> <!-- footer ends--> <!-- wrap ends here --> </div> </body> </html> Link to comment https://forums.phpfreaks.com/topic/189217-how-can-i-replace-spaces-with-_-when-uploading/ Share on other sites More sharing options...
JAY6390 Posted January 20, 2010 Share Posted January 20, 2010 I've not looked through your script, but it's just a matter of saving the file with the filename but using the str_replace to change all the spaces to _ $filename = str_replace(' ', '_', $filename); Link to comment https://forums.phpfreaks.com/topic/189217-how-can-i-replace-spaces-with-_-when-uploading/#findComment-998946 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.