Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中检索最新添加文件的上载文件路径_Php_C9.io - Fatal编程技术网

在php中检索最新添加文件的上载文件路径

在php中检索最新添加文件的上载文件路径,php,c9.io,Php,C9.io,让我们从头开始。我是一名学生,最近有一门关于数据库的WebDevelopment课程(我确信这门课程有不同的英文名称),我正在创建一个动态网站。 您可以添加、编辑和删除网站/文章 我想能够上传你自己的标志在你的导航栏和你的“标签”,但我已经走到了死胡同。我跟踪了youtube上的一个家伙,他解释了如何将文件上传到我的服务器的特定文件夹中,并将其限制为仅限于特定的文件类型和特定的文件大小 但现在我需要检索该图像的文件路径,以便在网站的导航栏上将其显示为徽标 我开始思考的方式是,我需要以某种方式获取

让我们从头开始。我是一名学生,最近有一门关于数据库的WebDevelopment课程(我确信这门课程有不同的英文名称),我正在创建一个动态网站。 您可以添加、编辑和删除网站/文章

我想能够上传你自己的标志在你的导航栏和你的“标签”,但我已经走到了死胡同。我跟踪了youtube上的一个家伙,他解释了如何将文件上传到我的服务器的特定文件夹中,并将其限制为仅限于特定的文件类型和特定的文件大小

但现在我需要检索该图像的文件路径,以便在网站的导航栏上将其显示为徽标

我开始思考的方式是,我需要以某种方式获取最新修改的文件,然后以某种方式获取其位置/文件路径,然后将其保存到变量中

我目前上传图像的代码如下: 它位于根文件夹中,名为“upload.php”

也许我无法从我试图获取的文件中访问变量? 如前所述,此文件(“upload.php”)位于根文件夹内。我试图在以下位置使用变量$latest_filename:root/views/master.php


我不知道还有什么要补充的,我试着让它尽可能透明。

您可以尝试将
$fileDestination
保存到会话中,或者访问存储图像的文件夹,并通过查看图像的时间戳检索最新图像
<?php
if (isset($_POST['upload'])) {
$file = $_FILES['file'];

/* $_FILES gives you an array of info of an file */
/* below i give each variable some info from my file */

$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$fileSize = $_FILES['file']['size'];
$fileError = $_FILES['file']['error'];
$fileType = $_FILES['file']['type'];

 /* Ext = extension.*/
 /* i only want .jpg and. png files on my site */
 /* Here i check if it has .jpg or png at the end of the file name */
$fileExt = explode('.', $fileName);
$fileActualExt = strtolower(end($fileExt));

/* Creating an array with accepted file endings */
$allowed = array('jpg', 'jpeg', 'png');

if (in_array($fileActualExt, $allowed)) {
    if ($fileError === 0) {
        if ($fileSize < 1000000) {

            /* newimages get uniq names inside database */
            /* in this case it uses milliseconds */
            $fileNameNew = uniqid('', true).".".$fileActualExt;

            /* set file destination */

            $fileDestination = 'images/'.$fileName;

            move_uploaded_file($fileTmpName, $fileDestination);
            header('Location: index.php?uploadsuccess');

        }else {
            echo "Your file was to big! Make sure it's less than 1MB!";
        }
    }else {
        echo "There was an error uploading your file! Please try again!";
    }
}else {
    echo "You cannot Upload files of this type!";
}
}
<img src="images/file_name.jpg>" class="navbar-logo" alt="">
/* get latest image name */ 
$path = "images"; 

$latest_ctime = 0;
$latest_filename = '';    

$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
// could do also other checks than just checking whether the entry is a file
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}