dongzhuang6247 2019-06-22 22:33
浏览 110

无法在PHP中获取查询字符串(初学者/简单问题?)

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)
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 eclipse mqtt 消息消费问题
    • ¥15 vue2(标签-chrome|关键词-浏览器兼容)
    • ¥15 python网络流自动生成系统 医学领域
    • ¥15 sql查询仓库里都有什么
    • ¥15 代码的修改,添加和运行完善
    • ¥15 krpano-场景分组和自定义地图分组
    • ¥15 lammps Gpu加速出错
    • ¥15 关于PLUS模型中kapaa值的问题
    • ¥15 关于博途V17进行仿真时无法建立连接问题
    • ¥15 机器学习教材中的例题询问