(我有一天到PHP,所以請原諒,如果這是重複) 我有代碼上傳圖片到我的上傳/目錄在網站文件夾。這是通過下面給出的index.html和upload.php完成的,最初見於PHP - Upload picture and display on page。PHP和JS - 上傳圖片不顯示
但是,我不能根據需要在html頁面上顯示圖片。它重定向到upload.php,沒有圖像處理器。任何幫助如何做到這一點?我的index.html工作正常,所以我猜我的PHP設置應該沒問題。我跟着this,this和this來安裝和設置一切。
的index.html
<!DOCTYPE html>
<html>
<head>
<script language= "javascript" type="text/javascript">
function juploadstop(result){
console.log(typeof result);
if(result==0){
$(".imageholder").html("");
}
else if(result!=0){
$(".imageholder").html("<img src='"+result+"'>");
}
}
</script>
</head>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<div class="imageholder"> <!-- a gif image to show that the process wasn't finished -->
</div>
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
和upload.php的
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$result = $target_file;
$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) {
$uploadOk = 1;
} else {
$uploadOk = 0;
}
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") {
// echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
// echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
$result = $target_file;
} else {
$result = 0;
}
}
?>
<script language="javascript" type="text/javascript">
window.top.window.juploadstop(<?php echo $result; ?>);
</script>
編輯1我加入
header("Location: index.html");
到端我的php代碼在php部分完成之前,它不再重定向。但是,圖像仍然不顯示。
非常感謝您的幫助! 我想從_upload.php_ 和要上傳的文件調用juploadstop,因此目標文件名取決於用戶。我可以在處理中給它一個固定的名字嗎?如果是,如何? – Bhavna