quick php question...

-Paul-

Fully Optimized
Messages
2,242
ok so im making an upload script, but i want only .txt files, to be allowed to be uploaded here is my script, what should i add to make it so only .txt files are uploaded...


PHP:
<center>
<font size="4">Here you can upload your file it must be in a .txt.  The max size is 50kilobytes.</font>
<br>
<?php
//edit this
$_max_file_size = '51200'; //file size in bytes.
$upload_dir = "uploads/"; //upload folder..chmod to 777
//end edit 

echo "Maximum file size : " . $_max_file_size/1000 . "KB<br>";
echo "<form enctype='multipart/form-data' action='?do=upload' method='post'>";
echo "<input type='hidden' name='MAX_FILE_SIZE' value='" . $_max_file_size , "'>";
echo "Choose a file to upload: <input name='filename' type='file'>";
echo "<input type='submit' value='Upload File'>";
echo "</form>";

if($_GET['do'] == 'upload')
{
$_random = rand(1, 1000000);
$target_path = $upload_dir . $_random . basename($_FILES['filename']['name']); 
$_file_name = $_random . basename($_FILES['filename']['name']);

if(move_uploaded_file($_FILES['filename']['tmp_name'], $target_path)) 
{
    echo "The file " . $_file_name . " has been uploaded.";
} 
else
{
    echo "There was an error uploading the file, please try again! Maybe be the file size. Maximum file size is " . $_max_file_size/1000 . "KB";
} 
}
?>
 
make a simple if statement to check the end of the filename. If it ends with ".txt" then allow the upload.

Code:
if ($filename = substr($filename, -4) == ".txt") {

put rest of upload code here
}
 
DanielASanders said:
make a simple if statement to check the end of the filename. If it ends with ".txt" then allow the upload.

Code:
if ($filename = substr($filename, -4) == ".txt") {

put rest of upload code here
}
can you um put it in up there, i dont know hardly any php...
 
make a simple if statement to check the end of the filename. If it ends with ".txt" then allow the upload.

PHP:
if ($filename = substr($filename, -4) == ".txt") {

if($_GET['do'] == 'upload')
{
$_random = rand(1, 1000000);
$target_path = $upload_dir . $_random . basename($_FILES['filename']['name']);
$_file_name = $_random . basename($_FILES['filename']['name']);

if(move_uploaded_file($_FILES['filename']['tmp_name'], $target_path))
{
    echo "The file " . $_file_name . " has been uploaded.";
}
else
{
    echo "There was an error uploading the file, please try again! Maybe be the file size. Maximum file size is " . $_max_file_size/1000 . "KB";
}
} 
}
 
Back
Top Bottom