Reputation: 534
<html>
<title>TestUploader</title>
<body>
<?php
if (isset($_POST['Submit1'])) {
//display contents of file that was uploaded
}
?>
<form name="form1" method="POST" action="testupload.php">
<div>
Upload A File
<br />
<input type="file" name="UserFile" size="50"/>
<br />
<input type="submit" value="Upload" name="Submit1"/>
</div>
</form>
</body>
</html>
What I am trying to do (and not really finding anything online in terms of examples) is have the user upload a file on their local machine and have PHP read it back to me.
In terms of the actual conditions within my project, it will be a CSV file that I would like to put into an array - but I can't figure out how to do this even with a simple txt file with contents 'hello world'
how i have it constructed in my mind is through POST but i am very novice at php at the moment and do not know if there is a better practice. Could anybody give me guidance or a resource for what i am trying to do? I would prefer to not use MySQL simply because I do not know MySQL yet and am trying to not learn too many things at once and the rest of my project currently does not use it.
Upvotes: 0
Views: 2538
Reputation: 229
Here's the code you can try
if (isset($_POST['Submit1'])) {
if (is_uploaded_file($_FILES['UserFile']['tmp_name'])) {
echo "<h1>" . "File ". $_FILES['UserFile']['name'] ." uploaded successfully." . "</h1>";
echo "<h2>Displaying contents:</h2>";
readfile($_FILES['UserFile']['tmp_name']);
}
}
Upvotes: 2
Reputation: 3520
Here are two good reads to solve your problem
Upvotes: 2
Reputation: 76601
Use $_FILE["UploadFile"]
at your server. This is not related to MySQL, as MySQL is an RDBMS where you store data. File upload itself is unrelated to MySQL, you can store things in a database though. See more here.
Upvotes: 0
Reputation: 496
your form needs to send enctype="multipart/form-data"
and you need to look for $_FILES
.
have a look at the tutorial http://www.w3schools.com/php/php_file_upload.asp
Upvotes: 1