/    Sign up×
Community /Pin to ProfileBookmark

Upload file and see contents on the same page

I need a single page with a file upload and a text area where the contents of the file are put.

At the moment I have a html file and a php file:

upload.html……

[COLOR=”Blue”]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>

<head>
<meta content=”text/html; charset=utf-8″ http-equiv=”Content-Type” />
<title>Type</title>
</head>

<body>

<form enctype=”multipart/form-data” action=”uploader.php” method=”POST”>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”100000″ />
Choose a file to upload: <input name=”uploadedfile” type=”file” /><br />
<input type=”submit” value=”Upload File” />
<textarea name=”Values”></textarea>
</form>
</body>

</html>
[/COLOR]

uploader.php…….

[COLOR=”Blue”]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>

<head>
<meta content=”text/html; charset=utf-8″ http-equiv=”Content-Type” />
<title>Untitled 2</title>
</head>

<body>

<?php
$target_path = “uploads/”;

$target_path = $target_path . basename( $_FILES[‘uploadedfile’][‘name’]);
$filename = basename( $_
FILES[‘uploadedfile’][‘name’]);

if(move_uploaded_file($_FILES[‘uploadedfile’][‘tmp_name’], $target_path)) {
// echo “The file ” . $filename . ” has been uploaded”;
} else{
echo “There was an error uploading the file, please try again!”;
}

$myFile = “uploads/” . $filename;
$fh = fopen($myFile, ‘r’);
theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;?>

</body>

</html>[/COLOR]

I need the contents of the file selected to upload put into the textarea named “Values” on the same page as the upload form instead of on the uploader.php page.

Any help is appreciated! ?

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@hastxFeb 15.2011 — I assume you only want to allow text files? That could get tricky if you allow spreadsheets etc.
Copy linkTweet thisAlerts:
@claireneedshelpauthorFeb 15.2011 — Ideally I need to get values from a text file and excel file and possibly from a csv file
Copy linkTweet thisAlerts:
@hastxFeb 15.2011 — You wont be able to get values out of an excel file if you are using the standard web host...you might stand a chance of displaying contents if you have root access to your own server... That is why I ask what type of files will be used. You really have to define and set limits on the format of the file before scripting what you are trying to do. A CSV is a standard format, and you should restrict the program to that.
Copy linkTweet thisAlerts:
@NogDogFeb 15.2011 — There are some 3rd-party PHP classes/scripts out there that can supposedly read Excel files, but I've never tried any. CSV is fairly simple with the fgetcsv() function, and anyone with Excel can easily save a spreadsheet as a CSV file, but that does require the user to perform that extra task.
Copy linkTweet thisAlerts:
@hastxFeb 15.2011 — I have successfully read word/Excel files using catdoc, etc, but had to do that by installing the poppler-utils package on the server. If someone has developed a standalone lib that would really simplify things and allow someone to use it without server access.
Copy linkTweet thisAlerts:
@claireneedshelpauthorFeb 15.2011 — ok so ignoring the excel file, how do I get the values from a text file and csv file into the text area?

Im new to php and have very basic knowledge of it.
Copy linkTweet thisAlerts:
@NogDogFeb 15.2011 — At the most basic:
[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Type</title>
</head>
<body>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
<textarea name="Values"><?php
if (!empty($_FILES['uploadedfile']) && file_exists($_FILES['uploaded_file']['tmp_name'])) {
echo htmlentities(file_get_contents($_FILES['uploadedfile']['tmp_name']) , ENT_QUOTES, 'UTF-8');
}
?></textarea>
</form>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@claireneedshelpauthorFeb 15.2011 — Thanks....Ive just tried that and when I select a text file and click upload it says 'No file chosen'
Copy linkTweet thisAlerts:
@NogDogFeb 15.2011 — I had one typo which might be part of the problem: in one place I used [b]$_FILES['uploaded_file'][/b] instead of [b]$_FILES['uploadedfile'][/b] (no underscore). With that fix it ran OK for me.
Copy linkTweet thisAlerts:
@eval_BadCode_Feb 15.2011 — You wont be able to get values out of an excel file if you are using the standard web host...you might stand a chance of displaying contents if you have root access to your own server... That is why I ask what type of files will be used. You really have to define and set limits on the format of the file before scripting what you are trying to do. A CSV is a standard format, and you should restrict the program to that.[/QUOTE]

just send it to python and get the xlrd module.

Goes through excel books really fast and can return it to PHP in csv format... its ~10 lines of code to do that, just 2 for loops XD ?, some imports etc.

Every shared host has python.

I didn't bother messing with it to show you only the parts you need. Its like the 4th program I had ever written so you'll have to dig through it to find the parts I did correctly which will be hidden.

Something like this:
[CODE]
import sys
import platform
if sys.platform != 'win32':
sys.path.append( "/ip/vir/wwws/lib/py/xlrd" )
from datetime import date,datetime,time
from xlrd import open_workbook,xldate_as_tuple
import string

if __name__ == '__main__':
if sys.platform != "win32":
path = "/ip/vir/wwws/files/"
xls_book = sys.argv[1] #Open the workbook
wb.sheet_names() #check sheet names

#Get the first sheet either by index or by name

sh = wb.sheet_by_index(0)
sh = wb.sheet_by_name(u'Sheet1')


#Iterate through rows, returning each as a list that you can index:
values = '';
for sh in wb.sheet_names():
values += "==================:: new sheet ::=================="
sh = wb.sheet_by_name(sh)
for rownum in range(sh.nrows):
values += sh.row_values(rownum)

with foo as open(path + sys.argv[1] + ".csv", "w"):
foo.write(values)


#fin
[/CODE]


Aside from the missing commas and errors ? this will convert an xls file to csv. My original script was much more complicated, this one will likely have trouble printing dates etc.
Copy linkTweet thisAlerts:
@claireneedshelpauthorFeb 16.2011 — thank you so much nogdog that works perfectly!

eval.....that code has completely confused me but thank you for the idea!
Copy linkTweet thisAlerts:
@claireneedshelpauthorFeb 16.2011 — nogdog.....after having some time to use your code and put it together with my own code I have noticed that whenever I click Upload File, the values are put into the text area but all over values in elements on other forms I have on the page are reset

Is there a way of stopping the other forms elements from resetting back to the default?
×

Success!

Help @claireneedshelp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.12,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...