2015-05-07 30 views
-1

我想製作一個簡單的圖片上傳器。爲什麼我不能上傳這個php代碼?

我有兩頁 '的index.html' 和 'upload.php的'

下面是index.html的

<html> 
<head> 

<script> 

$(document).ready(function(){ 
$("#im").click(function() { 
    $("input[id='my_file']").click(); 
}); 
$("#su").click(function() { 
    $("input[id='submit']").click(); 
});  
}); 
    </script>  
</head> 


    <body> 

    <form method="post" action="upload.php" > 
     <img id="im" src="pic.png" width="30" height="30" /> 
     <img id="su" src="sent.png" width="50" height="50" /> 
     <input type="file" id="my_file" style="display: none;" name="img" /> 
     <input type="submit" id="submit" style="display: none;" value="Upload Image" name="submit"/> 
    </form> 

下面的代碼爲upload.php的代碼:

<?php 

$target_path = "uploads/"; 
$target_path = $target_path . basename($_FILES['img']['name']); 

if(move_uploaded_file($_FILES['img']['tmp_name'], $target_path)) { 
    echo "The file ". basename($_FILES['img']['name']). 
    " has been uploaded"; 
} else{ 
    echo "fail : ". $_FILES['img']['error'] ; 
} 

?> 

此外,我創建了dir上傳,並檢查它是否可寫並且是。 我編輯php.ini並開啓上傳和提出的最大尺寸50米

當我上傳文件時,它dispay:「失敗:」

+0

' 「當我上傳文件時,它dispay: 」失敗:「 爲什麼?」' - 因爲你的最後一行的代碼明確顯示該文本... – David

+0

http://php.net/manual/en/function.print-r.php是你的朋友,例如,在'$ _FILES'上使用它,所以看看真正缺少的東西。 –

回答

5

當你有形式上傳文件,你需要添加enctype='multipart/form-data'屬性來形成。

<form method="post" action="upload.php" enctype="multipart/form-data"> 
+1

非常感謝,它的工作 –

0

嘗試使用此一:PHP代碼

<?php 
 
$target_dir = "uploads/"; 
 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
 
$uploadOk = 1; 
 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
 
// Check if image file is a actual image or fake image 
 
if(isset($_POST["submit"])) { 
 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
 
    if($check !== false) { 
 
     echo "File is an image - " . $check["mime"] . "."; 
 
     $uploadOk = 1; 
 
    } else { 
 
     echo "File is not an image."; 
 
     $uploadOk = 0; 
 
    } 
 
} 
 
?>

+0

問題在於HTML,而不是(主要)在PHP中。 – panther

相關問題