简体   繁体   English

PHP FTP上传BLOB TEMP图像

[英]PHP FTP Upload BLOB TEMP Image

I am trying to upload an Image of a Form via FTP. 我正在尝试通过FTP上传表单图像。 But because I validate my Form with Javascript I can't offer PHP the File Object. 但是,因为我使用Java验证表单,所以无法向PHP提供文件对象。 Instead I pass the temporary blob path of the image. 相反,我通过了图像的临时Blob路径。 When I try to upload, it doesn't work. 当我尝试上传时,它不起作用。 If I take then the blob path and put it manually in the brwoser line it displayes the image which means my blob path isn't corrupt. 如果我采用了Blob路径,然后将其手动放在brwoser行中,则会显示图像,这表示我的Blob路径未损坏。 Can't PHP / FTP take an blob temp image as the source file? PHP / FTP不能将Blob临时映像作为源文件吗? Here is my Code: 这是我的代码:

Javascript and HTML Javascript和HTML

 $(document).ready(function() { $('#testImageSelect').change( function(event) { var validation = false; var message = ""; validation = validateImage(); if(validation){ message = "Javascript: Das Bild ist ok!"; $("#submitTest").attr("disabled",false); }else{ message = "Javascript: Das Bild entspricht nicht den Anforderungen!"; $("#submitTest").attr("disabled",true); } document.getElementById("scriptresultJs").innerHTML = "<p>"+message+"</p>"; }); $("#submitTest").click(function () { var message = ""; var dataSubmit = []; var pic = document.getElementById("testImageSelect").files[0]; var pic_path = URL.createObjectURL(pic); var picture = []; picture = { tmp_path: pic_path, name: pic.name, size: pic.size } dataSubmit = { castingcity: "Coruscant", forename: "Anakin", lastname: "Skywalker", geschlecht: "Männlich" }; var result = "default"; result = $.ajax({ type: 'POST', async: false, // WICHTIG! url: 'http://hiddentalents.de/php/test.php', data: ({ data: dataSubmit, picture: picture }) }).responseText; message = result; document.getElementById("scriptresultPHP").innerHTML = "<p>"+message+"</p>"; }); }); function validateImage() { var validation = false; var pic = $("#testImageSelect").val().split('/').pop().split('\\\\').pop(); var ext = pic.substring(pic.lastIndexOf('.') + 1); if(ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG"){ validation = true; } else{ validation = false; } return validation; } 
 <!DOCTYPE HTML> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="screen"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <title>Servertesting</title> </head> <body> <form role="form" method="post" action="" id="testForm" enctype="multipart/form-data"> <input type="file" id="testImageSelect" name="testImageSelect" required> </form> <button class="btn btn-default" id="submitTest" disabled>Abschicken</button> <div id="scriptresultJs"></div> <p id="scriptresultPHP"></p> </body> </html> 

PHP 的PHP

<?php
$db_host = "rdbms.strato.de";
$db_datenbank = "(name of database)"; 
$db_username = "(username)"; 
$db_password = "(password)";

$output = "";

SESSION_START();

# Datenbankverbindung herstellen 
$datenbank = new mysqli($db_host, $db_username, $db_password, $db_datenbank);

# Hat die Verbindung geklappt ? 
if ($datenbank->connect_errno) {
    $output = $output . "\n" . "Fehler beim Verbinden mit der Datenbank: (" . $datenbank->connect_errno . ") " . $datenbank->connect_error;
}
//UTF 8 einstellen
mysqli_query($datenbank, "SET NAMES 'utf8'");

# Wurde überhaupt was eingetragen?
if(isset($_POST["data"])) {
    //Image überprüfung:
    $output = $output . "\n". $_POST["picture"]["tmp_path"];
    $target_dir = "temp/";
    $target_file_path = $target_dir . date('dmYHis_') . $_POST["picture"]["name"];
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file_path,PATHINFO_EXTENSION);

    // Check if file already exists
    if (file_exists($target_file_path)) {
        $output = $output . "\n" . "Es tut uns leid, das gewählte Bild existiert bereits.";
        $uploadOk = 0;
    }
    // Check file size
    if ($_POST["picture"]["size"] > 1500000) {
        $output = $output . "\n" . "Die Bilddatei ist leider zu groß.";
        $uploadOk = 0;
    }
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "jpeg" && $imageFileType != "JPG" && $imageFileType != "JPEG" ) {
        $output = $output . "\n" . "Leider sind nur JPG bzw. JPEG Dateien erlaubt. Sie haben eine " . $imageFileType . " Datei hochgeladen!";
        $uploadOk = 0;
    }
    // Check if $uploadOk is set to 0 by an error
    if ($uploadOk == 0) {
        $output = "Die Datei konnte leider nicht hochgeladen werden. Folgende Fehler sind verantwortlich:\n".$output;
    // if everything is ok, try to upload file
    } else {
        //Upload Image
        $ftp_server = "ftp.strato.de";
        $ftp_user_name = "(username)";
        $ftp_user_pass = "(password)";
        $destination_file = $target_file_path;
        $source_file = $_POST['picture']["temp_path"];

        // Verbindung aufbauen
        $conn_id = ftp_connect($ftp_server);

        // Login mit Benutzername und Passwort
        $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

        // Verbindung überprüfen
        if ((!$conn_id) || (!$login_result)) {
            $output = $output . "\n" . "FTP-Verbindung ist fehlgeschlagen!";
            $output = $output . "\n" . "Verbindungsaufbau zu $ftp_server mit Benutzername $ftp_user_name versucht.";
            exit;
        } else {
            $output = $output . "\n" . "Verbunden zu $ftp_server mit Benutzername $ftp_user_name";
        }

        // Datei hochladen
        $upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY);

        // Upload überprüfen
        if (!$upload) {
            $output = $output . "\n" . "FTP-Upload ist fehlgeschlagen!\nDie Datei $source_file konnte nicht auf dem Server $ftp_server als $destination_file hochgeladen werden!";
        } else {
            $output = $output . "\n" . "Datei $source_file auf Server $ftp_server als $destination_file hochgeladen";
        }

        // Verbindung schließen
        ftp_close($conn_id);

        //Datenbankeinträge machen:
        $castingcity = mysqli_real_escape_string($datenbank,$_POST["data"]["castingcity"]);
        $forename = mysqli_real_escape_string($datenbank,$_POST["data"]["forename"]);
        $lastname = mysqli_real_escape_string($datenbank,$_POST["data"]["lastname"]);
        $geschlecht = mysqli_real_escape_string($datenbank,$_POST["data"]["geschlecht"]);
        $picture = $target_file_path;
        //Insert Data (except Image)
        $sql = "INSERT INTO candidates_temp (castingcity, forename, lastname, geschlecht, picture)
        VALUES ('$castingcity', '$forename', '$lastname', '$geschlecht', '$picture')";
        if ($datenbank->query($sql) === TRUE) {
            $output = $output . "\n" . "Datenbank Werte eingetragen!";
        } else {
            $output = $output . "\n" . "Error: " . $sql . "<br>" . $datenbank->error;
        }
    }
}else {
    $output = $output . "\n" . "POST Variable leer!";
}

$datenbank->close();

echo $output;

?> ?>

Finally solved 终于解决了

If ound this Tutorial , and it works!!! 如果听起来这个教程 ,它的工作原理!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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