Page 1 of 1
filename capture
Posted: Wed Sep 15, 2004 7:40 am
by Chriss2004
hello, could anyone tell me pls how could i get a name of a file and add it to $db?
What I mean is : I have an upload section, and I want to automatically add the name of the picture I want to upload (without extension) in the $db.
Thx.
Posted: Wed Sep 15, 2004 8:30 am
by ol4pr0
This is what i use to get the extension.. so u could reverse this and get the name
Code: Select all
function getextension($filename)
{
$filename = strtolower($filename);
$extension = split("[/\\.]", $filename);
$n = count($extension)-1;
$extension = $extension[$n];
return $extension;
}
$file_type = getextension($file_name);
Altho
Code: Select all
if (isset($_FILES['input_form_field']['name'])) $file_name = $_FILES['input_form_field']['name'];
INSERT INTO <table> (filename) VALUES ($file_name); // should work i guess
Posted: Wed Sep 15, 2004 8:37 am
by Pozor
hi,
yopu can do this easily with basename()
Code: Select all
<?php
$filenamewithsuffix = basename($filename); //or full path to filename
$filenamewithoutsuffix = basename($filename, '.jpg'); //if you know the suffix
?>
this helps if you know the suffix
greez Pozor
Posted: Wed Sep 15, 2004 7:22 pm
by dethron
if you just need to insert the name of the file, you may use the following.
do you want to insert the file to db,too?
Code: Select all
<?php
if($_FILES['myfile']){
$data = $_FILES['myfile'];
$named = substr($data['name'],0,-4);
echo $named;
// now you can add it to db if you want.
}
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="myfile" >
<input type="submit">
</form>
?>
Posted: Wed Sep 15, 2004 7:24 pm
by tim
Pozor wrote:hi,
yopu can do this easily with basename()
Code: Select all
<?php
$filenamewithsuffix = basename($filename); //or full path to filename
$filenamewithoutsuffix = basename($filename, '.jpg'); //if you know the suffix
?>
this helps if you know the suffix
greez Pozor
I like this method plus you can generate a array of file name endings and use it against the variable.
Posted: Fri Sep 17, 2004 6:39 am
by Think Pink
here is how I do it.
After the form is submitet (the upload button is pushed)
I get the filename like this
Code: Select all
<?php
$file=$_FILES['file']['name'];
$path = "/folder_wehere_pictures_goes/$file";
$filename = current(explode('.', basename($path, '')));
?>
then I first check to see if the pics is in the $tb. If it is, a message is returned if not, the filename is inserted in the $db and the upload process continiues with adding the immage to the folder.