/    Sign up×
Community /Pin to ProfileBookmark

PHP File Upload on single page?

Hi,

Ideally I want to have a file upload form and then the contents of the file uploaded put into a text box on the same page.

At the moment I have two pages, upload.html and uploader.php

upload.html contains the form and the user selects which file to upload……uploader.php then shows the contents of the file.

I want this to happen all on the one page is this possible and how would I do this?

The code I currently have is:

upload.html:

<!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>

uploader.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>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>

Is this possible and how would I do it?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@OctoberWindDec 15.2010 — I'm a little confused, because it looks like upload.html and uploader.php are the same in your OP, but something like this should be able to get you in the right direction.

[code=php]

if (
(isset($_POST['submit'])) &&
(isset($_POST['file_upload']))
(
{
// validate and display the file upload

// (1) if you log any errors here in an $error_array[]
}
else
{
// show file upload form

// (1) then you can display said $error_array[] here, to inform the user.
}

[/code]
Copy linkTweet thisAlerts:
@claireneedshelpauthorDec 15.2010 — thanks but where do i put this code?
Copy linkTweet thisAlerts:
@OctoberWindDec 15.2010 — This replaces upload.html (obviously, saving it as .php).

The idea is to put the logic/code for both the file upload and the process/display on the same page, and use either an if/else, or switch/case to determine which code to use.
Copy linkTweet thisAlerts:
@claireneedshelpauthorDec 21.2010 — Im still confused about what code goes where in this if statement, can you tell me what code to put in the statement please?
×

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.18,
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,
)...