使用html5/javascript和php上传/更改头像照片?
我在看这个链接,它允许用户在头像/个人资料照片上拖动图像,他们可以将图像放在上面,这将改变/上传不同的图像 链接: 我现在想知道的是如何使用php上传照片,有人能告诉我怎么做吗?谢谢 html: avatar.js:使用html5/javascript和php上传/更改头像照片?,php,javascript,Php,Javascript,我在看这个链接,它允许用户在头像/个人资料照片上拖动图像,他们可以将图像放在上面,这将改变/上传不同的图像 链接: 我现在想知道的是如何使用php上传照片,有人能告诉我怎么做吗?谢谢 html: avatar.js: // Required for drag and drop file access jQuery.event.props.push('dataTransfer'); // IIFE to prevent globals (function() { var s;
// Required for drag and drop file access
jQuery.event.props.push('dataTransfer');
// IIFE to prevent globals
(function() {
var s;
var Avatar = {
settings: {
bod: $("body"),
img: $("#profile-avatar"),
fileInput: $("#uploader")
},
init: function() {
s = Avatar.settings;
Avatar.bindUIActions();
},
bindUIActions: function() {
var timer;
s.bod.on("dragover", function(event) {
clearTimeout(timer);
if (event.currentTarget == s.bod[0]) {
Avatar.showDroppableArea();
}
// Required for drop to work
return false;
});
s.bod.on('dragleave', function(event) {
if (event.currentTarget == s.bod[0]) {
// Flicker protection
timer = setTimeout(function() {
Avatar.hideDroppableArea();
}, 200);
}
});
s.bod.on('drop', function(event) {
// Or else the browser will open the file
event.preventDefault();
Avatar.handleDrop(event.dataTransfer.files);
});
s.fileInput.on('change', function(event) {
Avatar.handleDrop(event.target.files);
});
},
showDroppableArea: function() {
s.bod.addClass("droppable");
},
hideDroppableArea: function() {
s.bod.removeClass("droppable");
},
handleDrop: function(files) {
Avatar.hideDroppableArea();
// Multiple files can be dropped. Lets only deal with the "first" one.
var file = files[0];
if (typeof file !== 'undefined' && file.type.match('image.*')) {
Avatar.resizeImage(file, 256, function(data) {
Avatar.placeImage(data);
});
} else {
alert("That file wasn't an image.");
}
},
resizeImage: function(file, size, callback) {
var fileTracker = new FileReader;
fileTracker.onload = function() {
Resample(
this.result,
size,
size,
callback
);
}
fileTracker.readAsDataURL(file);
fileTracker.onabort = function() {
alert("The upload was aborted.");
}
fileTracker.onerror = function() {
alert("An error occured while reading the file.");
}
},
placeImage: function(data) {
s.img.attr("src", data);
}
}
Avatar.init();
})();
创建如下表单:
<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else{
echo "Sorry, there was a problem uploading your file.";
}
有关限制文件大小、扩展名等的更多选项,请查看链接仅链接答案应包括内容摘要对不起,我不知道。我编辑了它,我想现在可以了吧?
// Required for drag and drop file access
jQuery.event.props.push('dataTransfer');
// IIFE to prevent globals
(function() {
var s;
var Avatar = {
settings: {
bod: $("body"),
img: $("#profile-avatar"),
fileInput: $("#uploader")
},
init: function() {
s = Avatar.settings;
Avatar.bindUIActions();
},
bindUIActions: function() {
var timer;
s.bod.on("dragover", function(event) {
clearTimeout(timer);
if (event.currentTarget == s.bod[0]) {
Avatar.showDroppableArea();
}
// Required for drop to work
return false;
});
s.bod.on('dragleave', function(event) {
if (event.currentTarget == s.bod[0]) {
// Flicker protection
timer = setTimeout(function() {
Avatar.hideDroppableArea();
}, 200);
}
});
s.bod.on('drop', function(event) {
// Or else the browser will open the file
event.preventDefault();
Avatar.handleDrop(event.dataTransfer.files);
});
s.fileInput.on('change', function(event) {
Avatar.handleDrop(event.target.files);
});
},
showDroppableArea: function() {
s.bod.addClass("droppable");
},
hideDroppableArea: function() {
s.bod.removeClass("droppable");
},
handleDrop: function(files) {
Avatar.hideDroppableArea();
// Multiple files can be dropped. Lets only deal with the "first" one.
var file = files[0];
if (typeof file !== 'undefined' && file.type.match('image.*')) {
Avatar.resizeImage(file, 256, function(data) {
Avatar.placeImage(data);
});
} else {
alert("That file wasn't an image.");
}
},
resizeImage: function(file, size, callback) {
var fileTracker = new FileReader;
fileTracker.onload = function() {
Resample(
this.result,
size,
size,
callback
);
}
fileTracker.readAsDataURL(file);
fileTracker.onabort = function() {
alert("The upload was aborted.");
}
fileTracker.onerror = function() {
alert("An error occured while reading the file.");
}
},
placeImage: function(data) {
s.img.attr("src", data);
}
}
Avatar.init();
})();
<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)){
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else{
echo "Sorry, there was a problem uploading your file.";
}