Page 1 of 1

Capturing file path using browse functionality.

Posted: Thu May 06, 2010 11:29 am
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?

Re: Capturing file path using browse functionality.

Posted: Thu May 06, 2010 12:02 pm
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

Re: Capturing file path using browse functionality.

Posted: Thu May 06, 2010 12:18 pm
by requinix
You cannot get the full path with regular HTML. You'd need Flash/Java/etc. for it.

Re: Capturing file path using browse functionality.

Posted: Thu May 06, 2010 12:36 pm
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