I am having trouble removing spaces from a file name in php. The file does not upload to the server once the file is submitted. Here is the code:
<?php
session_start();
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('djmann1013');
if(!isset($_SESSION['username']) && !isset($_SESSION['auth'])){
header('Location: /');
}
$username = $_SESSION['username'];
echo $username;
$dirs = mysql_query("SELECT * FROM `users` WHERE `usr_name` = '" . $username . "'") or die(mysql_error());
$r = mysql_fetch_assoc($dirs);
$dir = $r['usr_directory'];
if(isset($_FILES['upload'])){
$file = preg_replace('/\s+/', '_', $_FILES['upload']['name']);
$current_time = date('n/j/Y ');
mysql_query("INSERT INTO `uploaded_files` (`username`,`file_name`,`time_updated`) VALUES('" . $username . "','" . $file . "','" . $current_time . "')") or die(mysql_error());
move_uploaded_file($_FILES['upload']['tmp_name'], "usr_files/$dir/$file");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Home - LAN File Upload</title>
</head>
<body>
<h1>Upload a file</h1>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="upload" id="file"><br />
<input type="submit" name="submit" value="Upload">
</form>
<b>Note: the larger the file, the longer it takes to upload.</b>
<h1>Account Settings</h1>
<b><a href="/file_browser.php">Your Files</a></b>
<b><a href="settings.php">Account Settings</a></b>
<b><a href="/?action=logout">Logout</a></b>
</body>
</html>
I don't know what is wrong with this code. Anyone have any ideas?
move_uploaded_file.exitimmediately after issuing a Location header. See thedailywtf.com/Articles/WellIntentioned-Destruction.aspx