manix
manix

Reputation: 14747

Ftp relative path in php

I have a problem that I don't know how to solve. Let me explain it...

Directory structure:

-/public_html/lab/
-/public_html/lab/upload.php
-/public_html/lab/plugins/

I am really newbie with php+ftp, but I have the idea that FTP does not use "absolute root path" (/home/user/public_html/), instead it use "absolute user path" according to the user in question (/public_html), right?

Well, in the upload.php I have the code where the FTP class take a file temporally stored at the same folder where upload.php reside (/public_html/lab/) and then try yo move it to /public_html/lab/plugins/ folder.

FTP class receive a path where you want to move the file. But, I don't want to pass the path like absolute, I want to avoid this:

ftp->move('the-file.zip', '/public_html/lab/plugins/');

I would like to do something like this:

ftp->move('the-file.zip', '../plugins/');

But the code above looks like is not valid. So, how can I reproduce that result?

Upvotes: 0

Views: 1439

Answers (2)

Habid Pk
Habid Pk

Reputation: 218

You can use php relative path Like for moving file to your FTP location

$_SERVER['DOCUMENT_ROOT'] ."/lab/plugins/" ;

Upvotes: 3

zamnuts
zamnuts

Reputation: 9582

Rather than FTP which is designed for communication between two different hosts, consider moving the file via move_uploaded_file in upload.php:

move_uploaded_file($_FILES['uploaded']['tmp_name'],'./plugins/'.$_FILES['uploaded']['name']);

Once the file is uploaded via the PHP script, the temp file will be available in the _FILES superglobal under $_FILES[$inputFieldName]['tmp_name']. If you're using the example I gave, make sure to clean the "name" attribute before, since it will allow for client-side injection and leave a huge security hole in the application.

Upvotes: 1

Related Questions