Hello

Lets say I have a text file, in a local or remote location. I want to select it with a file picker (standard Windows browser seleccion). Once I have that selected, I want to click a send button. This send will download the text file from whereever it is at then FTP it over to a server. After that, I want to call a web service.

Lets start from scratch. What is the best way to do the selecting the file from anywhere? From anywhere I mean a Windows shared computer but it should be able to get from another FTP location.

Thank you

Recommended Answers

The standard HTML file input control will let you select from the computer it's on, and attached network computers, but NOT from FTP. I haven't come across any file pickers yet that support this, probably because it cannot be done without server-side scripting. Javascript only supports HTTP and now WebSockets.

Jump to Post
if (isset($_POST['file']))
    print_r($_POST['file']);

It's an array.

Jump to Post

Check the PHP forum, there are enough threads about uploading files.

Jump to Post

jQuery still needs some scripting on the server...

Jump to Post

Yes, you should've gotten a PM about it.

Jump to Post

All 46 Replies

Cant seem to get your standard textbox and a button aside that says "Browse" working....

The standard HTML file input control will let you select from the computer it's on, and attached network computers, but NOT from FTP. I haven't come across any file pickers yet that support this, probably because it cannot be done without server-side scripting. Javascript only supports HTTP and now WebSockets.

OK got something semiworking......

(Itll do for now from the computer its on and attached network computers)

HTML:

<form id="form1input" action="page.php" method="post" enctype="multipart/form-data">
    <h3>File:</h3>
    <input id="file" type="file" name="file" /><br/>
    <input id="submit" type="submit" value="Upload File"/>
    </form>

PHP:

<?php



                $name=$_POST["file"];
                echo ($name);

                echo ($_POST["file"]);
                exit;

Says undefinded index file.....

For both lines

if (isset($_POST['file']))
    print_r($_POST['file']);

It's an array.

Same error.

print_r($_POST);

Prints out:

Array ( )

Check the PHP forum, there are enough threads about uploading files.

Check the PHP forum, there are enough threads about uploading files.

I perfer doing this with jQuery or something...

jQuery still needs some scripting on the server...

....did this thread move?

Yes, you should've gotten a PM about it.

Any way on how to do it?

Did you search this forum (PHP) ?

Did you search this forum (PHP) ?

Yes. Maybe the terms I used were not correct/valid so thats why it didnt show me any valid solution.

print_r($_FILES);

Sorry, my bad.

Sorry, my bad.

No problem. Now we are getting somewhere :)

<?php
                print_r($_FILES);
                exit;

This is the result:

Array ( [file] => Array ( [name] => filetest.txt [type] => text/plain [tmp_name] => C:\xampp\tmp\phpCB31.tmp [error] => 0 [size] => 267 ) )

OK, thats one part. Now how do I continue on to FTP that over?

Example code Ive thought up now:

$thefile=$_FILES['name'];
$conn_id = ftp_connect("127.0.0.1"); //example IP
$login_result = ftp_login($conn_id, "username","password"); 
ftp_pasv($conn_id, true); 
$fp = fopen(getcwd()."/".$thefile.".txt", "w");
/*HERE I THINK IT WOULD HAVE TO MAKE A COPY OF THE FILE WITH THE CONTENTS*/
fclose($fp);
$upload = ftp_put($conn_id, $thefile.".txt", getcwd()."/".$thefile.".txt", FTP_ASCII); 

This Ive thought up basing myself on some examples on the net. Anything wrong or different I should do???

You need to use move_uploaded_file first I think, to move the file from the temp directory to a permanent location (during your upload). If it is successful, then delete it.

Am not sure what kind of files you are ftp-ing, but I'd consider FTP_BINARY.

You need to use move_uploaded_file first I think, to move the file from the temp directory to a permanent location (during your upload). If it is successful, then delete it.

Doesnt ftp_put do this?

Am not sure what kind of files you are ftp-ing, but I'd consider FTP_BINARY.

Only text nonunicode files

<?php



                $name=$_FILES['file']['name'];




                $conn_id =  ftp_connect("127.0.0.1");

                $login_result = ftp_login($conn_id, "user","pass"); 

                ftp_pasv($conn_id, true); 

                $fp = fopen(getcwd()."/".$name.".txt", "w");

                /*I THINK I NEED SOMETHING HERE DIFFERENT THAN THE CODE IVE SEEN*/

                fclose($fp);

                $upload = ftp_put($conn_id, $name.".txt", getcwd()."/".$name.".txt", FTP_ASCII); 


                exit;

Hmmm cant quite get it...

The uploaded file is not in your getcwd() folder, but in sys_get_tmp_dir().

That's why I mentioned move_uploaded_file earlier, to move it from the temp folder to another location, prior to uploading.

$name=$_FILES['file']['name'];




                $conn_id =  ftp_connect("127.0.0.1");

                $login_result = ftp_login($conn_id, "user","pass"); 

                ftp_pasv($conn_id, true); 

                $fp = fopen(getcwd()."/".$name, "w");


                echo ("hi");
                fclose($fp);

                $upload = ftp_put($conn_id, $name, getcwd()."/".$name, FTP_ASCII); 


                exit;

This code "works" but the location where I FTP the file TO is empty. There is no text content.

Im not sure if move_uploaded_file has anything to do with it.

Like I said, the current work directory is NOT where uploaded files are stored (unless you changed your PHP.INI).

<?php



                $name=$_FILES['file']['name'];




                $conn_id =  ftp_connect("127.0.0.1");

                $login_result = ftp_login($conn_id, "user","pass"); 

                ftp_pasv($conn_id, true); 

                move_uploaded_file($name,getcwd()."/".$name);

                $fp = fopen(getcwd()."/".$name, "w");


                fclose($fp);

                $upload = ftp_put($conn_id, $name, getcwd()."/".$name, FTP_ASCII); 


                exit;

Is this what you ment or did I understand you incorrectly? (Doenst work)

<?php



                $name=$_FILES['file']['name'];


                print_r($_FILES);





                $conn_id =  ftp_connect("127.0.0.1");

                $login_result = ftp_login($conn_id, "user","pass"); 

                ftp_pasv($conn_id, true); 





                move_uploaded_file($_FILES["file"]["tmp_name"],getcwd()."/".$name);

                $fp = fopen(getcwd()."/".$name, "w");


                echo ("hi");
                fclose($fp);

                $upload = ftp_put($conn_id, $name, getcwd()."/".$name, FTP_ASCII); 


                exit;

Problably missing something obvious :(

I suggest you do some error checking to see what exactly fails. Is the uploaded file in your current work directory? Does ftp_connect return a resource, do ftp_login and ftp_put return true?

I suggest you do some error checking to see what exactly fails. Is the uploaded file in your current work directory? Does ftp_connect return a resource, do ftp_login and ftp_put return true?

Yup, Ill have to work if it returns true or false because there is no error message...

Ill do some if statements to print out...

if (move_uploaded_file($_FILES["file"]["tmp_name"],getcwd()."/".$name))
                    {
                        echo ("work");


                    }
                    else
                    {
                        echo ("doesnt work");
                    }

Changed to this and it prints out work but now it says:

Warning: ftp_put() [function.ftp-put]: Could not create file. in C:\testing\page.php on line 43

Hmm....FTP server is correct AFAIK.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.