簡體   English   中英

如何在上傳之前獲取文件名並傳遞給另一個文本輸入字段而沒有fakepath

[英]How to get file name before upload and pass to another text input field without fakepath

我想在(服務器上的)文件夾中上載圖像,並想在文件輸入選擇中獲得圖像名稱,然后將此圖像名稱傳遞給另一個沒有fakepath的文本輸入字段。 這是我的代碼:這是文件上傳php代碼

<?php

if (($_FILES['my_file']['name']!="")){
// Where the file is going to be stored
$target_dir = "upload/";
$file = $_FILES['my_file']['name'];
$path = pathinfo($file);
$filename = $path['filename'];
$ext = $path['extension'];
$temp_name = $_FILES['my_file']['tmp_name'];
$path_filename_ext = $target_dir.$filename.".".$ext;

// Check if file already exists
if (file_exists($path_filename_ext)) {
echo "Sorry, file already exists.";
}else{
move_uploaded_file($temp_name,$path_filename_ext);
echo "Congratulations! File Uploaded Successfully.";
}
}
?>

這是json文件寫代碼

<?php
$message = '';
$error = '';

if(isset($_POST["submit"]))
{

if(empty($_POST["filename"]))
{
$error = "<label class='text-danger'>some text </label>";
}

else
{
if(file_exists('jasonfile.json'))
{
$current_data = file_get_contents('jasonfile.json');
$array_data  = json_decode($current_data, true);

$item = array

( 
'ImageUrl'        =>     $_POST["filename"]
);
array_unshift($array_data["user"] , $item);

$final_data  = json_encode($array_data);
if(file_put_contents('jasonfile.json', $final_data)){
}}
else{
$error = 'JSON File not exits';
}}}
?>

這是我的表單html代碼,我想獲取圖像文件名並將其作為值傳遞給第二個文本輸入字段

<!DOCTYPE html>
<html>
<head></head>
<body>
<div class="container" style="width:500px;">
<h3 align="center">Upload YOUR file in folder</h3>
<form action = "?" method="post" enctype ="multipart/form-data">
<?php

?>

<form name="form" method="post" enctype="multipart/form-data" >
<input type="file" name="my_file" onchange="this.form.filename.value = this.value" /> 
<input type="text" name="filename" />
<input type="submit" name="submit" value="Upload"/>
</form>


</form>
</div>
</body>
</html>

提前致謝 :)

您可以實現iput文件的change事件並獲取e.target.files[0].name;

 $('input[type="file"]').change(function(e){ var fileName = e.target.files[0].name; $('[name=filename]').val(fileName.replace(/C:\\\\fakepath\\\\/i, '')); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form name="form" method="post" enctype="multipart/form-data" > <input type="file" name="my_file" onchange="this.form.filename.value = this.value" /> <input type="text" name="filename" /> <input type="submit" name="submit" value="Upload"/> </form> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM