使用PHP将单个图像文件上传到FTP [英] Upload single image file to FTP using PHP

查看:115
本文介绍了使用PHP将单个图像文件上传到FTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里拉出我的头发。我想要做的只是使用HTML表单,然后使用PHP将所选文件上传到FTP服务器上的某个目录,但似乎没有任何问题。



这里是html表单:

 < form action =enctype =multipart / form-datamethod =post > 
< input name =filetype =file/>
< input name =submittype =submitvalue =Upload File/>
< / form>

以下是它下面的PHP(在同一个文件中):

 <?php 
$ ftp_server =myftp.co.uk;
$ ftp_user_name =myusername;
$ ftp_user_pass =mypass;
$ destination_file =/ public_html / my / directory /;
$ source_file = $ _POST ['file'] ['tmp_name'];

//设置基本连接
$ conn_id = ftp_connect($ ftp_server);
ftp_pasv($ conn_id,true);

//使用用户名和密码登录
$ login_result = ftp_login($ conn_id,$ ftp_user_name,$ ftp_user_pass);

检查连接
if((!!conn_id)||(!$ login_result)){
echoFTP连接失败!
echo尝试连接到用户$ ftp_user_name的$ ftp_server;
出口;
} else {
echo连接到$ ftp_server,用户$ ftp_user_name;
}

//上传文件
$ upload = ftp_put($ conn_id,$ destination_file,$ source_file,FTP_BINARY);

//检查上传状态
if(!$ upload){
echoFTP upload has failed!;
} else {
echo上传$ source_file到$ ftp_server作为$ destination_file;
}

//关闭FTP流
ftp_close($ conn_id);
?>

它似乎连接到FTP正常,但不上载 - 失败。我假设这是与我处理文件上传的方式有关..?



另外,当我在这里时,我该如何设置作为变量上传的文件的名称?

解决方案

在此处更改您的代码

  $ source_file = $ _FILES ['file'] ['tmp_name']; 

这里我已将$ _POST更改为$ _FILES ....


Pulling my hair out a bit here. All I want to do is have a HTML form and then PHP to upload the selected file to a certain directory on an FTP server, but nothing seems to be working correctly.

Here's the html form :

<form action="" enctype="multipart/form-data" method="post">
<input name="file" type="file" />
<input name="submit" type="submit" value="Upload File" />
</form>

And here's the PHP below it (on the same file):

<?php
$ftp_server = "myftp.co.uk";
$ftp_user_name = "myusername";
$ftp_user_pass = "mypass";
$destination_file = "/public_html/my/directory/";
$source_file = $_POST['file']['tmp_name'];

// set up basic connection
$conn_id = ftp_connect($ftp_server);
ftp_pasv($conn_id, true); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
    echo "FTP connection has failed!";
    echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
    exit; 
} else {
    echo "Connected to $ftp_server, for user $ftp_user_name";
}

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
echo "FTP upload has failed!";
} else {
echo "Uploaded $source_file to $ftp_server as $destination_file";
}

// close the FTP stream 
ftp_close($conn_id);
?>

It seems to connect to the FTP ok, but doesn't upload - failed. I'm assuming it's something to do with the way I'm handling the file to be uploaded..?

Also, while I'm here, how do I set the name of the file that's been uploaded as a variable?

解决方案

Change your code here

$source_file = $_FILES['file']['tmp_name']; 

Here i have changed $_POST to $_FILES....

这篇关于使用PHP将单个图像文件上传到FTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆