Jump to content

file uploader problems


chris_rulez001

Recommended Posts

hi i have made a file upload script and its not working, its saying im firefox that the connection has been reset when it hasn't.

 

my code:

 

<?php
$id = $_POST['id'];
$username = $_POST['username'];
$allowed_ext = "jpg, gif, png, pdf, bmp, swf, mp3, wma";

if( strpos($_FILES["file"]["type"], $allowed_ext) == true 
&& $_FILES["file"]["size"] < 50000 )
{
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://*********.awardspace.com/memberuploads/" . $_FILES["file"]["name"];
echo "<br/><br/><strong>ALL</strong> inappropriate images, movies, music <strong>WILL</strong> be deleted!";
echo "<br/><br/>Click <a href='index.php'>here</a> to return to the homepage.";
$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

This will never work, the only way for strpos() to find a match is if $_FILES['file']['type'] is a string equal to the entire contents of $allowed_ext, in which case strpos() will return int(0) which will evaluate to FALSE.

<?php
$allowed_ext = "jpg, gif, png, pdf, bmp, swf, mp3, wma";
if (strpos($_FILES["file"]["type"], $allowed_ext) == true) {

}
?>

 

This would work:

<?php
$allowed_ext = "jpg, gif, png, pdf, bmp, swf, mp3, wma";
$allowed_ext = preg_split('/\s*,\s*/', $allowed_ext, -1, PREG_SPLIT_NO_EMPTY);

if (in_array($_FILES['file']['type'], $allowed_ext)) {

}
?>

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.