1
我创建了应用程序使用离子和角度与php后端。从角度上传和检索图像到php服务器
我可以根据下面的锻炼将角度上传到php服务器的图像。
Upload.js
$scope.onProfileFileSelect = function($files) {
//$files: an array of files selected, each file has name, size, and type.
for (var i = 0; i < $files.length; i++) {
var $file = $files[i];
console.log($file.name);
$upload.upload({
url: 'http://www.testphp.com/upload.php',
file: $file,
progress: function(e){}
}).then(function(response) {
// file is uploaded successfully
$timeout(function() {
console.log(response.data);
});
});
}
}
Upload.html
<div class="item item-input item-icon-right">
<span class="input-label">Capture your profile picture: </span>
<input type="file" name="ProfilePath" ng-model="inmateData.IMProfilePhotoPath" ng-file-select="onProfileFileSelect($files)" multiple />
</div>
upload.php的
<?php
if (isset($_SERVER['HTTP_ORIGIN']))
{
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$target_dir = "Inmate_Images/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$result = move_uploaded_file($_FILES["file"]["tmp_name"], $target_file);
echo json_encode($_FILES["file"]);
echo($result);
?>
截至目前我能够上传选定的图像并存储在我的PHP服务器“Inmate_Images”文件夹中。
我的要求是:
我需要改变图像文件名,然后直接上传到服务器。
以及如何使用获取请求获取图片以及上传时提供的名称。 ??
如何处理此活动的任何想法?
您是否尝试过这两点?什么是错误?询问具体情况。 – Jay
console.log($ file.name); - 我试图通过$ .file.name =“userone”更改图像名称;但它没有反映出来。我没有任何想法访问php文件夹。 –