[我无法使PHP中的named()函数正常工作(或与此有关的任何文件修改函数)

问题描述 投票:0回答:1

感谢您的时间。

所以,我有一个LAMP堆栈Web服务器,并且想要将路径更改为上载的文件。

例如,用户通过前端上传图片,PHP代码读取图片,将文件重命名为哈希字符串,这样文件就不会重叠并且在图片上传目录中具有相同的名称,并指向用户行上的图片列到SQL表中的新路径。

因此,除了重命名文件之外,我已经可以正常工作。

文件名成功传递(即ball.png)

    $fileName = basename($_FILES["file"]["name"]); //This works, and let's pretend that it is "ball.png" in this example.

    $baseDir = "uploads/profiles"; //This code works as it should.
    $targetPath = $baseDir.$fileName; //This code works as it should.

    $newFileName = $randomString.$fileExtension;
    //The random string variable is generated in a previous function, and it works. Let's pretend that it is "e7635hasf1 in this example."
    //The file extension variable is generated in a previous function, and it works. Let's pretend that it is ".png" in this example.

    rename($targetPath, $newFileName);
    //This is the code that does not work. I've tried changing permissions, doing it in the same directory to simplify things, but can't get rename() to work at all.

    updateTables();
    //This code works as it should. The SQL picture column for the row of the user that uploaded the picture now reads "uploads/profiles/e7635f1.png"

我正在使用PHP 7,如果有帮助的话。如果我不重命名任何内容(例如,如果我执行updateTables()函数并传递“ uploads / profiles / ball.png”而不是“ uploads / profiles / e7653hasf1.png”),则此代码可以正常工作。这基本上可以确定问题出在rename()函数中,因此特定的单行代码无法正常工作。

再次感谢您的时间。如果您有任何疑问,请让我知道。

php mysql lamp
1个回答
0
投票
$targetPath = $baseDir.'/'.$fileName;

$newFileName = $baseDir.'/'.$randomString.$fileExtension;
© www.soinside.com 2019 - 2024. All rights reserved.