Reputation: 19
I have a question about how the upload and download system works with php and mysql.
I would like to be able to upload files (preferably through my webpage) so that only a specific user can view available files sorted in some catagories and download the ones he/she wants.
I feel really stupid asking this but where is the files then saved? Inside a table? Or does the table point to a directory in my webserver?
I'm asking this as I really want to learn this but even the basic questions can be difficult even to formulate let alone solve... :)
Looking forward for some input
Cheers Adam
Upvotes: 1
Views: 1514
Reputation: 186
It seems you are totally new to PHP. I am giving you a sample code. use it and progress forward. It was for a student of mine. try it.
<?php
session_start();
if(!isset($_SESSION['Usernm']))
{
echo header("Location:../Common/MI_Login.php");
}
$usernm = $_SESSION['Usernm'];
include("../Connection/Connection.php");
if(isset($_POST['BTN_Submit_mi_songdesc']))
{
validate_song();
if(count($error) == 0)
{
$SongNm = $_POST['TXT_Nm'];
$ext=strchr($_FILES['FF_Song']['name'],".mp3");
$newfile=str_replace($_FILES['FF_Song']['name'],$SongNm,$_FILES['FF_Song']['name']);
$song = 'Songs/'.$newfile.$ext;
$album = $_POST['DDL_AlbumNm'];
$singernm = $_POST['DDL_SingerNm'];
$bitrate = $_POST['DDL_Bitrate'];
$catg = $_POST['DDL_Catg'];
$ins_song = "INSERT INTO mi_songdesc (SD_SongName, SD_File, SD_AlbumID, SD_SingerID, SD_Bitrate, SD_CatgID, SD_Approved, SD_UploadedBy) VALUES ('$SongNm','$song','$album','$singernm','$bitrate','$catg','1','$usernm')";
mysql_query($ins_song) or die(mysql_error());
move_uploaded_file($_FILES['FF_Song']['tmp_name'],'../'.$song);
header("Location:".$_SERVER['PHP_SELF']);
}
else
{
foreach($error as $i)
{
$test .= $i."<br />";
}
}
}
function validate_song()
{
global $error;
$error = array();
if($_POST['TXT_Nm'] == "")
{
$error[] = "Error : Enter Song Name";
}
if($_FILES['FF_Song']['name'] == "")
{
$error[] = "Error : Browse a Song";
}
if($_POST['DDL_AlbumNm'] == "")
{
$error[] = "Error : Select Album";
}
if($_POST['DDL_SingerNm'] == "")
{
$error[] = "Error : Select Singer";
}
if($_POST['DDL_Bitrate'] == "")
{
$error[] = "Error : Select Bitrate ";
}
if($_POST['DDL_Catg'] == "")
{
$error[] = "Error : Select Category";
}
}
?>
<!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>Upload Song</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data" name="form1" id="form1">
<table width="520" border="1" align="center">
<tr>
<td colspan="3" align="center" valign="middle">Enter Song Details</td>
</tr>
<tr>
<td width="185">Song Name</td>
<td width="16"> </td>
<td width="297"><label for="TXT_Nm"></label>
<input name="TXT_Nm" type="text" id="TXT_Nm" value="<?php echo $_POST['TXT_Nm']; ?>" size="30" maxlength="30"/></td>
</tr>
<tr>
<td>File</td>
<td> </td>
<td><label for="FF_Song"></label>
<input type="file" name="FF_Song" id="FF_Song"/></td>
</tr>
<tr>
<td>Album Name</td>
<td> </td>
<td><label for="DDL_AlbumNm"></label>
<select name="DDL_AlbumNm" id="DDL_AlbumNm">
<option value="">Select</option>
<?php
$res_alb = mysql_query("SELECT * FROM mi_album");
while($row_alb = mysql_fetch_array($res_alb))
{
if($_POST['DDL_AlbumNm']==$row_alb['Alb_SlNO'])
{
?>
<option selected="selected" value="<?php echo $row_alb['Alb_SlNO']; ?>"><?php echo $row_alb['Alb_Name']; ?></option>
<?php
}
else
{
?>
<option value="<?php echo $row_alb['Alb_SlNO']; ?>"><?php echo $row_alb['Alb_Name']; ?></option>
<?php
}
}
?>
</select></td>
</tr>
<tr>
<td>Singer Name</td>
<td> </td>
<td><label for="DDL_SingerNm"></label>
<select name="DDL_SingerNm" id="DDL_SingerNm">
<option value="">Select</option>
<?php
$res_singer = mysql_query("SELECT * FROM mi_singer");
while($row_singer = mysql_fetch_array($res_singer))
{
if($_POST['DDL_SingerNm']==$row_singer['SI_SlNo'])
{
?>
<option selected="selected" value="<?php echo $row_singer['SI_SlNo']; ?>"><?php echo $row_singer['SI_Name']; ?></option>
<?php
}
else
{
?>
<option value="<?php echo $row_singer['SI_SlNo']; ?>"><?php echo $row_singer['SI_Name']; ?></option>
<?php
}
}
?>
</select></td>
</tr>
<tr>
<td>Bitrate</td>
<td> </td>
<td><label for="DDL_Bitrate"></label>
<select name="DDL_Bitrate" id="DDL_Bitrate">
<option value="">Select</option>
<option value="128">128</option>
<option value="256">256</option>
</select>
kbps</td>
</tr>
<tr>
<td>Select Category</td>
<td> </td>
<td><label for="DDL_Catg"></label>
<select name="DDL_Catg" id="DDL_Catg">
<option value="">Select</option>
<?php
$res_catg = mysql_query("SELECT * FROM mi_catg");
while($row_catg = mysql_fetch_array($res_catg))
{
if($_POST['DDL_Catg']==$row_catg['C_SlNo'])
{
?>
<option selected="selected" value="<?php echo $row_catg['C_SlNo']; ?>"><?php echo $row_catg['C_Name']; ?></option>
<?php
}
else
{
?>
<option value="<?php echo $row_catg['C_SlNo']; ?>"><?php echo $row_catg['C_Name']; ?></option>
<?php
}
}
?>
</select></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td align="center" valign="top"><input type="submit" name="BTN_Submit_mi_songdesc" id="BTN_Submit_mi_songdesc" value="Submit" /></td>
</tr>
<tr>
<td colspan="3"><?php echo $test; ?> </td>
</tr>
</table>
</form>
</body>
</html>
Upvotes: 0
Reputation: 161
The PHP file upload is actually a two step process(if you want to save it to a desired path). The $_FILES variable has all the details when you try to upload a file, which includes, Name, type, size and temp_name. The temp_name actually contains the temporary path of the file. You can then make use of move_uploaded_file function to move the file to whatever path you want to.
About saving in the database, once you are able to successfully move the file to the desired location, you can update your database. I would suggest, it would be better to have a table just for files names and id and I am assuming you might have a table for users as well. You can then have a new table which can map user id and file ids. That would give you ability to share access among users. Of course there are others ways to do it as well. I think this might be a simple implementation.
Also I just realized, that you might want want to look at a simple file upload example. You can refer to this link
Upvotes: 1
Reputation: 12059
It all really depends on how complicated you make it.
The simplest way would be to have users upload a file. Use PHP to read the file info ($_FILE
) and move the file to some folder, and put the info into the database. Assuming you link the user and file, the user would be able to see all the files and then download their files from a list.
A more complex, secure, and overall better option would be to have user upload their files. Still use PHP to read the file info, store that in the database. Rename the file using the id, or some unique hash so it isn't ever overwritten, and move them into a protected directory, somewhere the user or any other users can access it. Set up a download page for users that finds the requested file based on the hash and then serve them the requested file if they are logged in correctly.
Again, it really depends on how complex you want to make it and the goals you are trying to accomplish. As you work on it and get stuck, come back and ask questions in regards to specific portions of your problem and you are bound to get a lot more help.
Upvotes: 1
Reputation: 454
You can upload files in PHP using the global variable $_FILES
.
Once you upload files.. they are temporarily stored in the temporary folder of your system..
For example.. if you are using XAMMP.. uploaded files are temprarily stored in xammp/tmp/
.
Check http://php.net/manual/en/reserved.variables.files.php
for the explanation of using $_FILES variable
Upvotes: 0