PHP 上传图片无效
PHP Upload an Image does not work
我有一个新问题。我想将网页中的图片上传到服务器上的文件夹中(我正在使用 XAMPP)。
我也尝试了很多例子,这两个来自 php.net 和 w3schools.com,但它不起作用,我不知道为什么。 -_-
这是我现在使用的代码:
$target_dir = "./uploads/images/";
$target_file = $target_dir . basename($_FILES["thumbnail"]["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["thumbnail"]["tmp_name"]);
if($check !== false) {
$text = $text . "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
$text = $text . "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
$text = $text . "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["thumbnail"]["size"] > 500000) {
$text = $text . "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$text = $text . "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$text = $text . "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["thumbnail"]["tmp_name"], $target_file)) {
$text = $text . "The file ". basename( $_FILES["thumbnail"]["name"]). " has been uploaded.";
} else {
$text = $text . "Sorry, there was an error uploading your file.";
}
}
这是来自 w3schools 的示例。
变量$text只是为了查看url中发生了什么。
该文件的输入字段称为:thumbnail
我想我没有从页面中获取图像,因为当我写这个时:
$text = $text . "Sorry, there was an error uploading your file: ". basename( $_FILES["thumbnail"]["name"]). " Text ";
我什么也不知道:文件:&文本
我希望有人能帮我解决我的问题。
=====================编辑=====================
这里是HTML内容:
<form id="postform" name="postform" action="?track=7" method="POST">
<label for="thumbnail" class="forinput">Thumbnail <span class="info">Das Bild wir auf 250px : 250px skaliert</span></label>
<input class="addMovieFormFile" id="thumbnail" type="file" name="thumbnail">
<input type="submit" value="FILM HINZUFÜGEN" class="button">
</form>
您缺少“enctype="multipart/form-data"。
请像这样在您的表单中使用它。
<form id="postform" name="postform" action="?track=7" method="POST" enctype="multipart/form-data">
如果不使用它,您将无法检索文件数据。
希望这对您有用。
我有一个新问题。我想将网页中的图片上传到服务器上的文件夹中(我正在使用 XAMPP)。
我也尝试了很多例子,这两个来自 php.net 和 w3schools.com,但它不起作用,我不知道为什么。 -_-
这是我现在使用的代码:
$target_dir = "./uploads/images/";
$target_file = $target_dir . basename($_FILES["thumbnail"]["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["thumbnail"]["tmp_name"]);
if($check !== false) {
$text = $text . "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
$text = $text . "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
$text = $text . "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["thumbnail"]["size"] > 500000) {
$text = $text . "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$text = $text . "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$text = $text . "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["thumbnail"]["tmp_name"], $target_file)) {
$text = $text . "The file ". basename( $_FILES["thumbnail"]["name"]). " has been uploaded.";
} else {
$text = $text . "Sorry, there was an error uploading your file.";
}
}
这是来自 w3schools 的示例。
变量$text只是为了查看url中发生了什么。 该文件的输入字段称为:thumbnail
我想我没有从页面中获取图像,因为当我写这个时:
$text = $text . "Sorry, there was an error uploading your file: ". basename( $_FILES["thumbnail"]["name"]). " Text ";
我什么也不知道:文件:&文本
我希望有人能帮我解决我的问题。
=====================编辑=====================
这里是HTML内容:
<form id="postform" name="postform" action="?track=7" method="POST">
<label for="thumbnail" class="forinput">Thumbnail <span class="info">Das Bild wir auf 250px : 250px skaliert</span></label>
<input class="addMovieFormFile" id="thumbnail" type="file" name="thumbnail">
<input type="submit" value="FILM HINZUFÜGEN" class="button">
</form>
您缺少“enctype="multipart/form-data"。
请像这样在您的表单中使用它。
<form id="postform" name="postform" action="?track=7" method="POST" enctype="multipart/form-data">
如果不使用它,您将无法检索文件数据。
希望这对您有用。