-1

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?

19
  • You need to move the file out of temp 1st: us2.php.net/move_uploaded_file Commented Nov 27, 2013 at 22:47
  • 1
    @user574632 OP is calling move_uploaded_file. Commented Nov 27, 2013 at 22:52
  • do you have any errors, warnings, notices? Is code inside if(isset($_FILES['upload'])) run? Commented Nov 27, 2013 at 22:53
  • @robson I have no errors, but the form disappears after submitting the form and the file does not appear on the server. Commented Nov 27, 2013 at 22:54
  • 1
    Also, always use exit immediately after issuing a Location header. See thedailywtf.com/Articles/WellIntentioned-Destruction.aspx Commented Nov 27, 2013 at 22:55

1 Answer 1

0

I believe the problem is because of an error in one of the following fields: the SQL query, not valid path used for storing the file or incorrect directory permissions where you want to store that file to.

For either of these errors you need to do a little bit debugging. First you need to enable error reporting in PHP, because it seems to be disabled in your case. Take a look at this question on how to enable it: How do I get PHP errors to display?

Basically you can try inserting:

error_reporting(E_ALL);<br>
ini_set('display_errors', 1);

at the beginning of your PHP code, right after the opening <?php tag but it might not work for some situations so you better enable it in the php.ini file and then restart the web server.

Your php.ini should have

display_errors = on

Then after you see what the real error is you'll be able to solve it really quickly.

Sign up to request clarification or add additional context in comments.

6 Comments

The display_erros is on in the php.ini.
You can ignore the time warning but as you can see there is a fatal error: Fatal error: Cannot use string offset as an array in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\home.php on line 27 so just see that line 27 in the home.php. It seems you have a string which you want to use as an array. For more information check [link]stackoverflow.com/questions/1873970/…) and that's how you are supposed to do your debugging until you get error free code and it works :)
P.S. to fix the first warning you need to set date.timezone = "UTC" in your php.ini to either Coordinated Universal Time(UTC) as I showed or to your real timezone as found here: php.net/manual/en/timezones.europe.php
I am downloading the latest build of PHP 5.5.6. Apparently it is a bug in PHP 5 (which currently I have) and hopefully downloading the latest (5.5.6) will fix it.
Yes because that's not a bug in PHP but error somewhere in the code. That is why I showed you how to debug it step by step. You just need to clear these errors you got one by one until it finally works.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.