简体   繁体   English

从angular到php服务器上传和检索图像

[英]upload and retrieve image from angular to php server

I created the application using ionic and angular with php back end. 我用php后端创建了使用ionic和angular的应用程序。

I can able to upload the image from angular to php server as per below workouts. 我可以根据下面的训练将图像从角度上传到php服务器。

Upload.js 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 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 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);
?>

As of now I can able to upload the selected image and stored in my PHP server "Inmate_Images" folder. 截至目前,我可以上传所选图像并存储在我的PHP服务器“Inmate_Images”文件夹中。

My requirement is : 我的要求是:

  1. I need to change the image file name and then upload it in server. 我需要更改图像文件名,然后将其上传到服务器中。

  2. And how to get the image using get request and the name which I have given while upload. 以及如何使用get请求和我在上传时提供的名称来获取图像。 ?? ??

How to handle this activity any idea ? 如何处理这个活动的想法?

As mentioned my requirement second point I found the answers please find the below one. 如上所述我的要求第二点我找到了答案,请找到下面的答案。 Any suggestions pls let me know. 任何建议请告诉我。

preview.html preview.html

<img ng-src="http://www.testphp.com/Inmate_Images/mike.png" style="height:75px; padding: 5px 5px 5px 5px;"/>

The thing is you just get the path of your images folder in your server and you can able to display it directly using < img > tag. 问题是你只需在服务器中获取图像文件夹的路径,就可以使用<img>标签直接显示它。

But My question is How can I change the image file name while choosing from < input type=file > tag Because I want to change the file name while upload it would be easier for me to get the file again. 但我的问题是如何在从<input type = file>标签中选择时更改图像文件名因为我想在上传时更改文件名,我会更容易再次获取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM