我正在尝试上传图片,但不会拍摄图片的填充路径。它只带有扩展名的文件名。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#sok").on('click',function(e){
//alert("Ok");
e.preventDefault();
var imf = $("#FoodieProfileImage").val();
alert(imf);
$.ajax({
url: 'Image.php',
type: 'POST',
data: imf,
async: false,
cache: false,
contentType: false,
processData: false,
success: function () {
//alert(returndata);
// $("#target").html(data);
},
error: function(){
}
});
});
});
</script>
</head>
<div class="modal-body">
<form id="editUserForm" method="post" action="Image.php" enctype="multipart/form-data">
<div class="form-group">
<label>Profile Image</label>
<input type="file" accept="image/*" name="FoodieProfileImage" id="FoodieProfileImage"/>
</div>
</form>
<div id="target"></div>
</div>
<div class="modal-footer">
<input type="button" id="sok" value="ok"/>
</div>
</html>
和php脚本一样:
<?php
if(!empty($_FILES["FoodieProfileImage"]["name"]) && $_FILES["FoodieProfileImage"]["size"] > 0) {
//Set upload path
$uploadPath = "images/";
echo $uploadPath;//print_r();
exit();
//Check for is directoty exist ot not, if not then create new directoty
if (!is_dir($uploadPath)) {
mkdir($uploadPath);
chmod($uploadPath, 0755);
}
if(isset($_FILES["FoodieProfileImage"]['name'])) {
$fileName = $_FILES["FoodieProfileImage"]["name"];
//$fileName = realpath($fileName);
//print_r(S_FILES);exit();
//$newUserDetail['profileImage'] = $fileName;
//pathinfo - Is a function that seperates file name, extension, basename
$path_parts = pathinfo($fileName);
$fileName = str_replace(" " , "_", strtolower($path_parts['filename'])); // Replaces all spaces with hyphens.
$fileName = preg_replace('/[^A-Za-z0-9\-]/', '', $fileName); // Removes special chars.
$fileName .= 'Foodie'.$userId ;
//Create new file name
move_uploaded_file($_FILES['FoodieProfileImage']["tmp_name"], $uploadPath . $fileName . '.' . $path_parts['extension']);
//Get Path for Uploaded File
$newUserDetail['profileImage'] = $uploadPath . $fileName. '.' . $path_parts['extension'];
//print_r($newUserDetail['profileImage']);exit();
//Get page image detail by page id
}
}
?>
答案 0 :(得分:0)
服务器本身只能显示它已经获得的数据,即使它知道文件路径,它也无法访问它(因为你通过HTTP请求接收数据,而不是任何拉扯等等。)
可以解决您的任务的是通过JavaScript使用FileReader API并实现一些逻辑,以便在同时上传图像的同时在客户端创建缩略图。