Jump to content

My First Upload Script with Validation


richiejones24

Recommended Posts

I am a total noob at php and am trying to write a upload script with validation the script uploads the files ok but the validation does not work any help or ideas would be much appreciated.

 

$allowed_filetypes = array('.jpg','.gif','.bmp','.png'); //These will be the types of file that will pass the validation.

$max_filesize = 1; // Maximum filesize in BYTES (currently 0.5MB).

$upload_path = '../pic_upload/'; // The place the files will be uploaded to.

 

 

foreach ($_FILES["pictures"]["error"] as $key => $error) {

if(filesize($_FILES['pictures']['tmp_name'][$key]) > $max_filesize)  // Now check the filesize, if it is too large then DIE and inform the user.

      die('The file you attempted to upload is too large.');

 

$filename = $_FILES['pictures']['name'][$key];

$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);

 

if(!in_array($ext,$allowed_filetypes))

      die('The file you attempted to upload is not allowed.');

 

if ($error == UPLOAD_ERR_OK) {

      echo"$error_codes[$error]";

      move_uploaded_file($_FILES["pictures"]["tmp_name"][$key],$upload_path . $_FILES["pictures"]["name"][$key]) or die("Problems with upload");

  }

}

Link to comment
https://forums.phpfreaks.com/topic/242943-my-first-upload-script-with-validation/
Share on other sites

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.