Upload by FTP with PHP, not sending the image

Asked

Viewed 164 times

3

Based on the code below I am trying to send an image through the FTP protocol, but the file is not being sent, something tells me it is in the file path, I inform the absolute path.

<?php

$arquivo = $_FILES["fileToUpload"]["name"];
$servidor = 'servidor';
$target_dir = "destino";
$con_id = ftp_connect($servidor) or die( 'Não conectou em: '.$servidor );
ftp_login( $con_id, 'usuario', 'senha' );

    if ($arquivo != "" && $_FILES != NULL){
            $uploadOk = 1;
        //echo $image_name;die;
        $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
        // Check if file already exists
        if (file_exists($target_file)) {
            echo "Sorry, file already exists.";die;
            $uploadOk = 0;
        }
        // Check file size
        if ($_FILES["fileToUpload"]["size"] > 500000) {
            echo "Sorry, your file is too large.";die;
            $uploadOk = 0;
        }
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {
            echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";die;
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) {
            echo "Sorry, your file was not uploaded.";die;
        // if everything is ok, try to upload file
        } else {
            ftp_put( $con_id, $target_dir.$arquivo['name'], $arquivo['tmp_name'], FTP_BINARY );
        }
    }
  • What error are you getting? If you are not getting it is worth following the basic template that captures login errors, shipping, etc. PS:, lacked the ftp_close($con_id);

  • It’s weird because I did all the code and it doesn’t return anything, well, this ftp is my own documentation of my site, does it cause anything? Thus, I am uploading an ftp to my own application in which I run this process. I tried to do it the default php way but also did not give.

  • The problem with your code is that it is not getting the result of the functions ftp_login, ftp_put, etc. The code can fail silently at several points. We also do not have the parameters (file, ftp connection parameters, etc.) to simulate an execution. It is important that you find which command is failing. You have tried to echo each of the parameters as well as con_id and the returns of ftp_login and ftp_put? If any of them are FALSE you already know what happened. If you will not know where the code failed.

  • i used a Try{} and it step straight I’ll give an echo to see

  • well, the con_id returns Resource id #xx (x=numbers) ftp_login returns 1 (which I think q is true) and empty ftp_put.

  • Empty? This function should return one bool (documentation). Puts a echo before and after the ftp_put to make sure you got into it.

  • I found the error, the path of my file was returning totally wrong, I don’t know why, so I resolved and the update worked.

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.