I wish to limit uploading to the video files swf pps bmp pps png gif mpg mp3 and wav - i'm sure I have the file / type incorrect. can someone please give logical solution...

here's my code:

<?php
if ((($_FILES["file"]["type"] == "video/swf")
|| ($_FILES["file"]["type"] == "video/pps")
|| ($_FILES["file"]["type"] == "video/bmp")
|| ($_FILES["file"]["type"] == "video/pps")
|| ($_FILES["file"]["type"] == "video/png")
|| ($_FILES["file"]["type"] == "video/gif")
|| ($_FILES["file"]["type"] == "video/mp3")
|| ($_FILES["file"]["type"] == "video/wav"))
&& ($_FILES["file"]["size"] < 200000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 200000) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Thank you for your" . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Dani AI

Generated

A few concrete points to fix that cover what and started: the upload is failing because the code compares $_FILES['file']['type'] to incorrect MIME strings (and many of the extensions listed are not video types). Also confirm server settings and the HTML form are correct — enctype="multipart/form-data" and method="post", and check PHP settings such as file_uploads, upload_max_filesize and post_max_size.

Do not trust $_FILES['file']['type'] alone (it comes from the client). Use a server-side MIME detector and an extension whitelist, and compare both. Typical MIME mappings you’ll want to accept (examples): swf => application/x-shockwave-flash, pps => application/vnd.ms-powerpoint, bmp => image/bmp, png => image/png, gif => image/gif, mpg => video/mpeg, mp3 => audio/mpeg, wav => audio/x-wav. Here is a minimal pattern showing the safer check:

// server-side MIME + extension whitelist
$allowed = [
  'swf'=>'application/x-shockwave-flash',
  'pps'=>'application/vnd.ms-powerpoint',
  'bmp'=>'image/bmp',
  'png'=>'image/png',
  'gif'=>'image/gif',
  'mpg'=>'video/mpeg',
  'mp3'=>'audio/mpeg',
  'wav'=>'audio/x-wav',
];

$ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $_FILES['file']['tmp_name']);
finfo_close($finfo);

if (isset($allowed[$ext]) && $allowed[$ext] === $mime && $_FILES['file']['size'] <= 200000) {
    // sanitize and generate a safe unique name, then move_uploaded_file()
} else {
    // reject upload
}

Also check these troubleshooting items: inspect $_FILES['file']['error'] (use UPLOAD_ERR_* constants), confirm the upload directory exists and is writable (avoid 0777 if you can), use uniqid() or a hash for stored filenames to prevent collisions, and store uploaded files outside the webroot or prevent execution (do not let uploaded .php run). Finally, validate content where possible (image functions or file-signature checks) rather than relying only on extension/MIME.

Recommended Answers

All 2 Replies

I wish to limit uploading to the video files swf pps bmp pps png gif mpg mp3 and wav>

Not all of those are video files. (Or rather; the extensions of known video formats.)
Only SWF and MPG would qualify. Unless you also count animated images, in which case PNG and GIF would count as well.

But yea, you have the mime types wrong.
You can look up the proper mime types at w3schools.com.

Maybe the trouble in php.ini maxfileupload.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.