/    Sign up×
Community /Pin to ProfileBookmark

Newbie problem with uploading txt file

Hi,

I got this code from a books CD, but it doesn’t seem to work. Can you see any problems with the code? It gives me these errors.

Warning: move_uploaded_file(/uploads/message.txt) [function.move-uploaded-file]: failed to open stream: No such file or directory in C:webserverhtdocsC19upload.php on line 43

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move ‘C:webservertopkatphpD9DF.tmp’ to ‘/uploads/message.txt’ in C:webserverhtdocsC19upload.php on line 43
Problem: Could not move file to destination directory

Line 43 is

[code=php] if (!move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], $upfile))[/code]

Here’s the HTML form.

[code=html]<html>
<head>
<title>Administration – upload new files</title>
</head>
<body>
<h1>Upload new news files</h1>
<form action=”upload.php” method=”post” enctype=”multipart/form-data” />
<div>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”1000000″ />
Upload this file: <input name=”userfile” type=”file” />
<input type=”submit” value=”Send File” />
</div>
</form>
</body>
</html>[/code]

And the PHP

[code=php]<html>
<head>
<title>Uploading…</title>
</head>
<body>
<h1>Uploading file…</h1>
<?php

//Check to see if an error code was generated on the upload attempt
if ($_FILES[‘userfile’][‘error’] > 0)
{
echo ‘Problem: ‘;
switch ($_FILES[‘userfile’][‘error’])
{
case 1: echo ‘File exceeded upload_max_filesize’;
break;
case 2: echo ‘File exceeded max_file_size’;
break;
case 3: echo ‘File only partially uploaded’;
break;
case 4: echo ‘No file uploaded’;
break;
case 6: echo ‘Cannot upload file: No temp directory specified.’;
break;
case 7: echo ‘Upload failed: Cannot write to disk.’;
break;
}
exit;
}

// Does the file have the right MIME type?
if ($_FILES[‘userfile’][‘type’] != ‘text/plain’)
{
echo ‘Problem: file is not plain text’;
exit;
}

// put the file where we’d like it
$upfile = ‘/uploads/’.$_FILES[‘userfile’][‘name’];

if (is_uploaded_file($_FILES[‘userfile’][‘tmp_name’]))
{
if (!move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], $upfile))
{
echo ‘Problem: Could not move file to destination directory’;
exit;
}
}
else
{
echo ‘Problem: Possible file upload attack. Filename: ‘;
echo $_FILES[‘userfile’][‘name’];
exit;
}

echo ‘File uploaded successfully<br><br>’;

// reformat the file contents
$fp = fopen($upfile, ‘r’);
$contents = fread ($fp, filesize ($upfile));
fclose ($fp);

$contents = strip_tags($contents);
$fp = fopen($upfile, ‘w’);
fwrite($fp, $contents);
fclose($fp);

// show what was uploaded
echo ‘Preview of uploaded file contents:<br><hr>’;
echo $contents;
echo ‘<br><hr>’;

?>
</body>
</html>
[/code]

Any ideas?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@TopkatauthorDec 07.2008 — I fixed it!!! :-)

I took out

'/uploads/'.

from line 39 and it worked!!! :-)
×

Success!

Help @Topkat 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.17,
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,
)...