在上传时发布上传的文件名

时间:2019-05-09 02:55:11

标签: php

我正在upload.php上创建一个上传页面。我还希望upload.php将文件名写为example.com的扩展名,并在段落中显示它,这是一个文件链接。我将使用PHP get内容在上载页面中显示该单独的页面。

这是我的代码

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if file already exists
if (file_exists($target_file)) {
    echo " Sorry, the file already exists. Please try renaming or adding a version number.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
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_file)) {
        echo " The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo " Sorry, there was an error uploading your file.";
    }
}
?>

<?php
$text = "<a href="example.com/$target_file"<p>$target_file</p></a> <br> \n";
$file = fopen("/files.html","a+ \n");
 fwrite($file, $text);
 fclose($file);
?>

我希望最终结果是这样:

<a href="example.com/uploadedfile.png"<p>uploadedfile.png</p></a> <br>

我找不到一个专门用于在上传时发布到单独文件的问题。任何帮助将不胜感激。

谢谢。

0 个答案:

没有答案