Jump to content

PHP Upload to MySQL


rwachowiak

Recommended Posts

So i installed Apache 2.2, PHP 5.something and MySQL on a extra server we had in the office. I made a database called intranet, a table called files, and the following fields:

 

id----int(11)----primary key

Filename----varchar(30)

Keyword1----varchar(30)

Keyword2----varchar(30)

Description----text

DateAdded----timestampCURRENT_TIMESTAMP

Type----varchar(30)

Size----int(11)

Content----mediumblob

 

Here is my script:

 

i thought the code tag would work but apparently not

 

here is a link to the code:

 

http://www.chiampou.com/code.php

 

 

 

When i try to upload a file, i get the "Error, insert query failed" Now, it was working before i put in the file part, with just filename, keyword 1 and 2 and the description. Since i threw the file part it i cant get it working... help please!

Link to comment
Share on other sites

If you weren't allowed to post code, it means it had a function that is banned from being posted (like fopen). Post the code as an attachment using the Additional Options area.

 

thanks let me try...

 

 

ok thanks, its notes.txt is the code, take a look at it and let me know what you think please!

 

Link to comment
Share on other sites

my guess is that you have some illegal characters in your SQL. Everything that is a string should be sent through mysql_real_escape_string() before getting executed. Here is the top part of the code re-written:

 

<?php
if(isset($_POST['add']))
{
include 'libs/dbopen.php';

$Keyword1 = mysql_real_escape_string($_POST['keyword1']);
$Keyword2 = mysql_real_escape_string($_POST['keyword2']);
$Description = mysql_real_escape_string($_POST['description']);

$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['Size'];
$fileType = $_FILES['userfile']['Type'];

$fp      = f_open($tmpName, 'r');
$content = f_read($fp, filesize($tmpName));
$content = mysql_real_escape_string($content);
f_close($fp);
$fileName = mysql_real_escape_string($fileName);
$fileType= mysql_real_escape_string($fileType);

//You should disable magic_quotes. It's been removed from PHP6
#if(!get_magic_quotes_gpc())
#{
#    $fileName = addslashes($fileName);
#}

$query = "INSERT INTO files (Filename, Keyword1, Keyword2, Description, Size, Type, Content) 
VALUES ('$fileName', '$Keyword1', '$Keyword2', '$Description', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, insert query failed: '.mysql_error());
.....

 

NOTE: To be able to post it, i had to put underscores in the file handle functions. Make sure you remove them.

Link to comment
Share on other sites

 

NOTE: To be able to post it, i had to put underscores in the file handle functions. Make sure you remove them.

 

ok so i changed what you gave me, and now i get this:

 

Error, insert query failed: Incorrect integer value: '' for column 'Size' at row 1

 

any ideas?!  (im a scripting newb, all this is a tutorial but i had to try and customize a lot of it and failed horribly!)

Link to comment
Share on other sites

Yup...you are using a start PHP tag when PHP is already started. This is the culprit:

echo "File Name: <span class='titlebar2'><a href='indextest.php?id=<?php=$id;?>'>{$row['Filename']}</a></span> <br>" .

it should be:

echo "File Name: <span class='titlebar2'><a href='indextest.php?id={$id}'>{$row['Filename']}</a></span> <br>" .

Link to comment
Share on other sites

Yup...you are using a start PHP tag when PHP is already started. This is the culprit:

echo "File Name: <span class='titlebar2'><a href='indextest.php?id=<?php=$id;?>'>{$row['Filename']}</a></span> <br>" .

it should be:

echo "File Name: <span class='titlebar2'><a href='indextest.php?id={$id}'>{$row['Filename']}</a></span> <br>" .

 

ok i made that change, now the link reads:

 

http://server/indextest.php?id=

 

Im guessing one of the ID pulling tags is wrong? its an edited/customized tutorial so some of the copy/pasting could be off

 

update: ok so i tried changing the {$id} to 18, (one of the IDs in my sql table) and it still doesnt work, it wants me to download indextest.php instead of the file in the database

Link to comment
Share on other sites

Let's not get ahead of ourselves. Let's get that URL working first.

 

And I found my error, change that line to this and see if it works:

echo "File Name: <span class='titlebar2'><a href='indextest.php?id={$row['id']}'>{$row['Filename']}</a></span> <br>" .

Link to comment
Share on other sites

Let's not get ahead of ourselves. Let's get that URL working first.

 

And I found my error, change that line to this and see if it works:

echo "File Name: <span class='titlebar2'><a href='indextest.php?id={$row['id']}'>{$row['Filename']}</a></span> <br>" .

 

wow this is so exciting, you are so smart

 

ok it is changing the URL to the correct ID, but its still downloading indextest.php!

 

UPDATE: OK WAIT

 

Its downloading the file as: Indextest.php  but it IS the actual file. If i upload a .xls, and it downloads as the .php i change the extension and open it up with excel it is the document i uploaded! hmm, filename is weird somewheres...

 

OMG FOUND IT! I think its workkkking! let me take an extra look!

Link to comment
Share on other sites

The only problem should be this line (I updated it):

header("Content-Disposition: attachment; filename=$name");

 

no thats wrong, the field in my table is Filename, not name, there was somewhere else where it was still name instead of Filename, changed it and its working great! Now my next project is, to make links on that page run a qeuery, like if i click on Audit, it brings up all the files that have Keyword Audit... now THAT should take me a while hahaha

 

thanks for all your help you should see me back here frequently!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.