php - 使用php将图像上传到文件夹
问题描述
我有一个带有 php 的图像上传脚本,它可以在 localhost 服务器中工作,但是当我将网站上传到我的域服务器时,它会上传图像,但不会在 spathfic 文件夹中我设置得很好,将文件夹路径设置为图像名称的一部分,例如
路径是
admin\upload\novelimage
图像名称将是这样的
upload\novelimage\...image that uploaded
如果是脚本或服务器中的错误,我不知道,但我没有看到他们一方需要上传权限,所以我想问题出在脚本上,这里是脚本
//upload files
$avatarname = $_FILES['NovelImage']['name'];
$avatarsize = $_FILES['NovelImage']['size'];
$avatartmp = $_FILES['NovelImage']['tmp_name'];
$avatartype = $_FILES['NovelImage']['type'];
$avatarAllowedextention = array("jpeg", "jpg", "png", "gif");
//get avatar extention
$avatarextention = strtolower(end(explode('.', $avatarname)));
#get the varibles from the form
$Novelname = $_POST['Novelname'];
$Describe = $_POST['Describe'];
$category = $_POST['langOpt3'];
// condtion ? true and false
//check username
$formerrors = array();
if (! empty($avatarname) && ! in_array($avatarextention, $avatarAllowedextention)) {
$formerrors[] = 'avatar extension is not allowed';
}
if (empty($avatarname)) {
$formerrors[] = 'empty avatar is not allowed';
}
if ($avatarsize > 4194304) {
$formerrors[] = 'avatar size is bigger than 4mb';
}
foreach ($formerrors as $error) {
echo '<div class="alert alert-danger">' . $error . '</div>';
}
if (empty($formerrors)) {
$avatar = rand(0, 1000000) . '_' . $avatarname;
move_uploaded_file($avatartmp, "upload\novelimage\\" . $avatar);
$check = checkitem("NovelName", "novel", $Novelname);
if ($check == 1) {
$theMsg = "<div class='alert alert-danger'>sorry this user name is exit</div>";
redirecthome($theMsg, 'back');
}else{
//insert userinfo into database whith this info
$stmt = $con->prepare("INSERT INTO novel(NovelName, NovelImage, description)
VALUES(:znovel, :zNovelImage, :zdescription)");
$stmt->execute(array(
'znovel' => $Novelname,
'zNovelImage' => $avatar,
'zdescription' => $Describe
));
$lastid = $con->LastInsertId();
$stmt = $con->prepare("INSERT INTO book_category(novelid, categoryID)
VALUES(:znovelid, :zcategoryID)");
foreach ($category as $cat) {
$stmt->execute(array(
'znovelid' => $lastid,
'zcategoryID' => $cat
));
}
}
//erg
$theMsg = "<div class='alert alert-success '>" . $stmt->rowCount(). 'Record Updated</div>';
redirecthome($theMsg, 'back', 100);
}
我真的在这里看不到任何问题,请帮忙
解决方案
你的问题不是很清楚,但要确认这个“你的图像没有上传到你想要在服务器中的特定路径”从你的路径代码中,我假设你的本地主机在 Windows 操作系统中运行,从这个问题我认为你的服务器是 Linux 服务器。因此,Linux 的正斜杠无法识别 Windows BackSlash('')。
if (empty($formerrors)) {
$avatar = rand(0, 1000000) . '_' . $avatarname;
move_uploaded_file($avatartmp, "upload\novelimage\\" . $avatar);
$check = checkitem("NovelName", "novel", $Novelname);
这将是:
move_uploaded_file($avatartmp, "upload/novelimage/\" . $avatar);
您可以参考与 Linux 服务器中的 Windows 路径问题相关的 StackOverflow 链接: Help with windows path - PHP
我希望这对你有帮助,但你的问题对我来说不是很清楚。
推荐阅读
- dialogflow-es - Dialogflow 检测意图并返回多个意图名称和置信度值
- python-3.x - 保留字典中不在列表中的值
- excel - 如何在excel中的两个valor之间求和?
- javascript - Vue 在 Axios API 调用中将布尔值从 true 更改为 false
- python - 从标签内的网页中提取公司名称
- powershell - 如何在 Pester 中“获取调用的参数”(或生成包含实际值和预期值的有用消息)?
- php - 如何在 symfony 3 中使用相对路径
- azure-active-directory - 向托管标识授予 O365 管理 API 权限
- search - 如何在我自己的模块中使用 Liferay 搜索服务?
- android-studio - 构建应用程序时出现错误:app java Pre compile debug