Jump to content

[SOLVED] Where in code to add $allowed_extensions array?


alconebay

Recommended Posts

I was following this tutorial (http://adriaanvm.com/archives/howto-make-a-simple-files-upload-system) on a file upload system for images and everything is working perfect, only the upload system lets anything under 100kb in. Even if it's not a image. That tutorial said you could restrict file types to images using this:

 

$allowed_extensions = array(

'png',

'gif',

'bmp',

'jpg',

'jpeg'

);

and using this:

$filename = $_FILES['file']['name'];

$new_filename = explode('.', $filename);

if(in_array($new_filename[count($new_filename) -1], $allowed_extensions)) {

// Continue with the code

} else {

// Stop the code.

}

 

But where do I put that code within this:

 

<?php

 

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

// If action is add.

if($_POST['action'] == 'add') {

// Define the folder where the file will be uploaded.

$upload_path = './uploads/' . $_FILES['file']['name'];

// Check if the file already exists in the folder.

if(!file_exists($upload_path)) {

// Check the size of the file.

// The file should be under 100 kb. But OVER 0 bytes.

if($_FILES['file']['size'] < 100000 && $_FILES['file']['size'] > 0) {

// If there are any errors.

if($_FILES['file']['error']) {

echo "Error: " . $_FILES['file']['error'];

} else {

// Success!

echo "File successfully uploaded.<br /><br />";

echo "The filename is: <strong>" . $_FILES['file']['name'] . "</strong><br />";

echo "The filesize is: <strong>" . $_FILES['file']['size'] . "</strong><br />";

echo "The filetype is: <strong>" . $_FILES['file']['type'] . "</strong><br />";

echo "The file is temporary saved in: <strong>" . $_FILES['file']['tmp_name'] . "</strong><br />";

 

// Move uploaded file to upload folder.

move_uploaded_file($_FILES['file']['tmp_name'], $upload_path);

}

} else {

echo $_FILES['file']['size'] < 1 ? 'Incorrect filename.' : 'File size is too big';

}

} else {

echo "File already exists in your folder. ";

}

}

}

?>

 

Ive tried just about every way I can think of to insert those two snips of code into the large section of code but I cant get it to work. It  still lets non image files in.

Link to comment
Share on other sites

do like this...

<?php

 

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

$allowed_extensions = array(

  'png',

  'gif',

  'bmp',

  'jpg',

  'jpeg'

);

 

$new_filename = explode('.', $_FILES['file']['name']);

if(in_array($new_filename[count($new_filename) -1], $allowed_extensions))

{

 

      $upload_path = "D:/apachefriends/xampp/htdocs/prac/uploads/" . $_FILES['file']['name'];

 

if(!file_exists($upload_path)) {

if($_FILES['file']['size'] < 100000 && $_FILES['file']['size'] > 0) {

 

            if($_FILES['file']['error']) {

              echo "Error: " . $_FILES['file']['error'];

            } else {

              // Success!

  move_uploaded_file($_FILES['file']['tmp_name'], $upload_path);

              echo "File successfully uploaded.";

              echo "The filename is: <strong>" . $_FILES['file']['name'] . "</strong>";

              echo "The filesize is: <strong>" . $_FILES['file']['size'] . "</strong>";

              echo "The filetype is: <strong>" . $_FILES['file']['type'] . "</strong>";

              echo "The file is temporary saved in: <strong>" . $_FILES['file']['tmp_name'] . "</strong>";

 

              // Move uploaded file to upload folder.

             

            }

}

          else {

            echo $_FILES['file']['size'] < 1 ? 'Incorrect filename.' : 'File size is too big';

        }

      }else {

        echo "File already exists in your folder. ";

      }

  }

  else {

 

  echo "not a valid extension";

}

 

 

}

?>

Link to comment
Share on other sites

On more question, I want to rename the image being uploaded to add the uploaders user name (which is carried over from step one of the form) and add it to the beginning of the image being uploaded. So it would look like "username_theirimage.jpg." (btw, I don't want to limit it to jpgs) I'm assuming I would modify this line:

 

$new_filename = explode('.', $_FILES['file']['name']);

 

and integrate the $username into the filename. How would I do that?

 

Thanks

Link to comment
Share on other sites

It took me 4 hours (Im a noob) but I figured it out! Here is everything:

 

<?php

$username = 1234(or whatever);

 

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

  $allowed_extensions = array(

  'png',

  'gif',

  'bmp',

  'jpg',

  'jpeg',

  'PNG',

  'GIF',

  'BMP',

  'JPG',

  'JPEG'

);

$filename = $_FILES['file']['name'];

$prefixed = $username . "_" . $filename;

$filesize = $_FILES['file']['size'];

$fileerror = $_FILES['file']['error'];

$filetmp = $_FILES['file']['tmp_name'];

$new_filename = explode('.', $prefixed);

if(in_array($new_filename[count($new_filename) -1], $allowed_extensions))

{

 

      $upload_path = "./mypath/" . $prefixed;

 

        if(!file_exists($upload_path)) {

          if($filesize < 500000 && $filesize > 0) {

 

            if($fileerror) {

              echo "Error: " . $fileerror;

            } else {

              // Success!

  move_uploaded_file($filetmp, $upload_path);

              echo "Finished";             

            }

        }

          else {

            echo $filesize < 1 ? 'too big.';

        }

      }else {

        echo "name already used";

      }

    }

    else {

 

  echo "bad extension.";

}

     

 

}

?>

 

That will rename name "photo.jpg" to "1234(or whatever)_photo.jpg" ($username_photo.jpg)

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.