php - 在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/
移动
我不知道为什么这是一个问题
谁能帮我解决这个问题
解决方案
您必须在 中使用本地路径名rename()
,而不是 URL。您可以使用它$_SERVER['DOCUMENT_ROOT']
来获取您网站的根目录。
rename($filename, $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/twentytwenty/templates/images/' . $filename);
推荐阅读
- android - 尽管我有一个 var (kotlin),但无法重新分配 Val?
- node.js - 如何在我的项目中从 npm 切换到 yarn?
- c - 字符串的多项式哈希与循环多项式移位
- css - 媒体查询未按预期运行
- java - 如何为 Undertow / JBoss 7.2 EAP 重写 RewriteValve?
- javascript - 如何正确处理来自多个 api 调用的承诺?
- mysql - Nodejs mysql事务回滚不起作用
- python - 返回已删除的 QDockWidget 标题栏以实现可拖动性
- java - 使用 Gradle 构建 EAR 文件:如果 webmodules 是 SNAPSHOT 依赖项,如何配置它们?
- javascript - 服务器发送的事件在某个服务器上不起作用