2016-09-15 36 views
0

我不想嘗試捕獲文件和數據並上傳到php文件。我不確定我做錯了什麼,如果有人可以看到我做錯了請。這將上傳文件/圖片沒有問題,但是當我添加一個文本輸入後它不會工作,這是我一直在使用什麼作爲模板來審判html提交文件和文本數據到php

<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

<?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; 
?> 
+0

請在上傳圖片時添加** error_reporting(E_ALL); **以檢查錯誤。 – Sunny

+0

我確實添加到我的php文件中,但是當我提交時,我只是得到了無法上傳 – INOH

+0

我建議你看看我在這裏回答這個問題:[Full Secure Image Upload Script](http://stackoverflow.com/questions/38509334/full-secure-image-upload-script/38712921#38712921)它會教你很多關於上傳腳本和安全性的知識,以及在最後給你一個完整的工作腳本。您可以輕鬆地將文本字段添加到表單中,並使用PHP進行處理。這應該不是問題。 – icecub

回答

1

我已經重寫一半的腳本來擺脫所有的錯誤和unnessesary變量。這應該使它工作。如果不是,它至少應該給什麼回事有用的信息:

<?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."); 
} 

?> 

使你有什麼權利現在備份,並測試了這一點。讓我知道它是如何去的。

+0

我試着運行你的代碼並收到錯誤'致命錯誤:調用未定義的方法mysqli_stmt :: affected_rows()在/ data/9/3/62/77/3714077/user/4129685/htdocs /第35行的upload01.php「 – INOH

+0

不確定這個問題,但我使用mysql而不是sqli – INOH

+1

@INOH我犯了一個小錯誤。現在應該修復這個錯誤。在你的問題上:沒關係。 – icecub

1

見你的代碼,你忘了添加「」在$ b $和說明

$sql = "INSERT INTO links (hyper_links, link) VALUES ($b, $Description)"; 

它應該是這樣的

$sql = "INSERT INTO links (hyper_links, link) VALUES ('$b', '$Description')"; 

希望這將工作:)

+1

它不會。他在準備查詢時根本不使用選擇器。他甚至試圖綁定根本沒有意義的參數。除此之外,整個腳本充滿了錯誤。就像在移動文件時不使用'basename()'一樣。''''在第4行等等處使用'basename()'等等。 – icecub

+1

Btw:準備查詢時不使用引號實際上是一件好事:P – icecub

+0

不!我不同意這個@icecub。當我在準備查詢時忘記添加引號時,我的php文件也無法正常工作。 – Sunny