Help me with my upload script? :D

Hi all,

First time poster and PHP newbie here. I’m trying to write a script to allow users to upload text files of a certain size to my website. The script is about half my own work and half code from various tutorials. Currently, the script is echoing “Getting file extension substringCheck if file extension substring is validSorry, your file type is invalid” when I try to upload a file of a valid type, so I know there is probably an issue with the part of my script that gets the file extension substring. Unfortunately, I can’t seem to solve the issue on my own so I would appreciate any help I can get from you all! Here is my code:

[php]<?php

$upload_path = ‘./files/’; // The place the files will be uploaded to.
$max_filesize = 524288; // Max file size

// Begin checking for file extension
echo ‘Getting file extension substring’;
$valid_extensions = array(’.txt’, ‘.rtf’, ‘.doc’, ‘.docx’, ‘.TXT’, ‘.RTF’, ‘.DOC’,’.DOCX’); //Declare valid file extensions
$file_name = $_FILES[‘userfile’][‘name’];// Get the name of the file (including file extension)
$file_extension = strrchr($file_name,’.’);// get everything after the LAST .(dot)

//Check if file extension substring is valid
echo ‘Check if file extension substring is valid’;
if(in_array($file_extension ,$valid_extensions)){
echo ‘File type is valid’;

// Now check the filesize, if it is too large then DIE and inform the user.
echo ‘Checking filesize’;
if(filesize($_FILES[‘userfile’][‘tmp_name’]) > $max_filesize)
die(‘The file you attempted to upload is too large.’);

// Check if we can upload to the specified path, if not DIE and inform the user.
echo ‘Checking upload path’;
if(!is_writable($upload_path))
die(‘You cannot upload to the specified directory, please CHMOD it to 777.’);

// Upload the file to your specified path.
echo ‘Uploading file to specified path’;
if(move_uploaded_file($_FILES[‘userfile’][‘tmp_name’],$upload_path . $file_name))
echo ‘Your file upload was successful, view the file here’; // It worked.
}

else{

//Display error message if file type is not valid
echo ‘Sorry, your file type is invalid.’;

}

?>[/php]

i made some changes and i comented all innecesary echo lines
[php]

<?php $upload_path = './files/'; // The place the files will be uploaded to. $max_filesize = 524288; // Max file size // Begin checking for file extension //echo 'Getting file extension substring'; $valid_extensions = array('.txt', '.rtf', '.doc', '.docx'); //Declare valid file extensions $file_name = @basename($_FILES['userfile']['name']);// Get the name of the file (including file extension) $valid_extensions = @strtolower(@strrchr($file_name,".")); $valid_extensions = @substr($valid_extensions, 1); // remove dot //Check if file extension substring is valid //echo 'Check if file extension substring is valid'; if(in_array($file_extension ,$valid_extensions)){ //echo 'File type is valid'; // Now check the filesize, if it is too large then DIE and inform the user. echo 'Checking filesize'; if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); // Check if we can upload to the specified path, if not DIE and inform the user. // echo 'Checking upload path'; if(!is_writable($upload_path)) die('You cannot upload to the specified directory, please CHMOD it to 777.'); // Upload the file to your specified path. // echo 'Uploading file to specified path'; if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $file_name)) echo 'Your file upload was successful, view the file here'; // It worked. } else{ //Display error message if file type is not valid echo 'Sorry, your file type is invalid.'; } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service