0

I wrote the php code and the code to be loaded using curl, but every time I upload it to the server, it loads it with a .tmp extension and a different name. What is the reason for this?

PHP Code

     <?php
    if (isset($_POST['submit'])) {
        $dataFile = $_FILES['srcfile']['tmp_name'];
        $sftpServer    = 'server_ip';
        $sftpUsername  = 'root';
        $sftpPassword  = 'password';
        $sftpPort      = 22;
        $sftpRemoteDir = '/root';
    
    
        $ch = curl_init('sftp://' . $sftpServer . ':' . $sftpPort . $sftpRemoteDir . '/' . basename($dataFile));
    
        $fh = fopen($dataFile, 'r');
    
        if ($fh) {
            curl_setopt($ch, CURLOPT_USERPWD, $sftpUsername . ':' . $sftpPassword);
            curl_setopt($ch, CURLOPT_UPLOAD, true);
            curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_SFTP);
            curl_setopt($ch, CURLOPT_INFILE, $fh);
            curl_setopt($ch, CURLOPT_INFILESIZE, filesize($dataFile));
            curl_setopt($ch, CURLOPT_VERBOSE, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $dataFile);
    
    
            $verbose = fopen('php://temp', 'w+');
            curl_setopt($ch, CURLOPT_STDERR, $verbose);
    
            $response = curl_exec($ch);
            $error = curl_error($ch);
            curl_close($ch);
    
            if ($response) {
                echo "Success";
            } else {
                echo "Failure";
                rewind($verbose);
                $verboseLog = stream_get_contents($verbose);
                echo "Verbose information:\n" . $verboseLog . "\n";
            }
        }
    }
    ?>

HTML CODE

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</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>

The files I try to upload to my server are also uploaded with this figure .tmp extension and a random name. The php version I use is php8 xampp

enter image description here

  • Not sure though, but other examples are using `CURLOPT_URL` to specify target-host + filename, have you tried that? see https://www.php.net/manual/en/function.curl-setopt.php#121973 – leberknecht May 17 '21 at 13:14
  • unfortunately this thing you shared has not worked for me – Bünyamin Efe May 17 '21 at 13:59
  • You are saving them as temporary files...if want the actual names just use $_FILES['file']['name'] – Kevin Gales May 17 '21 at 17:46
  • When you do as you say, it gives me such an error message after updating to $ _FILES ['YOUR_INPUT_NAME'] ['name'] Warning: fopen(index.html): Failed to open stream: No such file or directory in C:\xampp\htdocs\ftp_upload.php on line 13 – Bünyamin Efe May 17 '21 at 20:05

1 Answers1

0

Files uploaded use a temporary randomized name for the file reference on the server machine so that uploaded files of the same name do not conflict with each other.

More info can be found at https://www.php.net/manual/en/features.file-upload.post-method.php

The original filename will be located at $_FILES['YOUR_INPUT_NAME']['name']

  • When you do as you say, it gives me such an error message after updating to $ _FILES ['YOUR_INPUT_NAME'] ['name'] Warning: fopen(index.html): Failed to open stream: No such file or directory in C:\xampp\htdocs\ftp_upload.php on line 13 – Bünyamin Efe May 17 '21 at 20:04
  • Did you actually replace `YOUR_INPUT_NAME` with the name of your input tag? – Sharon Fox May 17 '21 at 20:45
  • `$datafile = $_FILES['srcfile'] ['name'];`As I updated the code, it gave me the error I threw. – Bünyamin Efe May 17 '21 at 21:05
  • You don't open that file on the server. That file does not exist. That is the original filename. You open the temp file, and upload it as the original filename. – Sharon Fox May 17 '21 at 21:13
  • my aim is to upload files to the server via the web site with the post method. I can not do this. Is there a way for that? – Bünyamin Efe May 17 '21 at 21:24