2013-07-26 75 views
0

我正在學習PHP,並試圖創建一個簡單的php上傳和下載腳本連接到MySQL數據庫。我用uid()爲每個文件生成一個特殊的ID並將其存儲在數據庫中。我差不多完成了,但問題是我無法弄清楚如何編寫download.php代碼? 請幫幫我。我正在分享我的index.php,upload.php和download.php當前文件。在PHP中下載代碼錯誤?上傳和下載表格

我的index.html有這種形式

<form enctype="multipart/form-data" action="uploader.php" method="POST"> 
<input type="hidden" name="MAX_FILE_SIZE" value="100000" /> 
Choose a file to upload: <input name="uploadedfile" type="file" /><br /> 
<input type="submit" value="Upload File" /> 
</form> 

我Uploader.php文件有此代碼

$target_path = "uploads/"; 

$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
echo "The file ". basename($_FILES['uploadedfile']['name']). 
" has been uploaded"; 
} 
$uid = uniqid(); 
$filename = basename($_FILES['uploadedfile']['name']); 
$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass); 
if(! $conn) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
$sql = 'INSERT INTO replitz_file '. 
    '(file_uid,file_name) '. 
    'VALUES ("$uid", "$filename")'; 

mysql_select_db('replitz'); 
$retval = mysql_query($sql, $conn); 
if(! $retval) 
{ 
die('Could not enter data: ' . mysql_error()); 
} 
echo "http://localhost/Project/download.php?$uid"; 
mysql_close($conn); 

而且我的download.php代碼

$dbhost = 'localhost'; 
$dbuser = 'root'; 
$dbpass = ''; 
$conn = mysql_connect($dbhost, $dbuser, $dbpass); 
if(! $conn) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
$db = mysql_select_db('replitz', $con); 
     $file = $_GET['_']; 
     $query = "SELECT * FROM replitz_files WHERE id='$file_uid'"; 
     $result = mysql_query($query); 
     $r = mysql_fetch_array($result); 

$retval = mysql_query($sql, $conn); 
if(! $retval) 
{ 
die('Link Not Valid: ' . mysql_error()); 
echo "Link Not Valid"; 
mysql_close($conn); 

現在請你我應該在Download.php中使用哪些代碼從存儲在數據庫中的uid()生成的代碼中下載文件。

+0

$ sql ='INSERT INTO replitz_file'。 '(file_uid,file_name)'。 'VALUES(「$ uid」,「$ filename」)'; 這是一個有效的MySQL查詢?我認爲它應該 $ sql =「INSERT INTO replitz_file」。 「(file_uid,file_name)」。 「VALUES('$ uid','$ filename')」; – dayitv89

回答