Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting complete PATH of uploaded file - PHP

Tags:

I have a form(HTML, PHP) that lets the end user upload a file to update the database(MySQL) with the records in the uploaded file(specifically .csv). However, in the phpscript, I can only get the filename and not the complete path of the file specificed. fopen() fails due to this reason. Can anyone please let me know how I can work on finding the complete path?

HTML Code:

<html>
<head>
</head>
<body>
<form method="POST" action="upload.php" enctype="multipart/form-data">
    <p>File to upload : <input type ="file" name = "UploadFileName"></p><br />
    <input type = "submit" name = "Submit" value = "Press THIS to upload">
</form>
</body>
</html>

PHP Script:

<?php
   .....
......
   $handle = fopen($_FILES["UploadFileName"]["name"], "r"); # fopen(test.csv) [function.fopen]: failed to open stream: No such file or directory
?>
like image 807
name_masked Avatar asked Mar 27 '11 16:03

name_masked


People also ask

How do I find the path of a file in php?

php $path = "C:\\test_folder\\folder2\\folder4"; $sub_folder = scandir($path); $num = count($sub_folder); for ($i = 2; $i < $num; $i++) { if(is_file($path. '\\'.

How do I find the file upload path?

When the Upload Button is clicked, the filename is extracted from the FileName property using the GetFileName function of the Path class. Then the uploaded File is saved into the Uploads Folder (Directory) using SaveAs method.

How can I upload image path in php?

$ext; $final_pathdir = $dirname . $image_name3; $suc = move_uploaded_file($_FILES['fileimg1']['tmp_name'], $final_pathdir); if ($suc > 0) { echo "Image uploaded successfully"; } else { echo "Error : " . $_FILES['filimg1']['error']; } ?>


1 Answers

name refers to the filename on the client-side. To get the filename (including the full path) on the server-side, you need to use tmp_name:

$handle = fopen($_FILES["UploadFileName"]["tmp_name"], 'r');
like image 196
netcoder Avatar answered Nov 07 '22 06:11

netcoder