I upload a file say 'example.csv'...How do I store 'example' inside a variable using php so as to create a table in the database having the table name as 'example'?
Asked
Active
Viewed 268 times
-4
-
You can use both `pathinfo()` and `basename()` PHP functions for that. – mitkosoft Mar 18 '16 at 13:45
-
The form that uploads a file to the database is of enctype=multipart/form-data. Hence i am getting an encoded filename. How to resolve this issue in order to get the filename without the extension? Please help! Thanks! – Tanay Pardeshi Mar 18 '16 at 13:56
-
@TanayPardeshi I post an answer below, you can very easily get this filename without the extension. – jszobody Mar 19 '16 at 02:17
-
Got it! Thanks! :) – Tanay Pardeshi Mar 20 '16 at 03:21
3 Answers
5
One way is to use pathinfo()
:
$filename = "example.csv";
$dbname = pathinfo($filename, PATHINFO_FILENAME); // $dbname is now "example"

jszobody
- 28,495
- 6
- 61
- 72
-
-
-
-
Sidenote: That should look more like `$db_name = pathinfo($filename, PATHINFO_FILENAME); echo $db_name;` - OP might not know what to do with that ;-) or `echo $db_name = pathinfo($filename, PATHINFO_FILENAME);` - That would now assign it to the table name they want to create. – Funk Forty Niner Mar 18 '16 at 14:10
-
@Fred-ii- whew, you'd hope even a beginner would know how to take an `echo` statement and assign to a variable? ha, maybe not. updated the answer. – jszobody Mar 18 '16 at 14:12
-
2
$filename = "example.csv";
//Find the position of the last occurrence of "."
$point_pos = strrpos($filename,".");
//the second argument of substr is the length of the substring
$target = substr($filename, 0, $point_pos);

Giovanni S. Fois
- 79
- 4
1
Another way is to use basename()
function:
<?php
// your file
$file = 'example.csv';
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
echo $file_name; // outputs 'example'
?>

mitkosoft
- 5,262
- 1
- 13
- 31