开发者

Uploading file via web-based ftp using PHP

开发者 https://www.devze.com 2023-03-08 02:38 出处:网络
Once again I need some much appreciated help from the experts :) I am trying to use PHP\'s FTP functions to upload a file to the server via a web form, I\'ve spent the day Googling for a simple tutori

Once again I need some much appreciated help from the experts :) I am trying to use PHP's FTP functions to upload a file to the server via a web form, I've spent the day Googling for a simple tutorial but most seem to expect a certain level of understanding about the process. So, I've found an example that I believe will do the job for me, but I frankly don't understand how to concoct the ftp path?

Here's the script in it's entirety (I found it on Tech Republic)

<?
// get FTP accessparameters
$host = "<URL of site>";
$user = "user@<URL of site>";
$pass = "<PASSWORD>";
$destDir= "www/uploads/files/";
$workDir= "/usr/local/temp"; // define this as perlocal system
// get temporary file namefor the uploaded file
$tmpName= basename($_FILES['ufile']['tmp_name']);
// copy uploaded file intocurrent directory
move_uploaded_file($_FILES['ufile']['tmp_name'], $workDir."/".$tmpName) or die("Cannot move uploaded file toworking directory");
// open connection
$conn= ftp_connect($host) or die ("Cannot initiate connection tohost");
// send access parameters
ftp_login($conn,$user, $pass) or die("Cannot login");
// perform file upload
$upload = ftp_put($conn, $destDir."/".$_FILES['ufile']['name'], $workDir."/".$tmpName, FTP_BINARY);
// check upload status
// display message
if (!$upload) {
    echo "Cannotupload";
} else {
    echo "Uploadcomplete";
}
// close the FTP stream
ftp_close($conn);
// delete local copy ofuploaded file
unlink($workDir."/".$tmpName) or die("Cannot delete uploaded filefrom wo开发者_高级运维rking directory -- manual deletion recommended");
?>

At the moment it's failing with "Cannot move uploaded file to working directory" - so I am assuming that the path is incorrect. When this particular user connects via ftp, their home directory is "files".

I appreciate the advice.

Answer below

I have it working. Because the users home directory is "files" it required that inside this directory there be two further directories called "file" and "temp" I altered the code above to:-

$destDir= "files";
$workDir= "temp";

and found an example of an ftp connection string to work backwards from so that the code generated the correct format. Easy when you know how!


I have it working. Because the users home directory is "files" it required that inside this directory there be two further directories called "file" and "temp" I altered the code above to:-

$destDir= "files";
$workDir= "temp";

and found an example of an ftp connection string to work backwards from so that the code generated the correct format. Easy when you know how!

0

精彩评论

暂无评论...
验证码 换一张
取 消