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 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用
    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛