私はこれらの2つのファイルindex.phpとuploadcover.inc.phpを持っており、入力フィールドをfileとしてindex.phpのフォームを送信すると、画像をアップロードしてカバー画像としてアップロードできます。コードは正常に動作しますが、このindex.phpファイルを修正して、AJAXを使用してページをリフレッシュせずに、uploadcover.inc.phpからヘッダー関数を使用してindex.phpにリダイレクトするようにします。私を出す。AJAXを実装するためのコードを修正してください
のindex.php =>
<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>
<form action="include/uploadcover.inc.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="cover-upload" style="display:none" onchange="this.form.submit();">
<label for="cover-upload" class="fa fa-camera fa-2x" aria-hidden="true"></label>
</form>
</body>
</html>
uploadcover.inc.php =>
<?php
session_start();
include_once 'dbh.inc.php';
$sessionid = $_SESSION['u_id'];
$file= $_FILES['file'];
$fileName= $file['name'];
$fileTmpName= $file['tmp_name'];
$fileSize= $file['size'];
$fileError= $file['error'];
$fileType= $file['type'];
$fileExt = explode('.',$fileName);
$fileActualExt = strtolower(end($fileExt));
$allowed = array('jpg','jpeg','png','gif');
if(in_array($fileActualExt,$allowed)){
if($fileError=== 0){
if($fileSize<3145728){
$fileNameNew = "cover".$sessionid.".".$fileActualExt;
$fileDestination = '../profile/'.$fileNameNew;
move_uploaded_file($fileTmpName,$fileDestination);
$sql = "UPDATE coverimg SET status=0 WHERE user_id='$sessionid'";
mysqli_query($conn,$sql);
header("Location: ../index.php?upload=success");
} else {
$_SESSION['errors']['size'] = 1;
session_write_close();
header("Location: ../index.php?upload=size_exceeded_3MB");
}
} else {
$_SESSION['errors']['upload'] = 1;
session_write_close();
header("Location: ../index.php?upload=error");
}
} else{
$_SESSION['errors']['type'] = 1;
session_write_close();
header("Location: ../index.php?upload=typeerror");
}
ヘルプ、index.phpファイルのためのAJAXコードでくれアウトはせずに同じことを実現するためにuploadcover.inc.phpにヘッダーが必要です。
編集用https://stackoverflow.com/questions/2320069/jquery-ajax-file-upload?rq=1 – Hkachhia