PHP ftp File Upload

Source: Internet
Author: User
Tags chmod create directory file upload ftp file strlen

<?php

function copyviaftprecursively ($uploadLocation, $previewPath, $remoteDirectory, $ftpType)
{
$errorMessage = ';

$connectionId = GetFtpConnection ($uploadLocation [' Host '], $uploadLocation [' username '], $uploadLocation [' Password '] , $uploadLocation [' Port ']);
Switch ($ftpType)
{
Case ' active ':
FTP_PASV ($connectionId, False);
Break
Case ' passive ':
FTP_PASV ($connectionId, True);
Break
}

        $baseDirectory = $uploadLocation [' basedirectory '];
        if (substr ($baseDirectory, strlen ($baseDirectory)-1, 1)!=]
& nbsp;               {
                 $baseDirectory. = '/';
               }
        Ftp_mkdir ($connectionId, $baseDirectory);//No point showing an error Message if the directory exists (most likely cause of error) because it'll exist (at least) after the "the".

$remoteBaseDirectory = $baseDirectory. $remoteDirectory;
if (substr ($remoteBaseDirectory, strlen ($remoteBaseDirectory)-1, 1) = = '/')
{
$remoteBaseDirectory = substr ($remoteBaseDirectory, 0, strlen ($remoteBaseDirectory)-1);
}

$remoteBaseDirectory. = '/';
$errorMessage. = Copyfileviaftp ($previewPath, $remoteBaseDirectory, $connectionId);

Ftp_close ($CONNECTIONID);

        $errorHtml = ';
        if ($errorMessage)
                 {
                 $errorHtml = NL2BR ($errorMessage);
               }
        return $errorHtml;
       }

function GetFtpConnection ($host, $username, $password, $port)
{
$connectionId = Ftp_connect ($host);
if (! @ftp_login ($connectionId, $username, $password))
{
WebServiceError (' FTP error. Unable to connect to "'. $host. '," "with username" '. $username. ' ");
}
return $connectionId;
}


function Copyfileviaftp ($sourcePath, $destinationPath, $connectionId)
{
$errorMessage = ';
$sourcePath = Str_replace ("", "-", $sourcePath);
$destinationPath = Str_replace ("", "-", $destinationPath);
if (!ftp_mkdir ($connectionId, $destinationPath))
{
$errorMessage. = "Unable to create directory at". $destinationPath. " (it may already exist) ";
}
Ftp_site ($connectionId, ' CHMOD 0777 '. $destinationPath);
Ftp_chdir ($connectionId, $destinationPath);
Print $sourcePath. ' to '. $destinationPath. " <br/> ";
if (Is_dir ($sourcePath))
{
ChDir ($sourcePath);
$handle =opendir ('. ');
while (($file = Readdir ($handle))!==false)
{
if ($file!= ".") && ($file!= "..."))
{
if (Is_dir ($file))
{
$errorMessage. = Copyfileviaftp ($sourcePath. Directory_separator. $file, $file, $connectionId);
ChDir ($sourcePath);
if (!ftp_cdup ($connectionId))
{
$errorMessage. = "Unable to ftp_cdup";
}
}
Else
{
if (substr ($file, strlen ($file)-4, 4)!= ". zip")
{
$fp = fopen ($file, "R");
if (!ftp_fput ($connectionId, Str_replace ("", "_", $file), $fp, ftp_binary))
{
$errorMessage. = "Unable to Ftp_fput ().";
}
Ftp_site ($connectionId, ' CHMOD 0755 '. Str_replace ("", "_", $file));
}
}
}
}
Closedir ($handle);
}

return $errorMessage;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.