icuucme Posted April 20, 2008 Share Posted April 20, 2008 How can i make upload validation that the uploaded file can only be of a certain name? eg. The only file that can be uploaded is "name.txt" no other name is valid Link to comment https://forums.phpfreaks.com/topic/101999-php-file/ Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 What would be the purpose of restricting the upload file name? Are you wanting to only allow uploads of a certain file type? Link to comment https://forums.phpfreaks.com/topic/101999-php-file/#findComment-521996 Share on other sites More sharing options...
icuucme Posted April 20, 2008 Author Share Posted April 20, 2008 yea certain file type (txt specifically) and it would be better if it could be of a certain name only (eg name.txt only) Link to comment https://forums.phpfreaks.com/topic/101999-php-file/#findComment-521998 Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 You can read the file name from the array $_FILES. The name should be in $_FILES['name_of_input_field']['name']. You can also see the mime type that the browser sent in $_FILES['name_of_input_field']['type']. Read this for more information on handling file uploads: http://us.php.net/manual/en/features.file-upload.php But remember that if you are wanting sort files based on their type, you will need to examine the actual file contents to determine the type. When a user uploads a file, he call at any name or any type, even though it may be incorrect. Link to comment https://forums.phpfreaks.com/topic/101999-php-file/#findComment-522004 Share on other sites More sharing options...
icuucme Posted April 20, 2008 Author Share Posted April 20, 2008 You can read the file name from the array $_FILES. The name should be in $_FILES['name_of_input_field']['name']. You can also see the mime type that the browser sent in $_FILES['name_of_input_field']['type']. Read this for more information on handling file uploads: http://us.php.net/manual/en/features.file-upload.php But remember that if you are wanting sort files based on their type, you will need to examine the actual file contents to determine the type. When a user uploads a file, he call at any name or any type, even though it may be incorrect. does $_FILES['name_of_input_field']['name'] only give the file name or does it give the directory_location + file_name ? Also what is the mime type for a txt file? i tried $_FILES['name_of_input_field']['type'] == "text/plain" but it didnt work? Link to comment https://forums.phpfreaks.com/topic/101999-php-file/#findComment-522006 Share on other sites More sharing options...
dptr1988 Posted April 20, 2008 Share Posted April 20, 2008 There are many other items in that array. You can read about them in the link I gave you. The normal way to handle an upload is to get it's name from $_FILES['name_of_input_field']['tmp_name'] and use move_uploaded_file() to move it from the upload directory to your destination directory. http://us.php.net/manual/en/function.move-uploaded-file.php Example from PHP manual <?php // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead // of $_FILES. $uploaddir = '/var/www/uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); echo '<pre>'; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { echo "File is valid, and was successfully uploaded.\n"; } else { echo "Possible file upload attack!\n"; } echo 'Here is some more debugging info:'; print_r($_FILES); print "</pre>"; ?> If you want to see the exact format of the mime type that was sent, use something like var_dump($_FILES) to show the structure of the array and the mime type. Then try uploading a text file and you will see the mime type that your browser sends for text files. Link to comment https://forums.phpfreaks.com/topic/101999-php-file/#findComment-522009 Share on other sites More sharing options...
icuucme Posted April 20, 2008 Author Share Posted April 20, 2008 it was a silly mistake on my end the form had the wrong name...that's y it was giving me invalid file. it's working now Link to comment https://forums.phpfreaks.com/topic/101999-php-file/#findComment-522015 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.