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.
filename capture
Moderator: General Moderators
This is what i use to get the extension.. so u could reverse this and get the name
Altho
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);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 guesshi,
yopu can do this easily with basename()
this helps if you know the suffix
greez Pozor
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
?>greez Pozor
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?
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>
?>I like this method plus you can generate a array of file name endings and use it against the variable.Pozor wrote:hi,
yopu can do this easily with basename()
this helps if you know the suffixCode: Select all
<?php $filenamewithsuffix = basename($filename); //or full path to filename $filenamewithoutsuffix = basename($filename, '.jpg'); //if you know the suffix ?>
greez Pozor
- Think Pink
- Forum Contributor
- Posts: 106
- Joined: Mon Aug 02, 2004 3:29 pm
here is how I do it.
After the form is submitet (the upload button is pushed)
I get the filename like this
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.
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, '')));
?>