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
https://forums.phpfreaks.com/topic/58871-file-uploader-problems/
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)) {

}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.