Jump to content

[SOLVED] $uploaded_type help


pleek

Recommended Posts

ok, i think that means try it. lol, i did and it didn't work. here's my upload script.

 

<?php
$target = "Games/";
$target = $target . basename( $_FILES['uploaded']['name']) ;

//This is our limit file type condition
if ($uploaded_type != "application/x-nes-rom")
{
echo "Only .NES files are allowed.<br>";
}

//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.";
}
}
?> 

 

now what....

make an upload form that submits to

<?php
echo "<pre>";
print_r($_FILES);
?>

and upload some files to it and see what they come up as.

 

Scott.

 

thank you for that, and your first post correctly told me the type

 

Array
(
    [uploadedfile] => Array
        (
            [name] => 3DWORLD.NES
            [type] => application/octet-stream
            [tmp_name] => /tmp/phpyQzpdv
            [error] => 0
            [size] => 262160
        )

)

 

so i changed my code to

 

<?php
$target = "Games/";
$target = $target . basename( $_FILES['uploaded']['name']) ;

//This is our limit file type condition
if ($_FILES['userfile']['type'] != "application/octet-stream")
{
echo "Only .NES files are allowed.<br>";
echo "<pre>";
print_r($_FILES);
}

//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.";
}
}
?> 

 

but it still doesn't work. Any other suggestions?

i tried the code you posted and i get the same results

 

Only .NES files are allowed.

Array
(
    [uploadedfile] => Array
        (
            [name] => 3DWORLD.NES
            [type] => application/octet-stream
            [tmp_name] => /tmp/php7s7TE1
            [error] => 0
            [size] => 262160
        )

)

 

and as for "Why not just check the filename?" that would be fine as long as i can check the file name and extension. so like "if file = game.nes"

i tried the code you posted and i get the same results

and as for "Why not just check the filename?" that would be fine as long as i can check the file name and extension. so like "if file = game.nes"

 

Does the filename have to br a specific game? I am confused. The code I posted above will verify that the extension is .nes no matter the case. Is that not what you wanted? As far as verifying that the file is a true .nes and not just some text file I uploaded, you can check that octet stream, but yea. I am not sure how you would go about verifying that.

 

EDIT:

I noticed I used $type, instead of the $_FILES['userfile']['name']  use that instead of $type, sorry I had it set to $type for testing purposes.

i tried the code you posted and i get the same results

and as for "Why not just check the filename?" that would be fine as long as i can check the file name and extension. so like "if file = game.nes"

 

Does the filename have to br a specific game? I am confused. The code I posted above will verify that the extension is .nes no matter the case. Is that not what you wanted? As far as verifying that the file is a true .nes and not just some text file I uploaded, you can check that octet stream, but yea. I am not sure how you would go about verifying that.

To that, no the the filename does not have to be a specific game. I added your code to my script and it didn't work.

 

Current code...

<?php
$target = "Games/";
$target = $target . basename( $_FILES['uploaded']['name']) ;

//This is our limit file type condition
if (!preg_match("/.*\.nes$/i", $type))
{
echo "Only .NES files are allowed.<br>";
echo "<pre>";
print_r($_FILES);
}

//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.";
}
}
?> 

 

Once again, try using the correct variable name in your condition.  You're checking $_FILES['userfile']['type] and that doesn't exist.

 

Um. ok then what should i check? I was going by what was posted earlier about $_file that linked me to http://us3.php.net/features.file-upload

In your code, I see you using 3 different names in different places:

 

$_FILES['uploaded']..

$_FILES['userfile']..

$_FILES['uploadedfile']..

 

Well what did you name your file input field in your form?  It's really simple:  If you have a form with a file upload input field and you name it "blah" then you use "blah" not "someothername". Posted examples are just that: examples.  You need to put your own var names into it.

There's a working example in the manual. Link has already been provided in this thread. Are you seriously wanting me to type out for you how to use a variable name to refer to its value?

 

$name = "something";

echo $came; // why won't this work?
echo $blah; // omg it still won't print 'something'
echo $wtf; // wtf is going on, for the love of god, please just echo 'something'
echo $mame; // ooh, ooh, I'm getting closer, I can feel it....
echo $name; // oh...there it is.  I guess I had to spell it like 'name' cuz like, that's the variable's name

 

i understand how to use var but i don't know how to check it with the file that is being uploaded.

 

$filename = game.nes
$filecheck = THE UPLOADED FILE NAME WITH EXTENSION

if ( $filecheck != $filename) {
echo' Only .nes games can be uploaded';
}

 

what i don't know is how to get the name and extension of the file that is being uploaded to set it to $filecheck.

 

Now do you get where im stuck? and by "in the manual" do you mean the $_file link post earlier? cause when i tried $_FILES['userfile']['type'] i was told i was doing something wrong and not setting the var correctly.

Now do you get where im stuck? and by "in the manual" do you mean the $_file link post earlier? cause when i tried $_FILES['userfile']['type'] i was told i was doing something wrong and not setting the var correctly.

 

EXACTLY.  In the manual, the example is 'userfile'  because in their example form they have

 

Send this file: <input name="userfile" type="file" />

 

You need to insert your own variable name in $_FILES['YOUROWNNAMEHERE'][...] : The one you used in your form. 

now that you explained that both checks are working. thanks a lot guys!!!!!

 

<?php

$target_path = "Games/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 
$file = "3DWORLD.NES";

if($_FILES['uploadedfile']['name'] == $file) {
echo'the files are the same<BR>';
}

if ($_FILES['uploadedfile']['type'] == "application/octet-stream")
{
echo "the file is a .NES<BR>";
}

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}


?>

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.