Php 将文件保存到服务器
我正在使用Php 将文件保存到服务器,php,angularjs,Php,Angularjs,我正在使用AngularJs和PHP来尝试实现这一点,但它似乎不起作用 我用WAMP创建了一个虚拟主机,在我接收所有内容的文件夹中,我创建了一个uploads文件夹,我想将通过不同表单上传的文件存储到该文件夹中。问题是每当我执行PHP时,文件夹仍然是空的。以下是我使用的代码: AngularJs myApp.controller('AdminPromotionsDetailsController', function($scope, $http) { $scope.msg = '';
AngularJs
和PHP
来尝试实现这一点,但它似乎不起作用
我用WAMP
创建了一个虚拟主机,在我接收所有内容的文件夹中,我创建了一个uploads
文件夹,我想将通过不同表单上传的文件存储到该文件夹中。问题是每当我执行PHP
时,文件夹仍然是空的。以下是我使用的代码:
AngularJs
myApp.controller('AdminPromotionsDetailsController', function($scope, $http) {
$scope.msg = '';
$scope.msg_type = 'error';
$scope.savePromotion = function(promotion) {
$scope.promotion.PromotionPhoto = promotion.PromotionPhoto.name;
$http.post('save.php', promotion)
.success(function(data, status, headers, config) {
if (data == 'success') {
$scope.msg = 'Success message';
$scope.msg_type = 'success';
} else {
$scope.msg = 'Error message';
$scope.msg_type = 'error';
}
}).error(function(data, status) {
$scope.msg = 'Error message';
$scope.msg_type = 'error';
});
}
});
PHP
<?php
$con = mysqli_connect('ecommerce', 'root', '', 'my_db');
// Check connection
if (!$con) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($con,"utf8");
$data = json_decode(file_get_contents("php://input"));
$id = '';
if(!empty($data->PromotionId)) {
$id = mysqli_real_escape_string($con, $data->PromotionId);
}
$name = mysqli_real_escape_string($con, $data->PromotionName);
$link = mysqli_real_escape_string($con, $data->PromotionLink);
$cover = mysqli_real_escape_string($con, $data->PromotionPhoto);
$date_start = mysqli_real_escape_string($con, $data->PromotionDateStart);
$date_end = mysqli_real_escape_string($con, $data->PromotionDateEnd);
$folder = 'http://ecommerce/uploads/';
move_uploaded_file($cover, "$folder".$cover);
if(!empty($data->PromotionContent)) {
$content = mysqli_real_escape_string($con, $data->PromotionContent);
} else {
$content = '';
}
if ($id != '') {
$sql = "UPDATE `promotions` SET PromotionName='$name', PromotionLink='$link', PromotionPhoto='$cover', PromotionDateStart='$date_start', PromotionDateEnd='$date_end', PromotionContent='$content' WHERE `PromotionId`='$id'";
$sql_res = mysqli_query($con, $sql) or die(mysqli_error($con));
if ($sql_res) {
print 'success';
} else {
print 'error';
}
} else {
$sql = "INSERT INTO promotions (PromotionName, PromotionLink, PromotionPhoto, PromotionDateStart, PromotionDateEnd, PromotionContent) VALUES ('$name', '$link', '$cover', '$date_start', '$date_end', '$content')";
$sql_res = mysqli_query($con, $sql) or die(mysqli_error($con));
if ($sql_res) {
print 'success';
} else {
print 'error';
}
}
?>
您不能在JSON中发布文件。当您使用ng file upload
时,您可以使用服务upload
进行上传。它负责标题,并使您能够跟踪进度。检查我找到了答案,我必须将$文件夹设置为$服务器['DOCUMENT\u ROOT']./uploads/'
尝试限制问题。进行调试,您将更快地得到答案。不可能有人会在自己的机器上尝试所有的代码。不,我不希望人们在他们的机器上尝试这些代码,更像是告诉我是否遗漏了导致我的问题的重要内容。因为我对所有这些都是新手,所以我想说的是,人们不能告诉你可能错过了什么,因为它有太多的代码,98%的人会直接点击,因为你根本没有调试,也没有将问题限制在任何方面。检查$data,它是空的吗?@Shank它不是。它包含了我在表单中传递的所有信息。是的,似乎你无法将http
传递给特定的函数
<form autocomplete="off" name="promotionForm">
<input type="hidden" value="" ng-model="promotion.PromotionId" />
<div class="form-notification form-notification--{{msg_type}}" ng-if="msg != ''">
{{msg}}
</div>
<div class="form-notification form-notification--error" ng-if="promotionForm.file.$error.maxSize">
{{msg}}
</div>
<ul>
<li class="input">
<label for="promotion_name" class="input__label">
Title
</label>
<input type="text" class="input__field" value="" ng-model="promotion.PromotionName" id="promotion_name" required="required" />
</li>
<li class="input">
<label for="promotion_link" class="input__label">
URL
</label>
<input type="text" class="input__field" value="" ng-model="promotion.PromotionLink" id="promotion_link" required="required" />
</li>
<li class="input">
<label for="promotion_link" class="input__label">
Cover
</label>
<input type="file" ngf-select ng-model="promotion.PromotionPhoto" name="file"
accept="image/*" ngf-max-size="10MB" required id="promotion_link"
class="input__field input__field--upload"
ngf-model-invalid="errorFile"
/>
<img ng-show="promotionForm.file.$valid" ngf-thumbnail="promotion.PromotionPhoto" alt="" />
<button ng-click="promotion.PromotionPhoto = null" ng-show="promotion.PromotionPhoto">
Remove
</button>
</li>
<li class="input">
<label for="date_start" class="input__label">
Date
</label>
<ul class="row">
<li class="small-6 columns">
<datepicker ng-model='promotion.PromotionDateStart' date-format='MMMM d, yyyy' disable-timepicker='true' ng-patter='/\d\d/\d\d/\d\d\d\d/' required></datepicker>
</li>
<li class="small-6 columns">
<datepicker ng-model='promotion.PromotionDateEnd' date-format='MMMM d, yyyy' disable-timepicker='true' ng-patter='/\d\d/\d\d/\d\d\d\d/' required></datepicker>
</li>
</ul>
</li>
<li class="input">
<label for="promotion_content" class="input__label">
Content
</label>
<text-angular ng-model="promotion.PromotionContent"></text-angular>
</li>
</ul>
<button type="submit" class="button--big" ng-click="savePromotion(promotion)"
ng-if="promotion.PromotionId != null"
>
Edit
</button>
<button type="submit" class="button--big" ng-click="savePromotion(promotion)"
ng-if="promotion.PromotionId == null"
>
Add
</button>
</form>