2013-11-27 111 views
-1

我無法從php文件名中刪除空格。提交文件後,文件不會上傳到服務器。這裏是代碼:PHP文件上傳不起作用

<?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> 

我不知道這段代碼有什麼問題。有人有主意嗎?

+0

您需要將文件移出temp 1st:http://us2.php.net/move_uploaded_file – Steve

+1

@ user574632 OP正在調用'move_uploaded_file'。 –

+0

你有任何錯誤,警告,通知嗎? if(isset($ _ FILES ['upload']))是否在代碼中運行? – robson

回答

0

我認爲這個問題是由於以下字段之一的錯誤:SQL查詢,用於存儲文件的無效路徑或者要存儲該文件的目錄權限不正確。

對於這些錯誤中的任何一個,您都需要進行一點點調試。首先你需要在PHP中啓用錯誤報告,因爲它似乎在你的情況下被禁用。看看如何啓用這個問題: How do I get PHP errors to display?

基本上你可以嘗試插入:

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

在你的PHP代碼開始,開幕< PHP標記後的權利,但它?可能無法在某些情況下使用,所以您最好在php.ini文件中啓用它,然後重新啓動Web服務器。

你的php.ini應該有

display_errors = on 

然後之後你看到真正的錯誤是什麼,你就可以真的很快解決。

+0

display_erros在php.ini中打開。 – Djmann1013

+0

http://pastebin.com/Gue5ZXs8 - 我有錯誤。 Idk如果這將有所幫助。 – Djmann1013

+0

您可以忽略時間警告,但您可以看到有一個致命錯誤:**致命錯誤:無法使用字符串偏移量作爲C:\ Program Files \ Apache Software Foundation \ Apache2.2 \ htdocs \ home.php中的數組在第27行**,所以只要看到home.php中的第27行。看起來你有一個你想用作數組的字符串。欲瞭解更多信息,請查看[鏈接] http://stackoverflow.com/questions/1873970/cannot-use-string-offset-as-an-array-in-php)這就是你應該如何進行調試,直到你得到無錯代碼,它的工作原理:) –