Home > other >  Bug in upload with ftp in php
Bug in upload with ftp in php

Time:10-30

I created an uploader that sends files to the host via ftp in php. My file is a photo. It does not just send the photo successfully to the host. My code:

<?php
    $name = $_FILES['cover']['name'];
    $type = $_FILES['cover']['type'];
    $size = $_FILES['cover']['size'];
    $tmp = $_FILES['cover']['tmp_name'];
    $image_properties = getimagesize($_FILES['cover']['tmp_name']);
    $ftpHost   = '*****';
    $ftpUsername = '*****';
    $ftpPassword = '*****';
    
    $connId = ftp_connect($ftpHost) or die("Couldn't connect to $ftpHost");
    
    $ftpLogin = ftp_login($connId, $ftpUsername, $ftpPassword);
    $filename=md5($name.microtime()).substr($name,-4,4);
    $namesql="************.com/7d97481b1fe66f4b51db90da7e794d9f/".$filename;
    
    if(ftp_put($connId, $tmp, $filename, FTP_ASCII)) {
        echo "File transfer successful - $name";
        echo "<img src='$namesql' alt='melipic' width='240px'>";
    }else{
        echo "There was an error while uploading $name";
    }
    
    ftp_close($connId);
    ?>
<form role="form" action="" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label style="float:right;" for="exampleInputEmail1">cover profile</label>
        <input name="cover" type="file" class="form-control" id="exampleInputEmail1">
    </div>
    <button name="btn" type="submit" class="btn btn-shadow btn-success">submit</button>
</form>

CodePudding user response:

As the manual says

ftp_put() stores a local file on the FTP server. So I think you need to save file from request to your file system before sending it to the server via ftp_put()

CodePudding user response:

You have the parameters of ftp_put in a wrong order. Additionally, as you are uploading binary files, you need to use FTP_BINARY mode (what is the default). You cannot force the text/ascii mode.

The code should be be:

ftp_put($connId, $filename, $tmp)
  • Related