Question

I am using Uploadify to upload a file. The filename is randomized by the PHP when uploaded. I need to echo the file name back to the JS. I don't know enough PHP to do this.

Here is my PHP Script:

<?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. How do I insert the changed or randomized file name there?

Was it helpful?

Solution

Your $targetFile variable contains the full path to your uploaded file (once moved). If you just want the filename part, use basename(), eg

echo basename($targetFile);

See http://php.net/basename

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top