I keep it short. I have a file that can be uploaded to our server via PHP. Now, I want to rename that file based on the query string in the URL: https://XXX.de/QG_002a.html?tic=undefined
In this case, I would want the file to be named "undefined".
Problem is, the upload works if I enter a filename manually, but it does not work if I try to grab the query string for it. Here is the code:
$actual_link = $_SERVER['REQUEST_URI'];
$parts = parse_url($actual_link);
parse_str($parts['query'], $query);
$newfilename = $target_dir . $query['tic'];
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir .
$newfilename))
{
echo "The file has been uploaded.";
}
I really googled a lot and tried many solutions for getting the current URL or getting the query string like:
$_GET['tic'];
But nothing works in my case. Could anyone please have a short look at the code? Maybe it's just a simple beginners mistake. Thanks a lot!
COMPLETE CURRENT PHP CODE
<?php
error_reporting(-1);
ini_set('display_errors', true);
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_dir . $_GET['tic'];)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
CURRENT ERRORS
VM46 uploadForm_Gallery.js:355 POST https://XXX.de/upload.php 500 (Internal Server Error)
saveToQueue @ VM46 uploadForm_Gallery.js:355
(anonymous) @ VM46 uploadForm_Gallery.js:245
dispatch @ VM44 jquery.min.js:3
q.handle @ VM44 jquery.min.js:3
JAVASCRIPT
const url = 'upload.php'
const request = new Request(url, {
method: 'POST',
body: formData
});
fetch(request)