upload php
This commit is contained in:
13
.vscode/launch.json
vendored
13
.vscode/launch.json
vendored
@ -1,10 +1,15 @@
|
|||||||
{
|
{
|
||||||
// Verwendet IntelliSense zum Ermitteln möglicher Attribute.
|
|
||||||
// Zeigen Sie auf vorhandene Attribute, um die zugehörigen Beschreibungen anzuzeigen.
|
|
||||||
// Weitere Informationen finden Sie unter https://go.microsoft.com/fwlink/?linkid=830387
|
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Listen for Xdebug",
|
||||||
|
"type": "php",
|
||||||
|
"request": "launch",
|
||||||
|
"port": [
|
||||||
|
9003,
|
||||||
|
9000
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "chrome",
|
"type": "chrome",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
|
|||||||
@ -46,6 +46,23 @@ class CUser
|
|||||||
$this->qr_id = $qr_id;
|
$this->qr_id = $qr_id;
|
||||||
$this->email = $email;
|
$this->email = $email;
|
||||||
}
|
}
|
||||||
|
function jsonarray()
|
||||||
|
{
|
||||||
|
return json_encode([
|
||||||
|
'id' => $this->id,
|
||||||
|
'qr_id' => $this->qr_id,
|
||||||
|
'email' => $this->email
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
function phparray()
|
||||||
|
{
|
||||||
|
return ([
|
||||||
|
'id' => $this->id,
|
||||||
|
'qr_id' => $this->qr_id,
|
||||||
|
'email' => $this->email
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -81,18 +98,26 @@ function random_str(
|
|||||||
function getNewFilename($targetDir, $fileExt, $length)
|
function getNewFilename($targetDir, $fileExt, $length)
|
||||||
{
|
{
|
||||||
$newFname = random_str($length);
|
$newFname = random_str($length);
|
||||||
|
|
||||||
|
// echo "newFname " . $newFname . "\n";
|
||||||
|
|
||||||
$maxtries = 100000; // prevent endless loop, most unlikely
|
$maxtries = 100000; // prevent endless loop, most unlikely
|
||||||
$tries = 0;
|
$tries = 0;
|
||||||
|
// echo "file_exists " . $targetDir . '/' . $newFname . '.' . $fileExt . "\n";
|
||||||
|
// echo "file_exists " . file_exists($targetDir . '/' . $newFname . '.' . $fileExt) . "\n";
|
||||||
while(file_exists($targetDir . '/' . $newFname . '.' . $fileExt) && $tries < $maxtries)
|
while(file_exists($targetDir . '/' . $newFname . '.' . $fileExt) && $tries < $maxtries)
|
||||||
{
|
{
|
||||||
|
// echo "file_exists " . $targetDir . '/' . $newFname . '.' . $fileExt . "\n";
|
||||||
++$tries;
|
++$tries;
|
||||||
$newFname = random_str($length);
|
$newFname = random_str($length);
|
||||||
|
// echo "tries " . $tries . "\n";
|
||||||
|
// echo "newFname " . $newFname . "\n";
|
||||||
}
|
}
|
||||||
if($tries < $maxtries)
|
if($tries >= $maxtries)
|
||||||
{
|
{
|
||||||
$newFname = "";
|
$newFname = "";
|
||||||
}
|
}
|
||||||
return $newFname;
|
return $newFname .".".$fileExt;
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
@ -89,7 +89,8 @@ try
|
|||||||
$row['qr_id'],
|
$row['qr_id'],
|
||||||
$row['email']
|
$row['email']
|
||||||
);
|
);
|
||||||
$_SESSION['user'] = $user;
|
// $_SESSION['user'] = $user;
|
||||||
|
$_SESSION['user'] = $user->phparray();
|
||||||
|
|
||||||
$returnData = new CMsg(
|
$returnData = new CMsg(
|
||||||
1,
|
1,
|
||||||
|
|||||||
@ -5,79 +5,135 @@ header("Access-Control-Allow-Methods: POST");
|
|||||||
header("Content-Type: application/json; charset=UTF-8");
|
header("Content-Type: application/json; charset=UTF-8");
|
||||||
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
|
||||||
|
|
||||||
session_start();
|
|
||||||
|
|
||||||
require __DIR__.'/classes/Database.php';
|
try {
|
||||||
require __DIR__.'/classes/lib.php';
|
|
||||||
|
|
||||||
$result = new CMsg(0);
|
|
||||||
|
|
||||||
if(!isset($_SESSION["user"]))
|
session_start();
|
||||||
{
|
|
||||||
$result = new CMsg(0, 401, "not logged in");
|
|
||||||
return $result->jsonarray();
|
|
||||||
}
|
|
||||||
|
|
||||||
$user = $_SESSION["user"];
|
// $resp = json_decode('
|
||||||
$allowTypes = array('jpg','png','jpeg','gif','pdf');
|
// {
|
||||||
|
// "success": 1,
|
||||||
|
// "user": {
|
||||||
|
// "__PHP_Incomplete_Class_Name": "CUser",
|
||||||
|
// "id": 11,
|
||||||
|
// "qr_id": "YQiwEB",
|
||||||
|
// "email": "p.para@gmx.de"
|
||||||
|
// }
|
||||||
|
// }');
|
||||||
|
|
||||||
// File upload path
|
// $_SESSION["user"] = $resp["user"];
|
||||||
$targetDir = "uploads/".$user->qr_id;
|
|
||||||
if (!file_exists($targetDir))
|
|
||||||
{
|
|
||||||
mkdir($targetDir, 0755, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
$fileName = basename($_FILES["file"]["name"]);
|
require __DIR__ . '/classes/Database.php';
|
||||||
$fileType = pathinfo($fileName,PATHINFO_EXTENSION);
|
require __DIR__ . '/classes/lib.php';
|
||||||
|
|
||||||
if(!in_array($fileType, $allowTypes))
|
$result = new CMsg(0);
|
||||||
{
|
|
||||||
$result = new CMsg(
|
|
||||||
0,
|
|
||||||
406,
|
|
||||||
'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.');
|
|
||||||
return $result->jsonarray();
|
|
||||||
}
|
|
||||||
|
|
||||||
$newFilename = getNewFilename($targetDir, $fileType, 20);
|
if (!isset($_SESSION["user"]))
|
||||||
if(strlen($newFilename) == 0)
|
|
||||||
{
|
|
||||||
$result = new CMsg(
|
|
||||||
0,
|
|
||||||
507,
|
|
||||||
'Too many uploaded files on the server, try it again later');
|
|
||||||
return $result->jsonarray();
|
|
||||||
}
|
|
||||||
|
|
||||||
$targetFilePath = $targetDir . '/' . $fileName;
|
|
||||||
|
|
||||||
if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"]))
|
|
||||||
{
|
|
||||||
// Upload file to server
|
|
||||||
if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath))
|
|
||||||
{
|
{
|
||||||
// Insert image file name into database
|
$result = new CMsg(0, 401, "not logged in");
|
||||||
$update = $db->query("UPDATE dogs SET picture = '".$fileName."' WHERE qr_id = ".$user->qr_id);
|
echo $result->jsonarray();
|
||||||
if($update)
|
return $result->jsonarray();
|
||||||
{
|
|
||||||
$result = new CMsg(1,200,"The file ".$fileName. " has been uploaded successfully.");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$result = new CMsg(0,500,"File upload failed, please try again.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$user = $_SESSION["user"];
|
||||||
|
$allowTypes = array('jpg', 'png', 'jpeg', 'gif', 'pdf');
|
||||||
|
//echo var_dump($user);
|
||||||
|
// File upload path
|
||||||
|
$targetDir = "../uploads/" . $user["qr_id"];
|
||||||
|
if (!file_exists($targetDir))
|
||||||
|
{
|
||||||
|
mkdir($targetDir, 0755, true);
|
||||||
|
}
|
||||||
|
echo "targetDir ". $targetDir . "\n";
|
||||||
|
|
||||||
|
$fileName = basename($_FILES["file"]["name"]);
|
||||||
|
$fileType = pathinfo($fileName, PATHINFO_EXTENSION);
|
||||||
|
|
||||||
|
echo "fileName ".$fileName . "\n";
|
||||||
|
echo "fileType ".$fileType . "\n";
|
||||||
|
|
||||||
|
if (!in_array($fileType, $allowTypes))
|
||||||
|
{
|
||||||
|
$result = new CMsg(
|
||||||
|
0,
|
||||||
|
406,
|
||||||
|
'Sorry, only JPG, JPEG, PNG, GIF, & PDF files are allowed to upload.'
|
||||||
|
);
|
||||||
|
echo $result->jsonarray();
|
||||||
|
return $result->jsonarray();
|
||||||
|
}
|
||||||
|
|
||||||
|
$newFilename = getNewFilename($targetDir, $fileType, 20);
|
||||||
|
|
||||||
|
echo "newFilename ".$newFilename . "\n";
|
||||||
|
|
||||||
|
|
||||||
|
if (strlen($newFilename) == 0)
|
||||||
|
{
|
||||||
|
$result = new CMsg(
|
||||||
|
0,
|
||||||
|
507,
|
||||||
|
'Too many uploaded files on the server, try it again later'
|
||||||
|
);
|
||||||
|
echo $result->jsonarray();
|
||||||
|
|
||||||
|
return $result->jsonarray();
|
||||||
|
}
|
||||||
|
|
||||||
|
$targetFilePath = $targetDir . '/' . $newFilename;
|
||||||
|
echo "targetFilePath ". $targetFilePath."\n";
|
||||||
|
|
||||||
|
if (isset($_POST["submit"]) && !empty($_FILES["file"]["name"]))
|
||||||
|
{
|
||||||
|
echo "submit file name ". $_FILES["file"]["name"]."\n";
|
||||||
|
// Upload file to server
|
||||||
|
if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath))
|
||||||
|
{
|
||||||
|
echo "move ok! ". $targetFilePath."\n";
|
||||||
|
$newPathFilename = '/' . $user["qr_id"] . '/' . $newFilename;
|
||||||
|
echo "query " . "UPDATE dogs SET picture = '" . $newPathFilename . "' WHERE qr_id = '" . $user["qr_id"]."'\n";
|
||||||
|
$db_connection = new Database();
|
||||||
|
$conn = $db_connection->dbConnection();
|
||||||
|
|
||||||
|
|
||||||
|
echo var_dump($conn);
|
||||||
|
// Insert image file name into database
|
||||||
|
$statement = $conn->prepare("UPDATE dogs SET picture = :filePic WHERE qr_id = :user_qr_id");
|
||||||
|
$update = $statement->execute(array('filePic' => $newPathFilename, 'user_qr_id' => $user["qr_id"]));
|
||||||
|
echo var_dump($update);
|
||||||
|
$count = $statement->rowCount();
|
||||||
|
echo 'rowcount ' . $count . '\n';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if ($update && $count > 0)
|
||||||
|
{
|
||||||
|
$result = new CMsg(1, 200, "The file " . $newPathFilename . " has been uploaded successfully.");
|
||||||
|
echo $result->jsonarray();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$result = new CMsg(0, 500, "File upload failed, please try again.");
|
||||||
|
echo $result->jsonarray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$result = new CMsg(0, 500, "Sorry, there was an error uploading your file.");
|
||||||
|
}
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$result = new CMsg(0,500,"Sorry, there was an error uploading your file.");
|
$result = new CMsg(1, 204, 'Please select a file to upload.');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$result = new CMsg(1,204,'Please select a file to upload.');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Display status message
|
// Display status message
|
||||||
echo $result->jsonarray();
|
echo $result->jsonarray();
|
||||||
?>
|
}
|
||||||
|
catch(Exception $e)
|
||||||
|
{
|
||||||
|
$result = new CMsg(0, 500, $e->getMessage() );
|
||||||
|
echo $result->jsonarray();
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|||||||
@ -6,6 +6,7 @@ const upload = (file: File, onUploadProgress: any): Promise<any> => {
|
|||||||
let formData = new FormData();
|
let formData = new FormData();
|
||||||
|
|
||||||
formData.append("file", file);
|
formData.append("file", file);
|
||||||
|
formData.append("submit", "1");
|
||||||
|
|
||||||
let uploadRes = Axios.post("upload.php", formData, {
|
let uploadRes = Axios.post("upload.php", formData, {
|
||||||
headers: {
|
headers: {
|
||||||
@ -14,8 +15,8 @@ const upload = (file: File, onUploadProgress: any): Promise<any> => {
|
|||||||
onUploadProgress,
|
onUploadProgress,
|
||||||
});
|
});
|
||||||
|
|
||||||
let a: number = 1+3;
|
// let a: number = 1+3;
|
||||||
a++;
|
// a++;
|
||||||
return uploadRes;
|
return uploadRes;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user