3

I am trying to write a script which uploads a file via a html form. When I click submit nothing happens.

file: upload_form.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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>

<form action="do_upload.php" method="post" enctype="multipart/form-data"></form>
<p><strong>File to upload</strong></p>
<p><input name="img1" type="file" size="30" /></p>
<p><input name="submit" type="submit" value="Upolad File" /></p>

</body>
</html>

file: do_upload.php

<?php
if ($_FILES[img1] != "" {
    @copy($_FILES[img1] [tm_name], "/tmp" .$_FILES[img1][name])
    or die("couldnt copy the file");
} else {
    die("no file specified");
}
?>

<HTML>
<head>
<title>Successfull File Upload</title>
</head>
<body>

<h1>Success</h1>
<p>You sent: <? echo $_FILES[img1][name]; ?>, a <? echo $_FILES[img1][size]; ?>byte filw with a mime type of <? echo $_FILES[img1][type]; ?></p>

</body>
</HTML>
4
  • 2
    "Nothing happens" as in "you get a blank page"? "It doesn't submit"? There's a syntax error in your second file, which may be the reason that nothing's happening. (In the first one as well, actually. :)) Commented Apr 16, 2010 at 2:48
  • as it it doesnt submit, I had the close </form> to early. Now I have to find an error on line 2 in the do_upload.php Commented Apr 16, 2010 at 2:51
  • 2
    You're missing the close parentheses. String literal keys should be quoted ($_FILES['img1'] rather than $_FILES[img1]; php.net/manual/en/…). Also, you should be using empty, isset or array_key_exists rather than comparing a potentially non-existent array value to the empty string. Lastly, don't use die that way (phpfreaks.com/blog/or-die-must-die). Commented Apr 16, 2010 at 2:53
  • where is the tmp directory located if this script is hosted at this address. /domains/domain.com.au/public_htm Commented Apr 16, 2010 at 3:05

2 Answers 2

6
<form action="do_upload.php" method="post" enctype="multipart/form-data"></form>
<p><strong>File to upload</strong></p>
<p><input name="img1" type="file" size="30" /></p>
<p><input name="submit" type="submit" value="Upolad File" /></p>

to

<form action="do_upload.php" method="post" enctype="multipart/form-data">
<p><strong>File to upload</strong></p>
<p><input name="img1" type="file" size="30" /></p>
<p><input name="submit" type="submit" value="Upolad File" /></p>
</form>

and

if ($_FILES[img1] != "" {

to

if (isset($_FILES['img1'])) {
Sign up to request clarification or add additional context in comments.

8 Comments

Well, now my answer looks pretty dumb. :) That close tag was staring at me the whole time and I never noticed it.
@Jacksta Exactly, there's the other error. Hint: There's no closing parenthesis for the if (.
Though as mentioned in the other comments, use isset() to check. Updated my answer.
And it sounds like you would very much benefit from reading a tutorial on file uploads.
Try reading the tutorial I linked to instead of what ever you're using in the book.
|
3

If you're not receiving any errors at all, add the following lines to the top of your script to ensure that error reporting is set up properly:

error_reporting(E_ALL);
ini_set('display_errors', 'On');

1 Comment

If it's an error. He isn't getting anything because his <form> tags are out of wack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.