php - PHP图片上传出错
问题描述
因此,我正在尝试使用 PHP 脚本上传图像,但出现这些错误 ps:file_uploads = On in php.ini
文件是图像 - 图像/jpeg。警告:move_uploaded_file(image/promotions/verso.jpg):无法打开流:第 43 行的 W:\xampp\htdocs\elkssar\functions\upload.php 中没有此类文件或目录警告:move_uploaded_file():无法移动第 43 行 W:\xampp\htdocs\elkssar\functions\upload.php 中的“W:\xampp\tmp\phpCB68.tmp”到“image/promotions/verso.jpg” 抱歉,上传文件时出错。
<?php
$target_dir = "image/promotions/";
$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["upload"])) {
$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 ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
解决方案
这是一个愚蠢的错误,因为我与 image/ 不在同一个位置,所以代码行应该是这样的$target_dir = "../image/promotions/";
推荐阅读
- html - 视频元素中的首选来源是如何选择的?
- html - Firefox 是否支持任何真正的草书字体?
- google-app-engine - 使用 golang 日志库,在哪里可以看到 localhost 日志?
- google-chrome-extension - 从后台脚本强制关闭 chrome 扩展弹出窗口
- python - 在 Python 中将函数结果保存为连续的列表名称?
- java - Java中的多线程AES加密
- powerbi - Power BI - Azure SQL 数据库 v Amazon RDS 的网关
- swift - 如何在 SwiftUI 中使用 Text 显示元组值数组?
- json - Bash Loop thru Array 嵌套在带有 jq 的未知长度的 JSON 中
- r - 使用 R & paws 如何切换/承担不同的 IAM 角色?