【问题标题】:image upload to ftp with arrays php使用数组php将图像上传到ftp
【发布时间】:2013-06-11 18:20:45
【问题描述】:

我想尝试将多张图片上传到我的 FTP 上的一个文件夹。我已经尝试上传到我的数据库,这适用于我的代码。这是我第一次编写将文件上传到 FTP 的代码。

在我的 config.php 中:

$ftp_server = "**";
$ftp_username = "**";
$ftp_password= "**";
$connect = mysql_connect($ftp_server, $ftp_username, $ftp_password)
or die ("Hey, check your server connection.");

上传文件的代码:

include_once '..includes/config.php';
$files = array();
$fdata = $_FILES['image'];

if (is_array($fdata['name'])) {
    for($i=0;$i<count($fdata['name']);$i++) {
        $files[] = array(
            'name' => $fdata['name'][$i],
            'tmp_name' => $fdata['tmp_name'][$i]
        );
    }
} else $files[] = $fdata;

foreach($files as $file) {
    $conn_id = ftp_connect($ftp_server);
    $login_result = ftp_login($conn_id, $ftp_username, $ftp_password);

    ftp_put($conn_id, "public_html/img/" . $file['name'],$file['tmp_name']),FTP_BINARY);
    print_r($files['tmp_name']);
}

当我尝试打印我的$files['tmp_name'] 时,我得到一个空白页面,并且文件没有上传到我的 FTP。有人可以帮帮我吗?

谢谢。

【问题讨论】:

  • 你的 print_r 表达式的问题是变量名。你想要 print_r($file['tmp_name'])
  • 你不应该使用 mysql_* 函数切换到 mysqli 以获得更好的安全性和性能。
  • var_dump($files);foreach 之前为您提供了什么?
  • @jeroen array(1) { [0]=> array(2) { ["name"]=> string(15) "favicon - 5.png" ["tmp_name"]=>字符串(14) "/tmp/phpWfBuw9" } }
  • 如果您使用的文件名称不包含空格,它是否有效?

标签: php arrays file-upload ftp


【解决方案1】:

我认为您需要为文件分配添加索引,如下所示:

$files[$i] = array(
        'name' => $fdata['name'][$i],
        'tmp_name' => $fdata['tmp_name'][$i]
    );

然后你会得到你想要的任何索引的 tmp_name

$files[$index]["tmp_name"];

【讨论】:

    猜你喜欢
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    相关资源
    最近更新 更多