Based on this two questions:
I wrote an easy password protected file uploader.
So I have password verifier named password.php
<body>
<?php
$pass = $_POST['pass'];
{?>
<form method="POST" action="password.php">
Password <input type="password" name="pass"></input>
<input type="submit" name="submit" value="Ok"></input>
</form>
<?}
if($pass == "admin")
{
include("../uploader.html");
}
?>
</body>
As you can tell from the code. If pass is ok it includes uploader.html from out of public_html folder.
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input name="thefile" type="file" /><br>
<input type="submit" value="Send" />
</form>
</body>
In the end we use upload.php to send "thefile" to folder named files.
<body>
<?php
$thefile_tmp = $_FILES['thefile']['tmp_name'];
$thefile_name = $_FILES['thefile']['name'];
if(is_uploaded_file($thefile_tmp))
{
move_uploaded_file($thefile_tmp, "files/$thefile_name");
}
?>
</body>
I changed files folder's chmod to 777 and everything works fine.
I gave the password to bunch of students and I asked them to send me reports. A student now can anonymously upload a malicious file (lets say loop.php) and open it in a browser .../files/loop.php.
So I want to protect my site from such stories. I tried to set files folder's chmod to 773 or 776. On 776 my code lost ability to send files. On 773 you can still open things in files folder.
Question/Request Is there an easy way to block uploaded files from opening?
Good easy solution which I used
Based on @Fred -ii- suggestion I added to upload.php an extension verifier. Now it look like this
<body>
<?php
$thefile_tmp = $_FILES['thefile']['tmp_name'];
$thefile_name = $_FILES['thefile']['name'];
$thefile_ext = pathinfo($thefile_name, PATHINFO_EXTENSION);
if(is_uploaded_file($thefile_tmp))
{
if( $thefile_ext == 'zip')
{
move_uploaded_file($thefile_tmp, "files/$thefile_name");
}
}
?>
</body>
Alternative solution
As @alexander.polomodov answeres. I just put .htaccess file in files folder and still files can be send, but no one can access it. This method does not fit me, cause I wanted students to be able to peep at each others reports.