[SOLVED] Upload and View Form/Script...thingie

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

v3xtra
Forum Newbie
Posts: 14
Joined: Wed Jan 05, 2005 4:56 pm

Upload and View Form/Script...thingie

Post by v3xtra »

Ok, howdy. I'm v3xtra, nice to meet you all! I'm been on ultiple sites, this one, phpbuilder, devshack or whatever that site is, I'm not sure at the moment! I'm looking for a simple upload script that would input the information from the form to a MySQL database, and be able to grab that infor from the database and show it on a site, with a link to download it.

I know it's possible because everywhere I go, I see somehting along the lines of:

http:www.myurl.com/downloads.php?id=878797

And that's how you download the file. I've searched many forums and this forum included, found alot of posts that talk about this, but none with the exact stuff I need, plus I'm a newb with this (just learned how to create a blog using PHP and Mysql and such...:) ) I even used someone's code (or TRIED to use) from the Code Snippits or however it's called, to do this, but it didn't work...well..period, and I'm no troubleshooter, especially in PHP! :)

So if you even get me started I have someone who is gone for a couple of days who would be able to help me, any info is good info, go technical if you like! :)

Thanks again for reading my post, I know this may sound easy but it's not to me! ;)

(BTW, one last thing, I'll be uploading .sma files, for AMX and AMXX etc, sound files, .mdl files, .spr fils, zips, etc, but no text or images, but that shouldn't make a difference as I can figure out how to make it allow those, although by reading some posts I noticed all you have to do is just leave out the mime code for it so it allows all, or at least that's what I figured out from it! :) )

Thanks once again, I had better shut up as I'm rambling!

-v3x
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uploading and downloading fairly easy. I'll try to explain them.

Uploaded files come into php through the $_FILES array. The original filename, errorcode (if any), mime-type sent by the browser, temporary filename, size in bytes are all stored under the field name they were sent in under. The other form information sent along with the upload is stored in the $_POST array. Each element of the $_POST array will have the name of the form field used in the submitting page. Storing the information sent into MySQL is a fairly simple [mysql_man]INSERT[/mysql_man]command. Read that link to learn how the insert command works. Instead of storing the uploaded file(s) in the database (highly not recommended), save the file into an archive folder or similar where you can keep all the files uploaded. You can make the filename match the original extension of the file, and the id of the newly entered row of information (to ease data keeping).

For the retrieval and download, your example link shows a $_GET array element of 'id' as the identifier to find the file to show, or download in this case. Downloading the file is fairly simple: just call [php_man]header()[/php_man] with the content-type information (provided by the upload's mime-type), along with a few other things like the filename to save it as. Things like that. [devnet]+force +download[/devnet] will show you several threads about what header calls are needed to initiate the download. Those examples should use a function called [php_man]readfile()[/php_man], this is the function that actually sends the file. Note that you cannot echo any other text out in the download script, as it'll pollute the file's stream and corrupt the file save to the user's machine. To find the information you inserted into the database, use a [mysql_man]SELECT[/mysql_man] query. Read that link to learn lots about how select's work. Searching here will show lots of practical and odd combinations as well. :)

Hope that was informative.
v3xtra
Forum Newbie
Posts: 14
Joined: Wed Jan 05, 2005 4:56 pm

Post by v3xtra »

Ok, I have a working upload script, and the information will be getting put in the database tonight, I'm working on it now. I also know I'm going to use auto_inc in order to get the number for the download.

My only question is going to be how to put the $_GET function in the link.

After readin those search results, I think I might have figured out how to get it to work....would it be something like this:

<A HREF="./plugins/download.php?<?php print $id; ?>">Download</a>

?? And then just have id as the select statement to get the ID of the file, if I'm understanding this correctly.

Thanks feyd for helping me, once I get this done I'm buying a frickin' book and learning myself instead of taking time from others to teach me! :D

-v3x
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code there is the basic idea, however, to help get the id a little easier, sorta, add a name for the value to get stuck in, like id:

Code: Select all

<A HREF="./plugins/download.php?id=<?php print $id; ?>">Download</a>
You can find the value now through $_GET['id']
v3xtra
Forum Newbie
Posts: 14
Joined: Wed Jan 05, 2005 4:56 pm

Post by v3xtra »

Gracias man, the upload script is writing to the database and uploading the file perfectly, and now with this I am able to finish the code!

I'll post the finished product soon! hanks again feyd! Big BIG help!

-v3x
v3xtra
Forum Newbie
Posts: 14
Joined: Wed Jan 05, 2005 4:56 pm

Post by v3xtra »

Since editting might not bump this post and I'm doing good on this script, I'm nto editting my last post. Sorry! :)

Ok, heer's where I am. Upload script works fine. I have more information in the form that writes to the database so I can have a very pretty output! But the problem is, I can't download it because there is nothing in the database that I can use to get the filename that it's been uploaded too.

I know I'm going to use the header() to force the download, thanks to your links! :) But I could do somehting like the force download /plugins/$filename so that it got the filename from the database, and then would put it in the header in order to download it.

How would I get the filename that the user used after it's uploaded, or when it's uploaded? My code looks like this, and please excuse me if it's messy, or wrong in some places, all I know is that it works! :)

Code: Select all

<?php 
ob_start();
$uploadDir = '/Appserv/www/plugins/'; 
$uploadFile = $uploadDir . $_FILES['userfile']['name']; 
print "<pre>"; 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile)) 
{ 
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n"; print_r($_FILES); 
} else { 
print "Possible file upload attack! Here's some debugging info:\n"; 
print_r($_FILES); 
} 
print "</pre>"; 
 
$dbserver = 'localhost'; 
$dbusername = 'root';
$dbpassword = '';
$dbname = 'plugins';

mysql_connect($dbserver, $dbusername, $dbpassword) or die('Couldn''t connect to MySQL server'); 
mysql_select_db($dbname) or die('Couldn''t select database'); 
$author = $_REQUEST['author'];
$title = $_REQUEST['title'];
$filename = $_FILES['username']['file'];

$query = mysql_query("INSERT INTO amx (author, title, filename) VALUES ('$author', '$title', '$filename')") or die(mysql_error());

ob_end_flush(); 
?>
That is the upload script, that processes the file, if youd din't already guess/know. BTW let me add, everything else writes to the database perfectly fine, so it's not that, I just don't know how to get the filename! :)This is the form I'm using, simple and works:

Code: Select all

&lt;html&gt;
&lt;body&gt;
&lt;form enctype="multipart/form-data" action="upload.php" method="post"&gt;
    &lt;input type="hidden" name="MAX_FILE_SIZE" value="900000" /&gt;
    Author:&amp;nbsp;&amp;nbsp;&lt;input type="text" size="15" name="author" /&gt;
    Title:&amp;nbsp;&amp;nbsp;&lt;input type="text" size="15" name="title" /&gt;
    Choose a file to upload: &lt;input name="userfile" type="file" /&gt;&lt;br /&gt;
    &lt;input type="submit" value="Upload File" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Thanks for any help!

-v3x
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

line 25 of your script needs fixing:

Code: Select all

$filename = $_FILES&#1111;'userfile']&#1111;'name'];
v3xtra
Forum Newbie
Posts: 14
Joined: Wed Jan 05, 2005 4:56 pm

Post by v3xtra »

Well currently I used this:

Code: Select all

<?php
$query = mysql_query("INSERT INTO amx (author, title, filename) VALUES ('$author', '$title', '$userfile_name')") or die(mysql_error());
?>
$username_file works when I'm doing, now it's just a matter of suing the links you gave me with the headers and using the $_GET['filename'] and the other links in order for it to work correctly! Thanks again feyd, big help!

IT seems to work the same, I'm going to try your code as well and see if there's any difference!

-v3x
v3xtra
Forum Newbie
Posts: 14
Joined: Wed Jan 05, 2005 4:56 pm

Post by v3xtra »

Both work, so thanks again. simple mistake that I didn't pick up. Thanks again. :)

-v3x
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sounds like you have register_globals on...
v3xtra
Forum Newbie
Posts: 14
Joined: Wed Jan 05, 2005 4:56 pm

Post by v3xtra »

I do. But hey, it works....so.

But now I've come to another problem and won't be able to look at it for a couple of days (finals to study for) so I would like to see what's wrong with this part. It's just the simple output for the stuff, and I've done this in the past, but it doesn't seem to work correctly. It only shows ONE line of the database, even if multiple ones are there. That's my trouble, and I've done stuff like blogs that show more than 1 but it's just not working. It might have smething to do with LIMIT in the query, which I have for the blog and not this, so I might add that later, but if it's not asking alot could you help me with this code for the last time? :)

Code: Select all

<?php
ob_start(); 
$dbserver = 'localhost'; 
$dbusername = 'root';
$dbpassword = '';
$dbname = 'plugins';

mysql_connect($dbserver, $dbusername, $dbpassword) or die('Couldn''t connect to MySQL server'); 
mysql_select_db($dbname) or die('Couldn''t select database'); 

$query = mysql_query("SELECT * FROM amx ORDER BY title DESC") or die(mysql_error());
if($array = mysql_fetch_array($query)){
$author = $array['author'];
$title = $array['title'];
$id = $array['id'];
$filename = $array['filename']; 

echo'<table border="2" cellpadding="2" cellspacing="2" width="500"><tr><td>Plugin - '.$title.'</td></tr>
<tr><td>Author:&nbsp;&nbsp;'.$author.'</td><td align="right"><a href="./plugins/'.$filename.'">Download</a></td></tr>
</table>';
}else{
echo 'No Plugins Found!';
}
ob_end_flush(); 
?>
And yes, it's not a pretty output, bear with me. :) Thanks again, hope to get this fixed and finally done! ;)

-v3x
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

if($array = mysql_fetch_array($query)){
$author = $array&#1111;'author'];
$title = $array&#1111;'title'];
$id = $array&#1111;'id'];
$filename = $array&#1111;'filename']; 

echo'&lt;table border="2" cellpadding="2" cellspacing="2" width="500"&gt;&lt;tr&gt;&lt;td&gt;Plugin - '.$title.'&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Author:&amp;nbsp;&amp;nbsp;'.$author.'&lt;/td&gt;&lt;td align="right"&gt;&lt;a href="./plugins/'.$filename.'"&gt;Download&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;';
}else{
echo 'No Plugins Found!';
}

Code: Select all

$count = 0;
while($array = mysql_fetch_array($query))
{
  $count++;
$author = $array&#1111;'author'];
$title = $array&#1111;'title'];
$id = $array&#1111;'id'];
$filename = $array&#1111;'filename']; 

echo'&lt;table border="2" cellpadding="2" cellspacing="2" width="500"&gt;&lt;tr&gt;&lt;td&gt;Plugin - '.$title.'&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Author:&amp;nbsp;&amp;nbsp;'.$author.'&lt;/td&gt;&lt;td align="right"&gt;&lt;a href="./plugins/'.$filename.'"&gt;Download&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;';
}
if($count == 0)
echo 'No Plugins Found!';
v3xtra
Forum Newbie
Posts: 14
Joined: Wed Jan 05, 2005 4:56 pm

Post by v3xtra »

Well thanks again feyd, but I installed in on a remote webhosting server, the one I had was local on my computer. I think this may have something to do with their php.ini but I'm not sure, this is the error I get:

Code: Select all

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, pluginname) VALUES ('nightmr', 'bunnyhop', 'kfjklj', 'bun
and this is my script for the form, upload.php:

Code: Select all

<?php
$uploadDir = '/home/explici/public_html/v3x/plugins/'; 
$uploadFile = $uploadDir . $_FILES['userfile']['name']; 
print "<pre>"; 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile)) 
{ 
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n"; print_r($_FILES); 
} else { 
print "Possible file upload attack! Here's some debugging info:\n"; 
print_r($_FILES); 
} 
print "</pre>"; 
 
$dbserver = 'localhost'; 
$dbusername = '****';
$dbpassword = '*****';
$dbname = 'explici_v3x';

mysql_connect($dbserver, $dbusername, $dbpassword) or die('Couldn''t connect to MySQL server'); 
mysql_select_db($dbname) or die('Couldn''t select database'); 
$author = $_REQUEST['author'];
$title = $_REQUEST['title'];
$desc = $_REQUEST['desc'];
$pluginname = $_FILES['userfile']['name'];

// Saves To Table
$query = mysql_query("INSERT INTO plugins (author, title, desc, pluginname) VALUES ('$author', '$title', '$desc', '$pluginname')") or die(mysql_error());
?>
I think it has something to do with the $_FILE part of it, and that "register globals" thing you talked about. This would complete the script, and hopefully if there is no solution to this problem I still have my host locally. :)

Thanks again feyd.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

desc is a keyword. You need to put backticks ` .. (character on the tilde ~ key) around desc.
v3xtra
Forum Newbie
Posts: 14
Joined: Wed Jan 05, 2005 4:56 pm

Post by v3xtra »

For both....something liek this?Not sure exactly which part you want 'em to go on, sorry for the inconvinience!

Code: Select all

<?php
$uploadDir = '/home/explici/public_html/v3x/plugins/'; 
$uploadFile = $uploadDir . $_FILES['userfile']['name']; 
print "<pre>"; 
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadFile)) 
{ 
print "File is valid, and was successfully uploaded. ";
print "Here's some more debugging info:\n"; print_r($_FILES); 
} else { 
print "Possible file upload attack! Here's some debugging info:\n"; 
print_r($_FILES); 
} 
print "</pre>"; 
 
$dbserver = 'localhost'; 
$dbusername = 'explici_v3x';
$dbpassword = '';
$dbname = 'explici_v3x';

mysql_connect($dbserver, $dbusername, $dbpassword) or die('Couldn''t connect to MySQL server'); 
mysql_select_db($dbname) or die('Couldn''t select database'); 
$author = $_REQUEST['author'];
$title = $_REQUEST['title'];
$desc = $_REQUEST[`desc`];
$pluginname = $_FILES['userfile']['name'];

// Saves To Table
$query = mysql_query("INSERT INTO plugins (author, title, `desc`, pluginname) VALUES ('$author', '$title', `$desc`, '$pluginname')") or die(mysql_error());
?>
-v3x
Post Reply