File characters speration

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
phpfan
Forum Newbie
Posts: 14
Joined: Thu Aug 27, 2009 4:37 am

File characters speration

Post by phpfan »

Hi,
My issue is i have to browse a file from the form and submit it, when i click submit only the file name and it's extension should be considered, i mean if i select a file, in the text file it is taking the entire file path example: C:\windows\temp\file.php
all i need to do is i have to submit only the " file.php ", i have gone thru the

Code: Select all

<?php
$ext = substr(strrchr($filename, '.'), 1);
?>
it will return only php

plz kindly give me the solution for this
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: File characters speration

Post by Mark Baker »

Have a look at PHPs pathinfo() function
phpfan
Forum Newbie
Posts: 14
Joined: Thu Aug 27, 2009 4:37 am

Re: File characters speration

Post by phpfan »

Code: Select all

<?php
include "connect.php";
 
if(isset($_POST['submit']))
   {
     $path = $_POST['file'];
     $file = basename($path);
     
     $handle = fopen("$file", "r");
 
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
     {
       $import="INSERT into excel(mobile) values('$data[0]')";
       mysql_query($import) or die(mysql_error());
     }
 
     fclose($handle);
 
     print "Import done";
   }
   else
   {
      print "<form action='import.php' method='post'enctype='multipart/form-data'>";
      print "Browse file to import:<br>";
      print "<input type='file' name='file'><br>";
      print "<input type='submit' name='submit' value='submit'></form>";
   }
?>
but im getting error, sorry for giving trouble, im bit confused, can you give me solution for where im wrong
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: File characters speration

Post by jackpf »

What's the error?

Also, can't you just use basename()?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: File characters speration

Post by Mark Baker »

Are you actually uploading this file from your form, in which case it's detailed in $_FILES rather than $_POST
Post Reply