我有一個PHP函數,上傳圖片在我的文件夾圖片頁面:如何上傳圖像,而無需重新加載
if(isset($_FILES['filename']))
{
$Dest = dirname(__FILE__).'/images/';
if(!isset($_FILES['filename']) || !is_uploaded_file($_FILES['filename']['tmp_name'][0]))
{
die('Something went wrong with Upload!');
}
$ImageName = str_replace(' ','-',strtolower($_FILES['filename']['name'][0]));
$ImageType = $_FILES['filename']['type'][0]; //"image/png", image/jpeg etc.
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
//Create new image name (with random number added).
$NewName = $ImageName.'.'.$ImageExt;
move_uploaded_file($_FILES['filename']['tmp_name'][0], "$Dest/$NewName");
$base_path="http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF']);
$base=$base_path.'/'.'images/'.$NewName;
?>
<script>
\t window.history.back();
</script>
<?php
}
這裏是我的形式:
<form action="images.php" ng-show="data.uploadFile" method="post" enctype="multipart/form-data" name="fileupload" id="fileupload">
\t <input name="filename[]" id="filename" type="file" />
\t <input type="submit" id="submit" ng-click="data.uploadFile = !data.uploadFile" value="Upload" />
</form>
最後,這是我的js代碼,從文件夾中獲取所有圖片:
sliders.controller('MainController', ['$scope', '$interval', '$http', function($scope, $interval, $http) {
\t $scope.data = {uploadFile: false};
\t //variables for slider #1
\t $scope.timer1;
\t $scope.index1 = 0;
\t
\t //variables for slider #2
\t $scope.timer2;
\t $scope.index2 = 0;
\t
\t $http.get('images.php').success(function(data) {
\t \t $scope.images = data.split(',');
\t \t $scope.images.pop();
\t }); //get array with images and put in $scope.images
我使用angularJs,實際上我不明白我怎麼可以用角或AJAX上傳例如,如果我將在我的表單中使用一些js函數而不是action =「images.php」,那麼將如何工作php上傳函數?
如果有人向我解釋我將不勝感激,我該如何做到這一點,謝謝。
在這裏網址:「URL: 'http://your.server.com/path/', ' 它應該是我的PHP文件的路徑? – Wahtel
是的,「url」與form中的「action」相同,在這種情況下,「images.php」改變回復。 – Oscargeek
不幸的是發生錯誤:Uncaught TypeError:非法調用 – Wahtel