Jump to content

[SOLVED] file uploader


chris_rulez001

Recommended Posts

hi ive made a script to make my users upload files and its not working, i am getting no error but just nothing what so ever

 

the script:

 

<?php
session_start();

$id = $_POST['id'];
$username = $_SESSION['username'];
if ($_FILES["file"]["type"] == "image/gif")
|| $_FILES["file"]["type"] == "image/pjpeg")
|| $_FILES["file"]["type"] == "image/png")
|| $_FILES["file"]["type"] == "image/bmp")
|| $_FILES["file"]["type"] == "movie/swf")
|| $_FILES["file"]["type"] == "image/jpeg")
|| $_FILES["file"]["type"] == "music/mp3")
|| $_FILES["file"]["type"] == "music/wma")
&& ($_FILES["file"]["size"] < 5000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("memberuploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{

$filename = $_FILES["file"]["name"];

move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "memberuploads/" . $_FILES["file"]["name"];

$dbh=mysql_connect("fdb1.awardspace.com", "cgitesting221_ss", "buzzer2242")
or die ('Connection to database failed: ' . mysql_error());
mysql_select_db("cgitesting221_ss");

$today = date("D M d Y");
$filetype = $_FILES["file"]["type"];
$file size = $_FILES["file"]["size"];

mysql_query("INSERT INTO tracking (id, username, date, ipaddress, filename, filetype, filesize) VALUES ('$id', '$username', '$today, '$_SERVER['REMOTE_ADDR']', '$filename', '$filetype', $filesize')") or die(mysql_error());

}
}
}
else
{
echo "Invalid file";
}
?>

 

can someone help me?

Link to comment
Share on other sites

hi ive made a script to make my users upload files and its not working, i am getting no error but just nothing what so ever

 

the script:

 

<?php
session_start();

$id = $_POST['id'];
$username = $_SESSION['username'];
if ($_FILES["file"]["type"] == "image/gif")
|| $_FILES["file"]["type"] == "image/pjpeg")
|| $_FILES["file"]["type"] == "image/png")
|| $_FILES["file"]["type"] == "image/bmp")
|| $_FILES["file"]["type"] == "movie/swf")
|| $_FILES["file"]["type"] == "image/jpeg")
|| $_FILES["file"]["type"] == "music/mp3")
|| $_FILES["file"]["type"] == "music/wma")
&& ($_FILES["file"]["size"] < 5000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("memberuploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{

$filename = $_FILES["file"]["name"];

move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "memberuploads/" . $_FILES["file"]["name"];

$dbh=mysql_connect("fdb1.awardspace.com", "", "")
or die ('Connection to database failed: ' . mysql_error());
mysql_select_db("cgitesting221_ss");

$today = date("D M d Y");
$filetype = $_FILES["file"]["type"];
$file size = $_FILES["file"]["size"];

mysql_query("INSERT INTO tracking (id, username, date, ipaddress, filename, filetype, filesize) VALUES ('$id', '$username', '$today, '$_SERVER['REMOTE_ADDR']', '$filename', '$filetype', $filesize')") or die(mysql_error());

}
}
}
else
{
echo "Invalid file";
}
?>

 

can someone help me?

Link to comment
Share on other sites

Well, I am not sure whats wrong, but I did see this:

 

$id = $_POST['id'];

$username = $_SESSION['username'];

if ($_FILES["file"]["type"] == "image/gif")

|| $_FILES["file"]["type"] == "image/pjpeg")

|| $_FILES["file"]["type"] == "image/png")

|| $_FILES["file"]["type"] == "image/bmp")

|| $_FILES["file"]["type"] == "movie/swf")

|| $_FILES["file"]["type"] == "image/jpeg")

|| $_FILES["file"]["type"] == "music/mp3")

|| $_FILES["file"]["type"] == "music/wma")

&& ($_FILES["file"]["size"] < 5000))

 

 

you could try to change that to:

 

$id = $_POST['id'];

$username = $_SESSION['username'];

if (($_FILES["file"]["type"] == "image/gif")

|| ($_FILES["file"]["type"] == "image/pjpeg")

|| ($_FILES["file"]["type"] == "image/png")

|| ($_FILES["file"]["type"] == "image/bmp")

|| ($_FILES["file"]["type"] == "movie/swf")

|| ($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "music/mp3")

|| ($_FILES["file"]["type"] == "music/wma")

&& ($_FILES["file"]["size"] < 5000))

Link to comment
Share on other sites

thanks, im getting an error,

 

Parse error: syntax error, unexpected T_BOOLEAN_OR in /home/www/hostsareus.awardspace.co.uk/file_upload2.php on line 4

 

code:

 

<?php
$id = $_POST['id'];
$username = $_SESSION['username'];
if (($_FILES["file"]["type"] == "image/gif")
|| $_FILES["file"]["type"] == "image/pjpeg")
|| $_FILES["file"]["type"] == "image/png")
|| $_FILES["file"]["type"] == "image/bmp")
|| $_FILES["file"]["type"] == "movie/swf")
|| $_FILES["file"]["type"] == "image/jpeg")
|| $_FILES["file"]["type"] == "music/mp3")
|| $_FILES["file"]["type"] == "music/wma")
&& ($_FILES["file"]["size"] < 5000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("memberuploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{

move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "memberuploads/" . $_FILES["file"]["name"];

$filename = $_FILES["file"]["name"];

$dbh=mysql_connect("fdb1.awardspace.com", "myusername", "mypassword")
or die ('Connection to database failed: ' . mysql_error());
mysql_select_db("mydatabase");

$today = date("D M d Y");
$filetype = $_FILES["file"]["type"];
$file size = $_FILES["file"]["size"];

mysql_query("INSERT INTO tracking (id, username, date, ipaddress, filename, filetype, filesize) VALUES ('$id', '$username', '$today, '$REMOTE_ADDR', '$filename', '$filetype', $filesize')") or die(mysql_error());

}
}
}
else
{
echo "Invalid file";
}
?>

Link to comment
Share on other sites

I can't seem to find any thing wrong.. The only thing I can possibly think if is removing the ")"s like below

 

<?php
if( $_FILES["file"]["type"] == "image/gif" 
|| $_FILES["file"]["type"] == "image/pjpeg" 
|| $_FILES["file"]["type"] == "image/png" 
|| $_FILES["file"]["type"] == "image/bmp" 
|| $_FILES["file"]["type"] == "movie/swf" 
|| $_FILES["file"]["type"] == "image/jpeg" 
|| $_FILES["file"]["type"] == "music/mp3" 
|| $_FILES["file"]["type"] == "music/wma" 
&& $_FILES["file"]["size"] < 5000 )
?>

 

I guess try this, but I really have no idea other wise.. Kinda odd

Link to comment
Share on other sites

ok the boolean error has gone but another error has occured:

 

Parse error: syntax error, unexpected T_STRING in /home/www/hostsareus.awardspace.co.uk/file_upload2.php on line 44

 

code:

<?php
$id = $_POST['id'];
$username = $_SESSION['username'];
if( $_FILES["file"]["type"] == "image/gif" 
|| $_FILES["file"]["type"] == "image/pjpeg" 
|| $_FILES["file"]["type"] == "image/png" 
|| $_FILES["file"]["type"] == "image/bmp" 
|| $_FILES["file"]["type"] == "movie/swf" 
|| $_FILES["file"]["type"] == "image/jpeg" 
|| $_FILES["file"]["type"] == "music/mp3" 
|| $_FILES["file"]["type"] == "music/wma" 
&& $_FILES["file"]["size"] < 5000 )
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("memberuploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{

move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "memberuploads/" . $_FILES["file"]["name"];

$filename = $_FILES["file"]["name"];

mysql_connect("fdb1.awardspace.com", "myusername", "mypassword")
or die ('Connection to database failed: ' . mysql_error());
mysql_select_db("mydatabase");

$today = date("D M d Y");
$filetype = $_FILES["file"]["type"];
$file size = $_FILES["file"]["size"];

mysql_query("INSERT INTO tracking (id, username, date, ipaddress, filename, filetype, filesize) VALUES ('$id', '$username', '$today, '$REMOTE_ADDR', '$filename', '$filetype', $filesize')") or die(mysql_error());

}
}
}
else
{
echo "Invalid file";
}
?>

Link to comment
Share on other sites

OK found it. You have

 

<?php
$file size = $_FILES["file"]["size"];
?>

 

you left a space between file and size just change it to $filesize. I've also found a potential error on line 46 near the end of your query:

 

'$filetype', $filesize')") or die(mysql_error()); -- you're missing a ' right before $filesize'

Link to comment
Share on other sites

thanks, after i click sumbit on my form it shows info i want but its showing other errors:

 

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 " at line 1

 

code:

 

<?php
$id = $_POST['id'];
$username = $_SESSION['username'];
if( $_FILES["file"]["type"] == "image/gif" 
|| $_FILES["file"]["type"] == "image/pjpeg" 
|| $_FILES["file"]["type"] == "image/png" 
|| $_FILES["file"]["type"] == "image/bmp" 
|| $_FILES["file"]["type"] == "movie/swf" 
|| $_FILES["file"]["type"] == "image/jpeg" 
|| $_FILES["file"]["type"] == "music/mp3" 
|| $_FILES["file"]["type"] == "music/wma" 
&& $_FILES["file"]["size"] < 5000 )
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("memberuploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{

move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "memberuploads/" . $_FILES["file"]["name"];

$filename = $_FILES["file"]["name"];

mysql_connect("fdb1.awardspace.com", "*****", "*****")
or die ('Connection to database failed: ' . mysql_error());
mysql_select_db("*******");

$today = date("D M d Y");
$filetype = $_FILES["file"]["type"];
$filesize = $_FILES["file"]["size"];

mysql_query("INSERT INTO tracking (id, username, date, ipaddress, filename, filetype, filesize) VALUES ('$id', '$username', '$today, '$REMOTE_ADDR', '$filename', '$filetype', '$filesize')") or die(mysql_error());

}
}
}
else
{
echo "Invalid file";
}
?>

Link to comment
Share on other sites

Ahh there we are. 99% of the time is something simple, usually in open but hard to find. I should of caught this while looking at your query the first time:

 

You have

VALUES ('$id', '$username', '$today, '$REMOTE_ADDR'

 

'$today is missing a '

 

should be

VALUES ('$id', '$username', '$today', '$REMOTE_ADDR'

 

Let's see if this works.

Link to comment
Share on other sites

Stubborn query! You mentioned that you saw the information you wanted. I'm assuming that would be the file, file size etc? Could you post that. I'm wondering if there is may be a ' or \ in there something to interrupt the query. I really looks fine.

 

Do you know what version of MySQL and php you're running?

Link to comment
Share on other sites

yeah you're right it is stubbon, i dont know what version of php and mysql, ive just tried something and its worked,

 

ive changed:

 

mysql_query("INSERT INTO tracking(id, username, date, ipaddress, filename, filetype, filesize) VALUES ('$id', '$username', '$today', '$ip', '$filename', '$filetype', '$filesize'") or die(mysql_error());

 

to

 

mysql_query("INSERT INTO tracking(id, username, date, ipaddress, filename, filetype, filesize) VALUES ('$id', '$username', '$today', '$ip', '$filename', '$filetype', '$filesize')");

Link to comment
Share on other sites

i have another problem, no errors are showing, its not importing the username into the database heres my code:

 

<?php
$id = $_POST['id'];
$username = $_POST['username'];
if( $_FILES["file"]["type"] == "image/gif" 
|| $_FILES["file"]["type"] == "image/pjpeg" 
|| $_FILES["file"]["type"] == "image/png" 
|| $_FILES["file"]["type"] == "image/bmp" 
|| $_FILES["file"]["type"] == "movie/swf" 
|| $_FILES["file"]["type"] == "image/jpeg" 
|| $_FILES["file"]["type"] == "music/mp3" 
|| $_FILES["file"]["type"] == "music/wma" 
&& $_FILES["file"]["size"] < 5000 )
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "<h4>Upload Successful!</h4><br/>";
echo "<strong>Upload:</strong> " . $_FILES["file"]["name"] . "<br /><br />";
echo "<strong>Type:</strong> " . $_FILES["file"]["type"] . "<br /><br />";
echo "<strong>Size:</strong> " . ($_FILES["file"]["size"] / 1024) . " Kb<br /><br />";
echo "<strong>Temp file:</strong> " . $_FILES["file"]["tmp_name"] . "<br /><br />";

if (file_exists("memberuploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{

move_uploaded_file($_FILES["file"]["tmp_name"],
"memberuploads/" . $_FILES["file"]["name"]);
echo "<strong>Stored in:</strong> " . "http://*****************/memberuploads/" . $_FILES["file"]["name"];
echo "<br/><br/><strong>ALL</strong> inappropriate images, movies, music <strong>WILL</strong> be deleted!";
$filename = $_FILES["file"]["name"];

mysql_connect("fdb1.awardspace.com", "**********", "********")
or die ('Connection to database failed: ' . mysql_error());
mysql_select_db("*********");

$today = date("D M d Y");
$filetype = $_FILES["file"]["type"];
$filesize = $_FILES["file"]["size"];
$ip = $_SERVER['REMOTE_ADDR'];

mysql_query("INSERT INTO tracking(id, username, date, ipaddress, filename, filetype, filesize) VALUES ('$id', '$username', '$today', '$ip', '$filename', '$filetype', '$filesize')");

}
}
}
else
{
echo "Invalid file";
}
?>

Link to comment
Share on other sites

Hmm odd when I was looking at the code you had included that last bit:  ')");

 

The $username = $_POST['username']; Is there a text field that the user has to enter their name? If there is make sure the <input name="" field is correct. Check the spelling. Probably 90% of the time when this happens to me It because I misspell a filed something like uesrname instead of username.

 

If you don't have a text field that the user fills out, did you mean $_SESSION['username']?

Would you mind posting the <form></form> code?

Link to comment
Share on other sites

<form action='file_upload2.php' method='post'
enctype='multipart/form-data'>
<br/><input type='file' name='file' id='file' size='50'/><br/><br/><strong>File Extensions Allowed:</strong> MP3, WMA, JPEG, PNG, GIF, BMP, SWF<br/><strong>File Size Limit:</strong> 5000 KB<br/>
<br />
<input name='username' type='hidden' value='".$_SESSION['username']."'/> <input type='submit' name='submit' value='Submit' />  <input type='reset' name='Reset' value='Reset' />
</form>

 

theres the form

Link to comment
Share on other sites

I see the hidden field, username. You do have the value set to $_SESSION['username']. You may just want to eliminate this hidden field. Manly for 2 reason. One is redundancy and second, security. Normally a user cannot modify a session; however, since you've added a hidden field, you actually unintentionally giving the use a chance to change their username with this. So in your php where you have

$username = $_POST['username']; you may just want to say

$username = $_SESSION['username'];

 

Now for the insert error. You may want to do a quick check to see if there is a value in $_SESSION['username']

 

You can also view the source of the page and see if that hidden field has a value.

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.