filename capture

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Chriss2004
Forum Newbie
Posts: 3
Joined: Thu Sep 09, 2004 7:04 pm

filename capture

Post 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.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post 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
Pozor
Forum Commoner
Posts: 74
Joined: Tue Mar 30, 2004 11:11 pm
Location: Switzerland

Post 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
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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>
?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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.
User avatar
Think Pink
Forum Contributor
Posts: 106
Joined: Mon Aug 02, 2004 3:29 pm

Post 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.
Post Reply