简体   繁体   中英

Upload file from php to ftp server

I'm using a script to upload file via php script to my ftp server. The connection is ok but every time i try to upload files it says: Error uploading file. Please try again later.

so it seems that there is something wrong in the script.

Directory destination is mydomain/test/upload/ and the script is into: mydomain/test/index.php

Anyone of you can help me to understand?

// ftp settings
$ftp_hostname = 'xxxx'; // change this
$ftp_username = 'xxxx'; // change this
$ftp_password = 'xxxx'; // change this
$remote_dir = '/upload/'; // change this
$src_file = $_FILES['srcfile']['name'];


if ($src_file!='')
{
    // remote file path
    $dst_file = $remote_dir . $src_file;
    
    // connect ftp
    $ftpcon = ftp_connect($ftp_hostname) or die('Error connecting to ftp server...');
    
    // ftp login
    $ftplogin = ftp_login($ftpcon, $ftp_username, $ftp_password);
    
    // ftp upload
    if (ftp_put($ftpcon, $dst_file, $src_file, FTP_ASCII))
        echo 'File uploaded successfully to FTP server!';
    else
        echo 'Error uploading file! Please try again later.';
    
    // close ftp stream
    ftp_close($ftpcon);
}
else
    header('Location: index.php');

and the index file is:

<!DOCTYPE html>
<html>
<head>
    <title>Upload Files to FTP Server in PHP</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" >
    <link href="css/bootstrap.css" rel="stylesheet" type="text/css" />
</head>
<body>
<br/>
<div class="container">
    <div class="col-xs-8 col-xs-offset-2 well" style="background:none;">
    <form action="ftp_upload.php" method="post" enctype="multipart/form-data">
        <legend>Please Choose File to Upload</legend>
        <div class="form-group">
            <input type="file" name="srcfile" />
        </div>
        <div class="form-group">
            <input type="submit" name="submit" value="Upload File to FTP Server" class="btn btn-warning"/>
        </div>
    </form>
    </div>
</div>
</body>
</html>

I want to have uploaded file into the upload folder: mydomain/test/upload/

Either the source file is incorrect, or your FTP sits behind a firewall.

If it's behind a firewall, try this ftp-pasv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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