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