upload or ftp post?

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
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

upload or ftp post?

Post by andrei.mita »

I tried a basic upload script, much like the one found on the php.net website, but I discovered that I don't have permission to make any upload thru the page. As I have FTP access, I thought I could trik the server and upload files thru FTP from the page. What do you think, could it work? What are the diferences between an upload with move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) and one thru ftp_fput()? Is it less secure, slower, or why people don't use it that much?

Thanks,
Andrei
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Well, using $_FILES and those functions (move_uploaded_file() to name one) makes it possible to visit a remote website and upload files using for example a form.

But by using the ftp functions you need the files stored on another location, thus sending the files from A to B (ie. locally to remote host).

Basicly completely different things.
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

Post by andrei.mita »

sorry but i don't get it.

lets make it simple stupid (for me).

i want to make a form that will alow people to upload pictures. why ftp can't work and i have to use $_FILES ?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

You create a form, that is supposed to send file "foo.txt" to the server. The user selects that file using an input type="file" form element. The file is then sendt to a temporary storage on the server when the user press the submit button. (At this point it can be retrieved by using the $_FILES superglobals.)

The file is now stored in a "temporary" space on the server (hence the $_FILES['userfile']['tmp_name'] superglobal). I might be way off, but I can't see the possability on how to use ftp functions with this "non-existing" file.

FTP are mainly used to ship files/data from point A to B, but in this case the file/data is allready somewhat stored on place B, hence you shouldn't be able to send it again. The form on the webpage itself has allready taken care of this part....

You could however send the files from your local server to the host server using ftp... But thats truly a real A -> B transfer.
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

Post by andrei.mita »

FTP are mainly used to ship files/data from point A to B, but in this case the file/data is allready somewhat stored on place B, hence you shouldn't be able to send it again. The form on the webpage itself has allready taken care of this part....
now i understand what you said and that's exactly why i asked in the first place. i don't have permision to upload files, better yet to use the move_uploaded_file function but i have ftp permision. I thought I could trick the server by using instead of move_uploaded_files the ftp_put one and it worked. so here is the code:

Code: Select all

<?php

$server = "server";
$user = "user";
$pass = "pasword";
$nume_fisier = $_FILES['userfile']['name'];    
$sursa = $_FILES['userfile']['tmp_name'];
$destinatie  = "/pics/".$nume;


$connect = ftp_connect($ftp_server);
$login = ftp_login($connect, $user, $pass);

if ((!$connect) || (!$login)) 
	{
       echo "Not working";       
       die();
   } 

$upload = ftp_put($connect, $destinatie, $sursa, FTP_BINARY);

if (!$upload) 
   {
       echo "Conmected but not uploaded!";
   } 

ftp_close($connect);
?>
don't you find this odd?
what do you thnik?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Yah, I thought more on the issue after I posted, and all the relevant info is there to be used (the binary etc.) so it should work afterall, but I'm to late to edit my post... :roll:

I personally would not use ftp as a workaround for this, rather than use the functions that is designed for this kind of work. I'd verify that the host has everything setup correctly, trying to debug the script(s), anything to get the fileuploads working as they should.

If the host intentionally disabled file uploads somehow, it would be pretty odd that they'd still used the --enable-ftp during php installation...
andrei.mita
Forum Commoner
Posts: 65
Joined: Sun May 08, 2005 4:06 am
Location: Barlad/Romania

Post by andrei.mita »

yeah I know, it's very odd. I'll check again with the admin and see what he has to say about this. interesting discovery thou.....
thanks for the help.
hongco
Forum Contributor
Posts: 186
Joined: Sun Feb 20, 2005 2:49 pm

Post by hongco »

the only case I would use ftp along with upload is when I need to chmod the folder in which the file will be written to. When the uploading is done, change mode of the folder not to be written again. I don't like to have the idea of keep the folder with 777 at all time. Of course with perl, you don't need to do this.
Post Reply