0

So as it stands I have a form that hands a file to my server. What I am interested in doing is sending only the files full name to the server. Currently, my code is as follows:

<form action="upload.php" method="POST" >
    <table align="center">
        <tr>
            <td align="right">
                Select your file to upload:
            </td>
            <td>
                <input name="userfile" type="file" size="50">
            </td>
        </tr>
    </table>
    <table align="center">
        <tr>
            <td align="center">
                <input type="submit" name="submit" value="Upload image" />
            </td>
        </tr>
    </table>
</form>

When the form passes to upload.php, the value I get for $_POST['userfile'] is the 'name' of the file I am handing up. I am looking to get the full path out of this field, rather than just a filename. I wonder if anyone has any recommendations for a client-side file browser that can hand this back, or perhaps another way to evaluate this form to get that data, I would greatly appreciate it. I've tried adding 'enctype="multipart/form-data"' to the form definition, and what is actually really surprising is that I don't get any form info on my upload page - which is an entirely different problem. Anyway, thanks for any help.

Edit

Elaborate on my question... I'm trying to upload large files to my web server. HTTP Forms break after 100 megs or who knows. FTP is difficult to manage on 30 systems with 10 different users that have no idea how to determine why their client can't connect. I'm writing a web form to upload files to my server when these people are done with them. I am trying to find a solution to do this. The above script, is supposed to hand my full file path to the following script:

<?
set_time_limit(300);

var_dump($_FILES);
var_dump($_POST);
$filep=$_POST['userfile'];
$name=$_POST['userfile']['name'];

echo($filep);
echo($name);
$paths="test";
$ftp_server="localhost";
$ftp_user_name="digitaljedi";
$ftp_user_pass="dontworryaboutit";

// set up a connection to ftp server
$conn_id = ftp_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

// check connection and login result
if ((!$conn_id) || (!$login_result)) {
    echo "FTP connection has encountered an error!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name....";
    exit;
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name".".....";
}

// upload the file to the path specified
$upload = ftp_put($conn_id, $paths.'/'.$name, $filep, FTP_BINARY);

// check the upload status
if (!$upload) {
    echo "FTP upload has encountered an error!";
} else {
    echo "Uploaded file with name $name to $ftp_server ";
}

// close the FTP connection
ftp_close($conn_id);    

?>

If I add an encoding type to the form, I end up with NO $_POST data, and NO $_FORM data in upload.php, but rather, undefined indexes to 'userfile'. As it sits; If I upload say... 'C:\users\digitaljedi\wtf.txt' - I end up with 'wtf.txt' on the other end.

.....

I just tried adjusting my PHP to use a static path, rather than one from the form, and I end up not being able to open the file anyway. Apparently ftp_put uses the server filesystem, which completely negates my entire theory here.

Now what I want to know is this; is there NO solution for uploading large files through a web page? I found a flash uploader, but it seems to be capped at 100 megs ( documentation says that this is all flash allows ). This is becoming more trouble than it's worth.

hakre
  • 193,403
  • 52
  • 435
  • 836
DigitalJedi805
  • 1,486
  • 4
  • 16
  • 41
  • The full path at this point is the file name. What are you trying to accomplish? – Paul Dessert Feb 13 '12 at 19:58
  • http://stackoverflow.com/questions/81180/how-to-get-the-file-path-from-html-input-form-in-firefox-3 and http://stackoverflow.com/questions/1676070/input-type-file-ie-gives-full-path-ff-gives-only-filename-or-directory-bro – Zul Feb 13 '12 at 20:12
  • While I am trying to avoid the oncoming barrage of 'you shouldn't do it like that's - I suppose if you must know - I am trying to work around the limitations of HTTP File upload. To do so, I am trying to allow a user to hand a full file name to my php script, and then upload it via ftp in the same script. There are plenty of 'ftp' scripts floating around, but none of them seem to work flawlessly. I found someones example on google ( which was easily the most straight-forward ) that handed me ( almost exactly ) the above form, and a small chunk of php, but for whatever reason I'm not getting – DigitalJedi805 Feb 13 '12 at 20:12
  • my form data on the other side of the script. So I'm considering other solutions, while trying to figure out why this one isn't working. – DigitalJedi805 Feb 13 '12 at 20:13
  • The field does not post the full path of the file for security reasons, and there should be no reason for it to do that. You have no business looking at peoples' directory structures. – Matti Virkkunen Feb 13 '12 at 19:57
  • While I have nothing but good things to say about this site, this is quite the irritant - Question: 'Hey, I can't figure out how to do something' Answer: 'You don't ever need to do that ever because ever.' This is not particularly helpful. I do have a good reason to do this, and while I could elaborate I would only hear more reasons why what I am doing is just 'not the right way to do it' - so the question remains, is there a way to perform what I am trying to perform. – DigitalJedi805 Feb 13 '12 at 20:07
  • @DigitalJedi805: IMHO blindly answering a question without even thinking of the underlying problem is equally as bad. Maybe you should [go ahead and elaborate](http://catb.org/esr/faqs/smart-questions.html#goal) on why you need the full path of a file via web form. – Matti Virkkunen Feb 13 '12 at 20:09

2 Answers2

2

You're uploading a file:

Firstly, in your form, submit it with <form> as:

<form enctype="multipart/form-data" action="uploader.php" method="POST">

Secondly, use the $_FILES variable!

$_FILES['name_of_file_field']['name']

That gives you the original file name - change name_of_file_field to match the name of the file input field, you used userfile so change it to that.

If they are uploading files that are 100MB+, then just change the memory size limit to accept greater-sized files:

// Set the timeout to be longer to allow the file to upload, a value in seconds (3600 = 1 hour)
set_time_limit(3600);

// Set these amounts to whatever you need (1M = 1MB)
ini_set("post_max_size", "1000M");
ini_set("upload_max_filesize", "1000M");

// Generally speaking, the memory_limit should be higher than your post size.  So make sure that's right too.
ini_set("memory_limit", "1100M");
MrJ
  • 1,910
  • 1
  • 16
  • 29
  • Thank you MrJ, you've been the most helpful thus far, however I am still having an issue of my form not passing the proper data over to my upload.php file. My form is as follows:
    And my PHP throws the following error: Notice: Undefined index: userfile in D:\xampp\htdocs\ftp\upload.php on line 12 Notice: Undefined index: userfile in D:\xampp\htdocs\ftp\upload.php on line 13 - Both reference $_FILES['userfile']
    – DigitalJedi805 Feb 13 '12 at 21:03
  • Thank you @MrJ - I was having a problem with my upload form so this answer was helpful to me. Turns out I forgot to add enctype="multipart/form-data" to the form tag. I should add to your answer, that the best way of testing file uploads, is by printing the server upload variable, like so: print_r($_FILES); – Joel Murphy Apr 12 '12 at 18:12
  • Yes, `print_r($_FILES)` is always helpful - especially if you give the `input file` a different name and forget to change it within PHP! – MrJ Apr 13 '12 at 10:26
1

Caution: ini_set() CANNOT be used to set a new value for post_max_size or upload_max_filesize (among others).

Indeed, these variables have PHP_INI_PERDIR access rights and such variables CAN ONLY be modified within php.ini.

andyvn22
  • 14,696
  • 1
  • 52
  • 74
Mickski
  • 11
  • 1