Capturing file path using browse functionality.

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
TorMike
Forum Newbie
Posts: 17
Joined: Thu Feb 25, 2010 9:14 am

Capturing file path using browse functionality.

Post by TorMike »

All,

Here's my simple code that I want to use on a site to upload a file(s) to the server.

Code: Select all

if(isset($_GET['submit']))
{
  require('./include/pf_ftp_connect.php');
  require('./include/pf_mysqli_connect.php');
  $upload = $_GET['uploaded'];

  $remotefile = '/web/uploads/'.$upload;
  $localfile = $upload;
  

  $fp = fopen ($localfile, 'r');
  if (!$success = ftp_fput($conn, $remotefile, $fp,  FTP_BINARY))
  {
    echo 'Error: Could not upload file';
    ftp_quit($conn);
    exit;
  }
  fclose($fp);
  echo 'File upload successfully';
  
  // close connection to host
  
  ftp_quit($conn);
}
else
{
  ?>
  <form enctype="multipart/form-data" action="image_upload.php" method="GET">
    <br /><br>
    Please choose a file: 
    <input name="uploaded" type="file" />
    <br /><br />
    <input type="submit" name="submit" value="Upload" />
  </form>
  <?php
  echo 'goodbye<br/>';
}
?>
My problem is getting the complete path to the file. For example, if the user wants to copy a file located on a windows machine located at:

c:\pictures\mine\mypic.jpg

the submit will only return the file name, in this case mypic.jpg, and not the entire path. Without the full path the fopen will fail.

My question is: How do I include the entire path using the browse form? This there a better way to go about this?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Capturing file path using browse functionality.

Post by AbraCadaver »

This has nothing to do with the client machine. When they upload a file, PHP receives it and saves it in a temporary location on the server that you can get from the $_FILES super global array: http://www.php.net/manual/en/features.f ... method.php
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Capturing file path using browse functionality.

Post by requinix »

You cannot get the full path with regular HTML. You'd need Flash/Java/etc. for it.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Capturing file path using browse functionality.

Post by Weirdan »

HTTP does not support file uploads via GET method, you need to use POST for this (and if you do, you will have a file uploaded to your webserver, thus removing the need to know file paths on client computer).

See http://us2.php.net/manual/en/features.f ... method.php
Post Reply