我有一个单独的图库和文件部分。两者都可以在桌面上正常工作但不能移动。
图库代码 - 每次只链接到无效图像页面。即使图像应该可以上传。
//When upload button is pressed
if (isset($_FILES['image']['tmp_name'])) {
$image = date(U).$_FILES['image']['name'];
$target = "gallery_images/".$username."/".$image;
$sql = "INSERT INTO `images` (username, image) VALUES ('$username', '$image')";
move_uploaded_file($_FILES['image']['tmp_name'], $target);
mysqli_query($link, $sql);
//checking to see if uploaded file is actually an image
$file_type = image_type_to_mime_type(exif_imagetype($target));
$allowed = array("image/jpeg", "image/jpg", "image/gif", "image/png", "image/svg+xml", "image/bmp", "video/x-flv". "video/mp4", "application/x-mpegURL", "video/MP2T", "video/3gpp", "video/quicktime", "video/x-msvideo", "video/x-ms-wmv");
if(in_array($file_type, $allowed)){
header("location:gallery.php");
}else{
header("location:invalid_file.php?img=$image");
}
}
文件代码 - 将文件名上传到db但实际上并未将文件放在指定的文件夹中
//When uploadfile is pressed - file is stored in db and folder
if (isset($_FILES['file']['tmp_name'])) {
if($_FILES['file']['name'] == $row["file"]){
header("location:file_copy.php");
}else{
$file = $_FILES['file']['name'];
$target = "file_storage/".$username."/".$file;
$sql = "INSERT INTO `files` (username, file) VALUES ('$username', '$file')";
move_uploaded_file($_FILES['file']['tmp_name'], $target);
mysqli_query($link, $sql);
header("location:files.php");
}
答案 0 :(得分:1)
将此正确的代码与您的代码一起使用。
<?php
$connection=mysqli_connect("localhost","root","","imgup");
if(isset($_POST['create_post'])){
$post_image = $_FILES['image']['name'];
$post_image_temp = $_FILES['image']['tmp_name'];
move_uploaded_file($post_image_temp, "images/$post_image");
$query = "INSERT INTO img(post_image) ";
$query .= "VALUES('{$post_image}')";
$create_post_query = mysqli_query($connection, $query);
}
?>
<form action="fileup.php" method="post" enctype="multipart/form-data">
<label for="post_image">Post Image</label>
<input type="file" name="image">
<input type="submit" name="create_post" value="Publish">
</form>