在php中移动上传的文件的问题

我正在制作一个图像压缩工具来压缩图像大小


为此,我制作了下面给出的代码


<?php

$name  = '';

$type  = '';

$size  = '';

$error = '';

function compress_image($source_url, $destination_url, $quality)

{

    $info = getimagesize($source_url);

    if ($info['mime'] == 'image/jpeg') {

        $image = imagecreatefromjpeg($source_url);

        imagejpeg($image, $destination_url, $quality);

    } elseif ($info['mime'] == 'image/gif') {

        $image = imagecreatefromgif($source_url);

        imagegif($image, $destination_url, $quality);

    } elseif ($info['mime'] == 'image/png') {

        $image = imagecreatefrompng($source_url);

        imagepng($image, $destination_url, 5);

    }

    return $destination_url;

}

if ($_FILES["file"]["error"] > 0) {

    $error = $_FILES["file"]["error"];

} else if (($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/pjpeg")) {

    $url       = $_FILES["file"]["name"];

    $temp_file = $_FILES["file"]["tmp_name"];

    $filename  = compress_image($temp_file, $url, 80);

    rename($filename, 'https://example.com/wp-content/themes/twentytwenty/templates/images/' . $filename);

    $location = "https://example.com/wp-content/themes/twentytwenty/templates/images/" . $url;

    /*$image_size = getimagesize($location);*/

    echo 'https://example.com/wp-content/themes/twentytwenty/templates/images/' . $filename; die();

} else {

    $error = "Uploaded image should be jpg or gif or png";

}

但是我的文件问题不能在它创建的图像文件夹中https://example.com/wp-content/themes/twentytwenty/templates/移动


我不知道为什么这是一个问题


谁能帮我解决这个问题


小怪兽爱吃肉
浏览 86回答 1
1回答

UYOU

您必须在 中使用本地路径名rename(),而不是 URL。您可以使用它$_SERVER['DOCUMENT_ROOT']来获取您网站的根目录。rename($filename,&nbsp;$_SERVER['DOCUMENT_ROOT']&nbsp;.&nbsp;'/wp-content/themes/twentytwenty/templates/images/'&nbsp;.&nbsp;$filename);
打开App,查看更多内容
随时随地看视频慕课网APP