html将文件和文本数据提交到php [英] html submit file and text data to php

查看:133
本文介绍了html将文件和文本数据提交到php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我未成功尝试捕获文件和数据并上传到php文件.我不确定如果有人可以看到我在做什么错,请问我在做什么错.它将上传文件/图像没有问题,但是当我添加文本输入以发布它时将不起作用,这就是我一直用作模板进行试用的

I am unsucessfully trying to capture file and data and upload to a php file. I am not sure what i am doing wrong if someone can maybe see what i am doing wrong please. It will upload file/image no problem but when i add a text input to post it will not work, here is what i have been using as a template to trial

<iframe name="my_iframe" src="" id="my_iframe"></iframe>
<form action="http://www.********/upload11.php" method="post" enctype="multipart/form-data" target="my_iframe">
<input type="file"  input id="input" name="image" />
<input type="text" name="Description" value="Image of Time"/>
<div>
<input type="submit" value="Send">
</div>
</form>

upload11.php

upload11.php

<?php
$upload_image = $_FILES["image"][ "name" ];
$folder = "images/";
move_uploaded_file($_FILES["image"]["tmp_name"], "$folder".$_FILES["image"]["name"]);;
$file = 'images/'.$_FILES["image"]["name"];
$uploadimage = $folder.$_FILES["image"]["name"];
$newname = $_FILES["image"]["name"];

$resize_image = $folder.$newname; 
list( $width,$height ) = getimagesize( $uploadimage );
$newwidth = 550;
$newheight = 350;
$thumb = imagecreatetruecolor( $newwidth, $newheight );
$source = imagecreatefromjpeg( $resize_image );
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg( $thumb, $resize_image, 100 ); 
$out_image=addslashes(file_get_contents($resize_image));
$msg = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
$a = ('" alt="" />');
$b = ("'<img src=\"\https://www.******/images/".$_FILES['image']['name']."$a'");
$Description = $_POST['Description'];
$image = $_FILES['image']['tmp_name'];
$img = file_get_contents($image);
$con = mysqli_connect('***','******','******','********');
$sql = "INSERT INTO links (hyper_links, link ) VALUES ($b, $Description)";

$stmt = mysqli_prepare($con,$sql);

mysqli_stmt_bind_param($stmt, "s",$img);
mysqli_stmt_execute($stmt);

$check = mysqli_stmt_affected_rows($stmt);
if($check==1){
    $msg = 'Successfullly UPloaded';
}else{
    $msg = 'Could not upload';
}
mysqli_close($con);
}
?>
<?php
echo $msg;
?>

推荐答案

我已经重写了一半的脚本,以消除所有错误和不必要的变量.这应该使它工作.如果没有,它至少应该提供有关正在发生的事情的有用信息:

I've been rewriting half your script to get rid of all the errors and unnessesary variables. This should make it work. If not, it should at least give usefull information about what is going on:

<?php

if(!empty($_FILES["image"])){
    $imgName = $_FILES["image"]["name"];
    $imgTmpName = $_FILES['image']['tmp_name'];
    $upload_dir = "images/";

    if(move_uploaded_file($imgTmpName, $folder . basename($imgName))){
        $imgPath = $upload_dir . $imgName;

        list( $imgWidth, $imgHeight ) = getimagesize( $imgPath );

        $newImgWidth = 550;
        $newImgHeight = 350;

        $thumbnailImg = imagecreatetruecolor( $newImgWidth, $newImgHeight );
        $originalImg = imagecreatefromjpeg( $imgPath );
        imagecopyresized($thumbnailImg, $originalImg, 0, 0, 0, 0, $newImgWidth, $newImgHeight, $imgWidth, $imgHeight);

        imagejpeg( $thumbnailImg, $imgPath, 100 );

        $mysqli = new mysqli("localhost", "user", "password", "database");

        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL: " . $mysqli->connect_error;
        }

        if($stmt = $mysqli->prepare("INSERT INTO links (hyper_links, link ) VALUES (?, ?)")){
            if($stmt->bind_param("ss", $html, $descryption)){

                $html = "<img src=\"https://www.******/". $imgPath ."\">";
                $descryption = $_POST['Description'];

                if($stmt->execute()){
                    if($stmt->affected_rows > 0){
                        echo "Successfully Uploaded";

                        $stmt->close();
                        $mysqli->close();
                    } else {
                        echo "Could not upload";
                    }
                } else {
                    die("Execute() failed: " . htmlspecialchars($stmt->error));
                }
            } else {
                die("Bind_param() failed: " . htmlspecialchars($stmt->error));
            }
        } else {
            die("Prepare() failed: " . htmlspecialchars($stmt->error));
        }
    } else {
        die("Unable to move uploaded file to upload folder.");
    }
} else {
    die("You did not select a file to upload.");
}

?>

备份您现在拥有的内容,然后进行测试.让我知道进展如何.

Make a backup of what you have right now, and test this out. Let me know how it went.

这篇关于html将文件和文本数据提交到php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆