what is wrong with this code ?

  Post new topicReply to topicPrintable Version
<< View previous topic View next topic >>
Share: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
#1   what is wrong with this code ?
cybershot
CZ Super Newbie
cybershot has been a member for over 17 year's 17 Year Member
usa.gif oregon.gif
Gender: Male
Website:
Status: Offline
Joined: Jan 31, 2007
0.01 posts per day
Posts: 75
Points: 25
   
i am trying to fix my upload program to only allow certain file types. I have it working this way

$uploaded_type = "text/php";

if ($uploaded_type =="text/php")

from what i understand. the first line defines the variable uploaded_type and says that it equals php
so now if i try to upload a php file, it doesn't work. It won't upload. that's good. but what i need is to tell the program to only allow file types that i want. because that's easier then trying to figure out what to cut out. so i tried this
$uploaded_type ="text/jpg";
$allowed_ext ="text/jpg";

if ($uploaded_type != "$allowed_ext")

but it doesn't work. i can't figure out why the != sign isn't working. can anyone shed some light on this



Attached Files
upload.txt (1.11 KB, Downloaded: 5235 Time(s))


Back to top Reply with quote
#2   re: what is wrong with this code ?
rlgnak
CZ Super Newbie
rlgnak has been a member for over 20 year's 20 Year Member
usa.gif alaska.gif
Occupation: College
Gender: Male
Status: Offline
Joined: Jun 22, 2004
0.01 posts per day
Posts: 62
Points: 3,367
   
ok its been a while but to limit the files you need to use this code

if ($_FILES['userfile']['type'] != "$allowed_ext")
{
echo "only jpg files are allowed<br>";
$ok=0;
}


if i understood right sorry if i didnt

EDIT:::::::

Oh ooops one more mistake the mime type is wromg jpeg is
image/jpeg




_________________
[ Register or login to view links on this board.]
Back to top Reply with quote
#3   re: what is wrong with this code ?
floppydrivez
CZ Addict
 Codezwiz Site Donator
floppydrivez has been a member for over 18 year's 18 Year Member
usa.gif mississippi.gif
Occupation: graphic design
Age: 43
Gender: Male
Website:
Status: Offline
Joined: Feb 26, 2006
0.08 posts per day
Posts: 518
Points: 4,848
AIM Address Yahoo Messenger MSN Messenger 
You can edit this to fit your needs, but it works well for image upload snipplets

$jpeg = 'image/jpeg';
$jpg = 'image/pjpeg';
$gif = 'image/gif';
$png = 'image/png';

$allowed_ext = $jpg || $gif || $png;



Back to top Reply with quote
#4   re: what is wrong with this code ?
cybershot
CZ Super Newbie
cybershot has been a member for over 17 year's 17 Year Member
usa.gif oregon.gif
Gender: Male
Website:
Status: Offline
Joined: Jan 31, 2007
0.01 posts per day
Posts: 75
Points: 25
   
wow, that is great info. i just posted it as jpeg because i was messing with the code just trying to get it to do one thing at a time. the upload file is really gonna be for jpegs, gifs, iwd, rar, zip. the site i am putting it on is a mapping site for call of duty 2. [ Register or login to view links on this board. ]
i want people to beable to upload text files as a way of submitting tutorials for mapping, and iwd's are the game files. so i thought it would be easier to tell the code what files to accept rather than what files to ignore.
one other problem i am having is that i want it to say please waite the file is being uploaded, then click over to the message. the file has been uploaded. does that need to go in the html form or can i do it in the code i attached. becuase now when they hit the submit button, it just sits there till the file is uploaded then displays the message that the file has been uploaded.
I have attached the html form



Attached Files
index.txt (767 Bytes, Downloaded: 5231 Time(s))


Back to top Reply with quote
#5   re: what is wrong with this code ?
cybershot
CZ Super Newbie
cybershot has been a member for over 17 year's 17 Year Member
usa.gif oregon.gif
Gender: Male
Website:
Status: Offline
Joined: Jan 31, 2007
0.01 posts per day
Posts: 75
Points: 25
   
It didn't work. I am not a programmer so it could take me days or weeks to figure this out, so please, take a look.

Here is the code that doesn't work.

<?php
if (eregi("block-Block_Creator.php",$PHP_SELF)) {
    Header("Location: index.php");
    die();
}

require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
include("header.php");
$index = 0;
$allowed_ext = "image/jpeg";
OpenTable();

$target = "../map/uploaded ";
$target = $target . basename( $_FILES['uploaded']['name']) ;

$ok=1;

//This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}

//This is our limit file type condition
if ($_FILES['userfile']['type'] != "$allowed_ext")

{
echo "only jpg files are allowed<br>";
$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "your file was not uploaded";
}

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
CloseTable();
include('footer.php');
?>


Here is the code that does work.
<?php
if (eregi("block-Block_Creator.php",$PHP_SELF)) {
    Header("Location: index.php");
    die();
}

require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
include("header.php");
$index = 0;
OpenTable();
$target = "../map/uploaded ";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$uploaded_type = "text/php, exe";
$ok=1;

//This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}

//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "Sorry, this file type is not allowed<br>";
$ok=0;
}

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "your file was not uploaded";
}

//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
CloseTable();
include('footer.php');
?>


I have been able to get the second bit of code to work. it won't let you upload php, from there i figure it shouldn't be hard to make it include other file types, but that would be more difficult then getting it to just let you upload types that i do need.


Back to top Reply with quote
#6   re: what is wrong with this code ?
rlgnak
CZ Super Newbie
rlgnak has been a member for over 20 year's 20 Year Member
usa.gif alaska.gif
Occupation: College
Gender: Male
Status: Offline
Joined: Jun 22, 2004
0.01 posts per day
Posts: 62
Points: 3,367
   
ok im curious is this going to be a block ?

the tutorial that you got the orignal code form is quiet odd . it seems to get variables out of nowhere . ( the code seems to be missing alot maybe you have rest of code somewhere else).

i wrote this

<?php

require_once("mainfile.php");
$module_name = basename(dirname(__FILE__));
get_lang($module_name);
include("header.php");
$index = 0;
$file_size = 350000;
$target = "../map/uploaded/ ";

OpenTable();

$run = true;

if(isset($_POST['Submit'])){

   if ($_FILES['imagefile']['type'] != "image/pjpeg" AND $_FILES['imagefile']['type'] != "image/jpeg" AND $_FILES['imagefile']['type'] != "image/jpg" AND $_FILES['imagefile']['type'] != "image/pjpg"){
        echo "Sorry, this file type is not allowed<br>";
        echo $_FILES['imagefile']['type'];
        $run = false;
   }
   
   if ($_FILES['imagefile']['size'] > $file_size){
        echo "Sorry, this file is to big<br>";
        $run = false;
   }
   
   if($run){
      copy ($_FILES['imagefile']['tmp_name'], $target.$_FILES['imagefile']['name']) or die ("Could not copy");
   }   
   
}else{

   echo '<form name="form1" method="post" action="" enctype="multipart/form-data"> ';
   echo '<input type="file" name="imagefile"> ';
   echo '<br> ';
   echo '<input type="submit" name="Submit" value="Submit"> ';
   
}

CloseTable();
include('footer.php');
?>


youll need to chmod your upload folder to 777




_________________
[ Register or login to view links on this board.]
Back to top Reply with quote
#7   re: what is wrong with this code ?
cybershot
CZ Super Newbie
cybershot has been a member for over 17 year's 17 Year Member
usa.gif oregon.gif
Gender: Male
Website:
Status: Offline
Joined: Jan 31, 2007
0.01 posts per day
Posts: 75
Points: 25
   
i have already modded the folder to 777. that only took a weekend to figure out.
as for the code. yes, you are right. it did leave out some stuff. how much stuff? i don't know. i am not a programmer. but the code originaly was set up to stop the upload of php and it wouldn't. i had to define the variable $uploaded_type. then it worked. So yes, i am piecing it together to finish it. I do not know why the tutorial i got the code from was incomplete. but i did post the entire code here. nothing more was given.



Back to top Reply with quote
#8   re: what is wrong with this code ?
cybershot
CZ Super Newbie
cybershot has been a member for over 17 year's 17 Year Member
usa.gif oregon.gif
Gender: Male
Website:
Status: Offline
Joined: Jan 31, 2007
0.01 posts per day
Posts: 75
Points: 25
   
Sorry bud. i tried and tried and tried. but i couldn't get that code you wrote to work. i tried it as a block and it crashes my website. i had to delete it.



Back to top Reply with quote
#9   re: what is wrong with this code ?
rlgnak
CZ Super Newbie
rlgnak has been a member for over 20 year's 20 Year Member
usa.gif alaska.gif
Occupation: College
Gender: Male
Status: Offline
Joined: Jun 22, 2004
0.01 posts per day
Posts: 62
Points: 3,367
   
thats becuase its a module not a block =-s , youd need to create a module first then make block with code ( edit the parts to match yoru module ) icon_mrgreen.gif

   echo '<form name="form1" method="post" action="modules.php?name=YOUR_MODULE_NAME_HERE" enctype="multipart/form-data"> ';
   echo '<input type="file" name="imagefile"> ';
   echo '<br> ';
   echo '<input type="submit" name="Submit" value="Submit"> ';


thats how i think its done for blocks , there mightbe eaiser way i dont know lol



_________________
[ Register or login to view links on this board.]
Back to top Reply with quote
#10   re: what is wrong with this code ?
cybershot
CZ Super Newbie
cybershot has been a member for over 17 year's 17 Year Member
usa.gif oregon.gif
Gender: Male
Website:
Status: Offline
Joined: Jan 31, 2007
0.01 posts per day
Posts: 75
Points: 25
   
ok, i will give it another try and let you know what happens



Back to top Reply with quote
Display posts from previous:      
Add To: Del.icio.us  Digg  Google  Spurl  Blink  Furl  Y! MyWeb  
<< View previous topic View next topic >>
Post new topicReply to topic

Jump to 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum