php is selected,使用php選擇文件時自動提交上傳表單?

在个人网站的头像上传表单中,尝试通过JavaScript的onchange()事件实现选择图片后自动提交,但在使用PHP处理上传时遇到问题。尽管JS和jQuery代码正确触发了提交,但上传.php页面只显示空白,没有执行任何代码。问题出在PHP的文件上传处理部分。解决方案包括检查$_FILES['file']是否已设置,并开启PHP错误显示以定位错误源。
摘要由CSDN通过智能技术生成

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值