Getting complete PATH of uploaded file - PHP
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
?>
Solution 1:
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');
Solution 2:
$target='uploads/'.basename($_FILES['UploadFileName']['name']);
if(move_uploaded_file($_FILES['UploadFileName']['tmp_name'],$target)) {
//Insert into your db
$fp = fopen($target, "r");
}