这个问题在这里已有答案:
所以,我正在尝试将图像上传到我的数据库,所以我可以将它用作用户个人资料图片,但点击“上传”后,没有任何反应,这里是代码:
<?php
include ("conexao.php");
$_SESSION['usu_nome'] = "EDER";
$usuario = $_SESSION['usu_nome'];
echo "Welcome, ".$usuario."!<p>";
if (isset($_POST['submit'])) {
//get file attrivutes
$name=$_FILES['myfile']['name'];
$tmp_name=$_FILES['myfile']['tmp_name'];
if ($name)
{
$location = "avatars/$name";
move_uploaded_file($tmp_name, $location);
$query = mysqli_query("UPDATE esc_usuarios SET imagelocation='$location' WHERE usu_nome ='$usuario'");
die("Your avatar has benen uploaded");
}
else{
die ("Please select a file!");
}
}
echo "Upload your image:
<form action='upload.php' method='POST' enctype='multipart/form-data'>
File: <input type='file' name='myfile'> <input type='submit' value='Upload!'>
</form>
";
?>
发布于 2018-08-28 10:30:33
编辑并尝试
if((isset($_FILES['myfile']) || $_FILES['myfile']['error'] == UPLOAD_ERR_OK)) {
$name=$_FILES['myfile']['name'];
$tmp_name=$_FILES['myfile']['tmp_name'];
$location = "avatars/$name";
move_uploaded_file($tmp_name, $location);
$query = mysqli_query("UPDATE esc_usuarios SET imagelocation='$location' WHERE usu_nome ='$usuario'");
die("Your avatar has benen uploaded");
}
第二,这整个代码是upload.php
什么?或在任何其他页面?如此form action='upload.php'
设置,那就是为什么要问....
发布于 2018-08-28 11:16:34
首先尝试使用此代码,然后根据自己的方式进行调整。链接:https://www.w3schools.com/php/php_file_upload.asp
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(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;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
https://stackoverflow.com/questions/-100002447
复制相似问题