Jump to content

PHP upload with extensions


dafallenangel

Recommended Posts

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

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!";
}
?>

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

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.