dafallenangel Posted December 15, 2008 Share Posted December 15, 2008 I have a basic upload script and I'm not sure on how to add file extensions to be accepted i need .exe, .zip, .rar, and .7z extensions to be accepted how can i do this? <?php include 'db.php'; $title = $_POST['title']; $sim = $_POST['sim']; $page = $_POST['page']; $target_path = "download/"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { $sql = mysql_query("INSERT INTO download (title, filename, sim, page) VALUES('$title', '". $_FILES['uploadedfile']['name'] ."', '$sim', '$page')"); echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; echo "You will be redirected in 10 seconds unless you want to upload another file"; echo '<a href=\"..index.php?seashadows=upload\">Upload another file</a>'; echo '<meta HTTP-EQUIV="REFRESH" content="10; url=../index.php">'; } else{ echo "There was an error uploading the file, please try again! or contact <a href=\"mailto:webmaster\">webmaster</a>"; } ?> Sorry haven't done extensions acceptions before.. Thank you DafallenAngel Link to comment https://forums.phpfreaks.com/topic/137088-php-upload-with-extensions/ Share on other sites More sharing options...
gevans Posted December 15, 2008 Share Posted December 15, 2008 $allowedExtensions = array("exe", "zip", "rar", "7z"); if(in_array(end(explode(".", $_FILES['userfile']['name'])), $allowedExtensions){ //it's got the right extension } You could aslo look into $_FILES['userfile']['type'] Link to comment https://forums.phpfreaks.com/topic/137088-php-upload-with-extensions/#findComment-715992 Share on other sites More sharing options...
dafallenangel Posted December 15, 2008 Author Share Posted December 15, 2008 $allowedExtensions = array("exe", "zip", "rar", "7z"); if(in_array(end(explode(".", $_FILES['userfile']['name'])), $allowedExtensions){ //it's got the right extension } You could aslo look into $_FILES['userfile']['type'] thank you Link to comment https://forums.phpfreaks.com/topic/137088-php-upload-with-extensions/#findComment-715994 Share on other sites More sharing options...
dafallenangel Posted December 16, 2008 Author Share Posted December 16, 2008 I have changed my upload script and it works on my Xampp server but when I upload it to the site it uploads all the data but it doesn't transfer the file to the folder and yes i did set the chmod to 777 here is the script <?php // ============== // Configuration // ============== include 'db.php'; $title = $_POST['title']; $sim = $_POST['sim']; $page = $_POST['page']; $uploaddir = "download/"; // Where you want the files to upload to - Important: Make sure this folders permissions is 0777! $allowed_ext = "zip, exe, rar, 7z"; // These are the allowed extensions of the files that are uploaded $max_size = "50000"; // 50000 is the same as 50kb // 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"; } } // Check File Size if ($ok == "1") { if($_FILES['file']['size'] > $max_size) { print "File size is too big!"; exit; } // The Upload Part if(is_uploaded_file($_FILES['file']['tmp_name'])) { move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']); } $sql = mysql_query("INSERT INTO download (title, filename, sim, page) VALUES('$title', '". $_FILES['file']['name'] ."', '$sim', '$page')"); print "Your file has been uploaded successfully! Yay!"; } else { print "Incorrect file extension!"; } ?> Link to comment https://forums.phpfreaks.com/topic/137088-php-upload-with-extensions/#findComment-716335 Share on other sites More sharing options...
gevans Posted December 16, 2008 Share Posted December 16, 2008 change this line move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name']); to if(move_uploaded_file($_FILES['file']['tmp_name'],$uploaddir.'/'.$_FILES['file']['name'])){ echo 'all good'; }else{ echo 'problem with upload'; } this will see if it's actually moving the emp file into the new directory Link to comment https://forums.phpfreaks.com/topic/137088-php-upload-with-extensions/#findComment-716337 Share on other sites More sharing options...
dafallenangel Posted December 16, 2008 Author Share Posted December 16, 2008 well i think its timing me out or something ??? because when i upload a small file it goes right in now if I upload a 5 mb (and editing the max_size for 10 mb) it doesn't out put the echo from the if move uploaded file part.. Link to comment https://forums.phpfreaks.com/topic/137088-php-upload-with-extensions/#findComment-716341 Share on other sites More sharing options...
gevans Posted December 16, 2008 Share Posted December 16, 2008 as well as max_file_size look for... post_max_size = 10M ;EDITED TO 10M; That's also in php.ini with upload_max_filesize = 16M Link to comment https://forums.phpfreaks.com/topic/137088-php-upload-with-extensions/#findComment-716532 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.