创建新的目标文件夹为空



对PHP图像函数完全感到困惑。无法获取新映像。

$file = $_FILES['inpfile'];  // input
$tempname = $file['tmp_name'];
$info = getimagesize($tempname);
$mime = $info['mime'];
if ($mime == 'image/jpg') {
    $newimg = imagecreatefromjpeg($tempname);
}
elseif ($mime == 'image/png') {
    $newimg = imagecreatefrompng($tempname);
}
elseif ($mime == 'image/gif') {
    $newimg = imagecreatefromgif($tempname);
}
imagejpeg($newimg, 'test.jpg', 70);
$uniqname = uniqid() . '.jpg';
$targ = 'test/' . $uniqname;
move_uploaded_file('test.jpg', $targ);

我期待文件夹中test一个新图像(test.jpg(,但它是空的。

没有错误,假设这里缺少一些东西。

任何帮助。

:)试试这段代码,我做了一个简单的系统,可以上传图像并将图像存储在1个目录名称上传中,您也可以在图库中查看图像

<?php
require('db.php');
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if (isset($_POST['submit']))
{
    $name = $_POST['Name'];
    $Descrp = $_POST['Descrp'];
    if (empty($name)) {
        echo "<script>alert('Name was empty');
                                window.location='index.php';
                            </script>";
    }
    else
    {
        // Allow certain file formats
        if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
        && $imageFileType != "gif" ) {
            echo "<script>alert('Sorry, only JPG, JPEG, PNG & GIF files are allowed.');
                                window.location='index.php';
                            </script>";
            $uploadOk = 0;
        }
        // Check if $uploadOk is set to 0 by an error
        if ($uploadOk == 0) 
        {
            echo "<script>alert('Sorry, your file was not uploaded.');
                                window.location='index.php';
                            </script>";
        // if everything is ok, try to upload file
        } 
        else 
        {
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
                $imgName = $_FILES["fileToUpload"]["name"];
                mysqli_query($con,"INSERT INTO users (`id`, `name`, `img`,`descrp`) VALUES (NULL, '$name','uploads/$imgName','$Descrp')");
                echo "<script>alert('Successfully Added');
                                window.location='gallery.php';
                            </script>";
            } 
            else 
            {
                echo "<script>alert('Sorry, there was an error uploading your file.');
                                window.location='index.php';
                            </script>";
            }
        }
    }

}
?>

Github存储库:使用图像库将图像上传到上传目录

最新更新