将文件保存到服务器

时间:2016-06-22 13:44:48

标签: php angularjs

我正在使用AngularJsPHP来尝试完成此操作,但它似乎无效。

我创建了一个带有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';
        }
    }
?>

HTML

<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>

2 个答案:

答案 0 :(得分:0)

我找到了必须将$folder设置为$_SERVER['DOCUMENT_ROOT'].'/uploads/'

的答案

答案 1 :(得分:-1)

您无法在JSON中POST文件。使用ng-file-upload时,您可以使用服务Upload进行上传。它负责标题并使您能够跟踪进度。查看https://github.com/danialfarid/ng-file-upload#usage