简体   繁体   English

使用 PHP 通过基于 Web 的 ftp 上传文件

[英]Uploading file via web-based ftp using PHP

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.我再次需要专家的一些非常感谢的帮助:) 我正在尝试使用 PHP 的 FTP 函数通过 web 表单将文件上传到服务器,我花了一天时间谷歌搜索一个简单的教程,但大多数人似乎期望对过程有一定程度的了解。 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?所以,我找到了一个我相信会为我完成这项工作的例子,但坦率地说,我不明白如何编造 ftp 路径?

Here's the script in it's entirety (I found it on Tech Republic)这是完整的脚本(我在 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 working 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".当这个特定用户通过 ftp 连接时,他们的主目录是“文件”。

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:-因为用户的主目录是“files”,所以它要求在这个目录中还有两个名为“file”和“temp”的目录,我将上面的代码更改为:-

$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.并找到了一个 ftp 连接字符串的示例,可以向后工作,以便代码生成正确的格式。 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:-因为用户的主目录是“files”,所以它要求在这个目录中还有两个名为“file”和“temp”的目录,我将上面的代码更改为:-

$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.并找到了一个 ftp 连接字符串的示例,可以向后工作,以便代码生成正确的格式。 Easy when you know how!当你知道怎么做时很容易!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM