i have a simple upload profile avatar form in my own website and i want to make it upload automatically after selecting an image.. i tried using onchange()
我在我自己的網站上有一個簡單的上傳個人資料頭像形式,我想在選擇圖像后自動上傳..我嘗試使用onchange()
js:
JS:
document.getElementById("file").onchange = function() {
document.getElementById("form").submit();
};
Jquery:
jQuery的:
$('#file').change(function() {
$('#target').submit();
});
html:
HTML:
but for some weird reasons it doesn't work with my upload.php page, after choosing the file it just redirect to upload.php without making it's code to work which shows a blank page..
但由於一些奇怪的原因,它不適用於我的upload.php頁面,選擇文件后它只是重定向到upload.php而沒有使它的代碼工作,顯示一個空白頁面..
in case this is my upload.php code:
如果這是我的upload.php代碼:
include_once 'includes/dbh.inc.php';
include_once 'includes/vars.inc.php';
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$fileNAME = $_FILES['file']['name'];
$fileTYPE = $_FILES['file']['type'];
$fileTMPNM = $_FILES['file']['tmp_name'];
$fileERROR = $_FILES['file']['error'];
$fileSIZE = $_FILES['file']['size'];
$fileEXT = explode('.', $fileNAME);
$fileACTUALEXT = strtolower(end($fileEXT));
$fileALLOWED = array('jpg', 'jpeg', 'png', 'gif');
if (in_array($fileACTUALEXT, $fileALLOWED)) {
if ($fileERROR === 0) {
if ($fileSIZE < 5000000) {
$fileNEWNAME = $userUID."-avatar.".$fileACTUALEXT;
$fileROOT = 'content/uploads/'.$fileNEWNAME;
move_uploaded_file($fileTMPNM, $fileROOT);
$sql = "UPDATE user_meta SET um_avatar_status=0 WHERE um_user_id='$userID';";
$result = mysqli_query($conn, $sql);
header('Location: '.$siteurl.'/user/'.$userUID.'?editavatar=success');
} else {
echo "The file you are trying to upload is TOO big!";
}
} else {
echo "Oops! there was an unknown ERROR, please try again later.";
}
} else {
echo "you can't upload this type of files!";
}
}
?>
2 个解决方案
#1
0
You need to change This part if (isset($_file['submit'])) { withif (isset($_FILES['file'])) {
您需要更改此部分if(isset($ _ file ['submit'])){withif(isset($ _ FILES ['file'])){
$_POST contains all the data from forms (except files).
$ _POST包含表單中的所有數據(文件除外)。
$_FILES contains all files sent to server via forms (only from )
$ _FILES包含通過表單發送到服務器的所有文件(僅來自)
include_once 'includes/dbh.inc.php';
include_once 'includes/vars.inc.php';
if (isset($_FILES['file'])) {
$file = $_FILES['file'];
$fileNAME = $_FILES['file']['name'];
$fileTYPE = $_FILES['file']['type'];
$fileTMPNM = $_FILES['file']['tmp_name'];
$fileERROR = $_FILES['file']['error'];
$fileSIZE = $_FILES['file']['size'];
$fileEXT = explode('.', $fileNAME);
$fileACTUALEXT = strtolower(end($fileEXT));
$fileALLOWED = array('jpg', 'jpeg', 'png', 'gif');
if (in_array($fileACTUALEXT, $fileALLOWED)) {
if ($fileERROR === 0) {
if ($fileSIZE < 5000000) {
$fileNEWNAME = $userUID."-avatar.".$fileACTUALEXT;
$fileROOT = 'content/uploads/'.$fileNEWNAME;
move_uploaded_file($fileTMPNM, $fileROOT);
$sql = "UPDATE user_meta SET um_avatar_status=0 WHERE um_user_id='$userID';";
$result = mysqli_query($conn, $sql);
header('Location: '.$siteurl.'/user/'.$userUID.'?editavatar=success');
} else {
echo "The file you are trying to upload is TOO big!";
}
} else {
echo "Oops! there was an unknown ERROR, please try again later.";
}
} else {
echo "you can't upload this type of files!";
}
}
?>
#2
0
Add this on top of upload.php to see php errors, It will be useful for you to track down where the error is occurring in
在upload.php之上添加它以查看php錯誤,您將有助於追蹤錯誤發生的位置
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);