Jump to content

checking file extensions for upload


bryanptcs

Recommended Posts

Ok, I had the below code to check file extensions.  I have changed my upload code to handle more than 1 upload at a time, however in doing that, the pathinfo() is no longer reading 1 file name, it is reading an array and thus causing errror.

Here is my original code for checking extensions:

[code]
$allowed_ext = "jpg, gif, png, pdf, txt, psd, ai, doc, zip, rar, gz, jpeg"; // These are the allowed extensions of the files that are uploaded
// Check Entension
$extension = pathinfo($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for($i = 0; $i < count($allowed_paths); $i++) {
if ($allowed_paths[$i] == "$extension") {
$ok = "1";
}
}
[/code]
Link to comment
Share on other sites

I use this for multiple uploads and file and size checks. change if to your liking
[code]<?php
$absolute_path = "/home/theeenr8/public_html/misc"; //Absolute path to where files are uploaded
$size_limit = "yes"; //do you want a size limit yes or no.
$limit_size = "600000"; //How big do you want size limit to be in bytes
$limit_ext = "yes"; //do you want to limit the extensions of files uploaded
$ext_count = "1"; //total number of extensions in array below
$extensions = array(".jpg"); //List extensions you want files uploaded to be

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

        if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) {
          $extens = "any extension";
        } else {
        $ext_count2 = $ext_count+1;
        for($counter=0; $counter<$ext_count; $counter++) {
            $extens = "&nbsp; $extensions[$counter]";
        }
        }
        if (($limit_size == "") or ($size_limit != "yes")) {
            $limit_size = "any size";
        } else {
            $limit_size .= " bytes";
            $mb_size = ($limit_size/1000000);
        }
        $pichead = "<li><font size=\"2\" color=660000>File extension must be $extens<b>";
        $pichead .="</b></font>
        <li><font size=\"2\" color=660000>Maximum file size is $limit_size ($mb_size MB)</font></li>
        <li><font size=\"2\" color=660000>No spaces in the filename</font></li>";
?>
<html>
<head>
<title>HTML Form for uploading image to server</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<html>
<title>Page upload</title>
<body>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p><?=$pichead?></p>
<p>Pictures:<br />
1 <input type="file" name="pictures[]" /><br />
2 <input type="file" name="pictures[]" /><br />
3 <input type="file" name="pictures[]" /><br />
4 <input type="file" name="pictures[]" /><br />
5 <input type="file" name="pictures[]" /><br />
6 <input type="file" name="pictures[]" /><br />
<input type="submit" name=submit value="Send" />
</p>
</form>
<?php
} else {
$i=0;
$photoarray = array();
  foreach ($_FILES["pictures"]["error"] as $key => $error) {
  $file_name =  $_FILES["pictures"]['name'][$i]; // can call this anything you like this will take the original name
  $file =  $_FILES["pictures"]['tmp_name'][$i];
  $photoarray[$i+1]= $file_name;
  $endresult = "<font size=\"4\" color=990000>$file_name uploaded successfully</font>";
    if ($file_name == "") {
    $pic = $i+1;
    $endresult = "<font size=\"4\" color=990000>Pic#$pic Not selected</font>";
    }else{
      if(file_exists("$absolute_path/$file_name")) {
      $endresult = "<font size=\"4\" color=990000>File Already Existed</font>";
      } else {
        if (($size_limit == "yes") && ($limit_size < $file_size)) {
        $endresult = "<font size=\"4\" color=990000>File was to big</font>";
        } else {
        $ext = strrchr($file_name,'.');
          if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
          $endresult = "<font size=\"4\" color=990000>File is wrong type</font>";
          }else{
          copy($file, "$absolute_path/$file_name") or $endresult = "<font size=\"4\" color=990000>Couldn't Copy File To Server</font>";
          }
        }
      }
    }
  $i++;
  echo $endresult."<br>";
  }
}
?>
</body>
</html>[/code]

Ray
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.