简体   繁体   English

使用PHP回显文件名

[英]Echoing File Name using PHP

I am using Uploadify to upload a file. 我使用Uploadify上传文件。 The filename is randomized by the PHP when uploaded. 上传时,PHP会随机化文件名。 I need to echo the file name back to the JS. 我需要将文件名回显给JS。 I don't know enough PHP to do this. 我不知道足够的PHP来做到这一点。

Here is my PHP Script: 这是我的PHP脚本:

<?php

$targetFolder = '/uploads'; 

$verifyToken = md5('unique_salt' . $_POST['timestamp']);

if (!empty($_FILES)) {
 $tempFile = $_FILES['Filedata']['tmp_name'];
 $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
 $fileParts = pathinfo($_FILES['Filedata']['name']);
 $targetFile = sprintf('%s/%s.%s', $targetPath, uniqid(), $fileParts['extension']);


// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png','doc','docx','pdf','xlsx','pptx','tiff','tif','odt','flv','mpg','mp4','avi','mp3','wav','html','htm','psd','bmp','ai','pns','eps'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);

if (in_array($fileParts['extension'],$fileTypes)) {
    move_uploaded_file($tempFile,$targetFile);
    echo '1';
} else {
    echo 'Invalid file type.';
}
}
?> 

If I understand the script correctly where the echo '1' at the bottom of the script is where the file name should be. 如果我正确理解了脚本,那么脚本底部的echo '1'就是文件名所在的位置。 How do I insert the changed or randomized file name there? 如何在那里插入更改或随机化的文件名?

Your $targetFile variable contains the full path to your uploaded file (once moved). $targetFile变量包含上传文件的完整路径(一旦移动)。 If you just want the filename part, use basename() , eg 如果您只想要文件名部分,请使用basename() ,例如

echo basename($targetFile);

See http://php.net/basename 请参见http://php.net/basename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM