Page 1 of 1

File characters speration

Posted: Thu Sep 17, 2009 7:19 am
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

Re: File characters speration

Posted: Thu Sep 17, 2009 7:35 am
by Mark Baker
Have a look at PHPs pathinfo() function

Re: File characters speration

Posted: Thu Sep 17, 2009 8:00 am
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

Re: File characters speration

Posted: Thu Sep 17, 2009 9:52 am
by jackpf
What's the error?

Also, can't you just use basename()?

Re: File characters speration

Posted: Thu Sep 17, 2009 10:21 am
by Mark Baker
Are you actually uploading this file from your form, in which case it's detailed in $_FILES rather than $_POST