Jump to content

[SOLVED] Problem with "IF" statements


sh0wtym3

Recommended Posts

Hey all,

 

The upload script below is written to STOP the upload if the file is not an MP3, too large, or is being written to a folder than doesn't exist. It works, but not quite the way it should.

 

When you upload a NON-MP3 file, it redirects you to http://www.mysite.com/members.php?cmd=filetype as it should.

 

However when you upload a file that is too big, it SHOULD redirect you to http://www.mysite.com/members.php?cmd=filesize, but instead it redirects you to http://www.mysite.com/members.php?cmd=filetype as well.

 

??

 

Thanks in advance.

 

<?php
//Connect to Database
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

   // Configuration - Your Options
   $filesize = $_FILES['title']['size'];
   $username = $_COOKIE['ID_my_site'];
   $allowed_filetypes = array('.mp3'); // These will be the types of file that will pass the validation.
   $max_filesize = 10000001; // Maximum filesize in BYTES (currently 0.5MB).
   $upload_path = './uploads/'.$username.'/'; // The place the files will be uploaded to (currently a 'files' directory).
  
   //Make folder for user
   ob_start();
   mkdir("uploads/".$username."/", 0700);
   ob_end_clean();
   
   // Get the name of the file and then separate the extension.
   $filename = $_FILES['userfile']['name'];
   $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
   
   //Renames the file
   $file = str_replace(' ', '_', $_POST['title']);
   $file = strtolower($file);
   
   //Create New File Name
   $new_file_name=$username.'_'.$file.$ext;
   
   // Check if the filetype is allowed, if not DIE and redirect the user.
   if(!in_array($ext,$allowed_filetypes)) {
   header("Location: http://www.mysite.com/members.php?cmd=filetype");
   }
   die('');

   // Now check the filesize, if it is too large then DIE and redirect the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) {
   header("Location: http://www.mysite.com/members.php?cmd=filesize");
   }
   die('');

   // Check if we can upload to the specified path, if not DIE and redirect the user.
   if(!is_writable($upload_path)) {
   header("Location: http://www.members.com/members.php?cmd=fileerror");
   }
   die('');

Thanks for your response, I tried swapping out that line but it didn't seem to work.

 

I then tried experimenting a little further, and it works now. I don't know why but when I included the die() inside the {} it worked. For example instead of:

   // Now check the filesize, if it is too large then DIE and redirect the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) {
   header("Location: http://www.mysite.com/members.php?cmd=filesize");
   }
   die('');

 

I used:

   // Now check the filesize, if it is too large then DIE and redirect the user.
   if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) {
   header("Location: http://www.mysite.com/members.php?cmd=filesize");
   die('');
   }

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.