Uploading a txt file problem (example script)...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

list($filename, $fileext) = split(".", $HTTP_POST_FILES['file']); is an array, you want:
list($filename, $fileext) = split(".", $HTTP_POST_FILES['file']['name']);
If you put error_reporting(E_ALL); as the first line of your script you'll see it's full of buggets ;)
I've tweaked your version a bit (and used the superglobals as there's no reason not to unless you're using PHP < 4.1.0):

Code: Select all

<?php
error_reporting(E_ALL);
include("dbconnection/connect_db.inc.php");
if (!empty($_POST['submit'])) {
  list($filename, $fileext) = split(".", $_FILES['file']['name']);
  print_r($HTTP_POST_FILES);
  print_r($fileext);
  if (!is_uploaded_file($_FILES['file']['tmp_name'])) {
    $error = "You did not upload a file!";
    unlink($_FILES['file']['tmp_name']);
    // assign error message, remove uploaded file, redisplay form.
  } else {
    //a file was uploaded
    $maxfilesize=3000000;

    if ($fileext == "php")
    {
      $error = "This file type is not allowed.";
      unlink($HTTP_POST_FILES['file']['tmp_name']);
      // assign error message, remove uploaded file, redisplay form.
    } elseif ($_FILES['file']['size'] > $maxfilesize) {
      $error = "This file is too large";
      unlink($_FILES['file']['tmp_name']);
      // assign error message, remove uploaded file, redisplay form.
    } elseif ($_FILES['file']['type'] != "text/plain") {
        $error = "This file type is not allowed";
        unlink($_FILES['file']['tmp_name']);
        // assign error message, remove uploaded file, redisplay form.
      } else {
       //File has passed all validation, copy it to the final destination and remove the temporary file:
       copy($_FILES['file']['tmp_name'],"faqs/".$_FILES['file']['name']);
       unlink($_FILES['file']['tmp_name']);
       print "File has been successfully uploaded!";
       print "$filename";
       print "$fileext";
       exit;
     }
   }
}

?>
<html>
<head></head>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<?php if(!empty($error)) echo $error; ?>
<br><br>
Choose a file to upload:<br>
<input type="file" name="file"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

It still uploads a PHP file :(

http://www.bolt3.com/uploadtest.php
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Your link shows:
This file type is not allowed
When i upload a .php file? :o
jonas
Forum Commoner
Posts: 96
Joined: Sun May 23, 2004 9:25 pm

Post by jonas »

Not for me :(


ACtually the .php file I am trying to upload is the uploadtest.php file.

Maybe uploading the same file is automatically allowed?

So yes, it does work unless it's uploading itself.

Odd.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try this one, the previous did have a logic problem :o

Code: Select all

<?php
error_reporting(E_ALL);
include("dbconnection/connect_db.inc.php");
if (!empty($_POST['submit'])) {
  $fileext = end(explode('.', $_FILES['file']['name']));
  print_r($HTTP_POST_FILES);
  print_r($fileext);
  if (!is_uploaded_file($_FILES['file']['tmp_name'])) {
    $error = "You did not upload a file!";
    unlink($_FILES['file']['tmp_name']);
    // assign error message, remove uploaded file, redisplay form.
  } else {
    //a file was uploaded
    $maxfilesize=3000000;

    if ($fileext != 'txt')
    {
      $error = "This file type is not allowed.";
      unlink($HTTP_POST_FILES['file']['tmp_name']);
      // assign error message, remove uploaded file, redisplay form.
    } elseif ($_FILES['file']['size'] > $maxfilesize) {
      $error = "This file is too large";
      unlink($_FILES['file']['tmp_name']);
      // assign error message, remove uploaded file, redisplay form.
    } else {
       //File has passed all validation, copy it to the final destination and remove the temporary file:
       copy($_FILES['file']['tmp_name'],"faqs/".$_FILES['file']['name']);
       unlink($_FILES['file']['tmp_name']);
       print "File has been successfully uploaded!";
       print $_FILES['file']['name'];
       print '<br />Ext: '.$fileext;
       exit;
     }
   }
}

?>
<html>
<head></head>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data">
<?php if(!empty($error)) echo $error; ?>
<br><br>
Choose a file to upload:<br>
<input type="file" name="file"><br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

I am unable to upload .php files to your server. Thats strange!
Post Reply