php file upload help

Soldato
Joined
1 Feb 2006
Posts
8,188
hey, i need to write a script to upload files to a given directory along with some labels (i.e. name, category etc) and then provide links to the files on a downloads page on my site. Anyone know of how I can go about this? I googled for php uploads but they all are just simple uploads and they do not deal with the linking to the uploaded materials. File types will include .doc files, images and possible xls files. Would appreciate some help with this!
 
yes i beleive i do.

hmm i have only just learnt this a week or so ago so my explantion may be kind of not all good.

ok i found it too hard to right an explantion so i have just put part of the code i use to for uploading of files and added more comments to it for you to understand

Code:
// The file name being uploading
$file_name = $HTTP_POST_FILES['ufile']['name'];

// 4 random digits to add to the file name, not needed though will mean that 2 files will never have the same name
$random_digit=rand(0000,9999);

// Defines file size limit, again not needed. can change to whatever you choose
$limit_size=50000;


//combine random digit to the file name to create new file name
//use dot (.) to combile these two variables
$new_file_name=$random_digit.$file_name;

// Store upload file size in $file_size
$file_size=$HTTP_POST_FILES['ufile']['size'];

//set where is wanted to store files
//$new_file_name = new upload file name
$path= "upload/".$new_file_name;

if($ufile !=none)



if($file_size >= $limit_size){
echo "Your file size is over limit<BR>";
echo "Your file size = ".$file_size;
echo " K";
echo "<BR>File size limit = 50000 k";
}
else {

{
if(copy($HTTP_POST_FILES['ufile']['tmp_name'], $path))
{
echo "File Upload Success<br />";

//$new_file_name = new file name
//$HTTP_POST_FILES['ufile']['size'] = file size
//$HTTP_POST_FILES['ufile']['type'] = type of file
echo "<b>File Name :</b>".$new_file_name."<br />";
echo "<b>File Size :</b>".$HTTP_POST_FILES['ufile']['size']."<br />";
echo "<b>File Type :</b>".$HTTP_POST_FILES['ufile']['type']."<br />";
}
else
{
echo "";
}
}

<form action='$self?action=addnews' method='post' enctype=\"multipart/form-data\">
<table>
	<tr>
		<td>
			Category:<input type='text' name='ufile'></input>
	   </td>
	</tr>	
	
<tr>
		<td>
			Upload Image: <input type='file' name='ufile' id='ufile' size='50'></input>
		</td>
	</tr>
	
	<tr>
		<td>
			<input type='submit' value='submit news topic'>
		</td>
	</tr>
	
</table>
</form>";

i then use a simple bit of php that selects everything from a table where the category is equal to whatever you want for that page.

e.g. the first line querys mysql db and selects everything from submit where the category is equal to arts and valid is equal to 1 and then orders the results by id number and limits the amount shown to show results.
Code:
$query = mysql_query("SELECT * FROM submit WHERE category='arts' AND valid='1' ORDER BY `id` DESC LIMIT 20");

the folliwing then displays it in the way i want
Code:
echo"";

while($row = mysql_fetch_array($query)) {
echo"
				   
			<table>
			
				<tr>
					<td>
						<img src='submit/upload/$row[imagename]' />
					</td>
				
					<td>
					   Posted  by <b>$row[author]</b>
					</td>
				</tr>
											
			</table>
";
 
Last edited:
thanks for the help guys. This is for a uni project so it wont be going live so permissions maybe aren't necessary at the moment. As long as I can upload files into a folder il be happy enough!
Cheers
 
jonnyc747 said:
thanks for the help guys. This is for a uni project so it wont be going live so permissions maybe aren't necessary at the moment. As long as I can upload files into a folder il be happy enough!
Cheers

yeah but remember though, you will not be able to upload files into the folder without changing its permission to 777 like someone has said already. even if its not going live may be worth sauying this for more makrs? dont know how unis work.

can i ask where and what course you are doing and what year? i should be getting into uni next year and i know all this stuff and a lot mroe so lil bit boost of confidence that im gonig to get in and do well if this is what you're learning. hope that made sense
 
addy_010 said:
yeah but remember though, you will not be able to upload files into the folder without changing its permission to 777 like someone has said already. even if its not going live may be worth sauying this for more makrs? dont know how unis work.

can i ask where and what course you are doing and what year? i should be getting into uni next year and i know all this stuff and a lot mroe so lil bit boost of confidence that im gonig to get in and do well if this is what you're learning. hope that made sense

im doing a computing science degree at a uni in Northern ireland. We were not taught PHP once on the whole course. First time we were taught javascript was 4th year! Most of the stuff I picked up I taught myself. Decided on php for my own project so that i could get a wee bit of experience in another language.
 
Back
Top Bottom